65c93f717b483923d5bd87c935165ff528f5b8f2
[m6w6/pq-gateway] / tests / lib / pq / Gateway / TableTest.php
1 <?php
2
3 namespace pq\Gateway;
4
5 include_once __DIR__."/../../../setup.inc";
6
7 class TableTest extends \PHPUnit_Framework_TestCase {
8
9 /**
10 * @var \pq\Connection
11 */
12 protected $conn;
13
14 /**
15 * @var Table
16 */
17 protected $table;
18
19 protected function setUp() {
20 $this->conn = new \pq\Connection(PQ_DSN);
21 $this->conn->exec(PQ_TEST_DROP_TABLE);
22 $this->conn->exec(PQ_TEST_CREATE_TABLE);
23 Table::$defaultConnection = $this->conn;
24 $this->table = new Table(PQ_TEST_TABLE_NAME);
25 }
26
27 protected function tearDown() {
28 $this->conn->exec(PQ_TEST_DROP_TABLE);
29 }
30
31 protected function createTestData() {
32 $this->conn->exec(PQ_TEST_CREATE_DATA);
33 }
34
35 public function testSetRowsetPrototype() {
36 $prop = new \ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
37 $prop->setAccessible(true);
38 $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table));
39 $this->table->setRowsetPrototype(null);
40 $this->assertNull($prop->getValue($this->table));
41 $rowset = new \pq\Gateway\Rowset($this->table);
42 $this->table->setRowsetPrototype($rowset);
43 $this->assertSame($rowset, $prop->getValue($this->table));
44 }
45
46 public function testGetConnection() {
47 $this->assertSame($this->conn, $this->table->getConnection());
48 }
49
50 public function testGetName() {
51 $this->assertSame(PQ_TEST_TABLE_NAME, $this->table->getName());
52 }
53
54 public function testFind() {
55 $rowset = $this->table->find();
56 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
57 $rowset = $this->table->find(array("id = " => 1));
58 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
59 $rowset = $this->table->find(array("id = " => 0));
60 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
61 $rowset = $this->table->find(array(array("id<" => 2), array("id>" => 2)));
62 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
63 }
64
65 public function testCreate() {
66 $rowset = $this->table->create(array("id" => new \pq\Query\Expr("DEFAULT")));
67 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
68 $this->assertCount(1, $rowset);
69 }
70
71 public function testUpdate() {
72 $row = $this->table->create(array())->current();
73 $data = array(
74 "created" => "2013-03-03 03:03:03",
75 "counter" => 2,
76 "number" => 2.2,
77 "data" => "this is a test",
78 );
79 $row = $this->table->update(array("id = " => $row->id), $data)->current();
80 $data = array("id" => $row->id->get()) + $data;
81 $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData());
82 }
83
84 public function testDelete() {
85 $this->table->delete(array("id!=" => 0));
86 $this->assertCount(0, $this->table->find());
87 }
88 }