3f7b35c500ff8d004d726f735bf90026461ae999
[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_TEST_DSN);
21 $this->conn->exec(PQ_TEST_SETUP_SQL);
22 Table::$defaultConnection = $this->conn;
23 $this->table = new Table("test");
24 $this->table->getQueryExecutor()->attach(new \QueryLogger());
25 }
26
27 protected function tearDown() {
28 $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
29 }
30
31 public function testSetRowsetPrototype() {
32 $prop = new \ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
33 $prop->setAccessible(true);
34 $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table));
35 $this->table->setRowsetPrototype(null);
36 $this->assertNull($prop->getValue($this->table));
37 $rowset = new \pq\Gateway\Rowset($this->table);
38 $this->table->setRowsetPrototype($rowset);
39 $this->assertSame($rowset, $prop->getValue($this->table));
40 }
41
42 public function testGetConnection() {
43 $this->assertSame($this->conn, $this->table->getConnection());
44 }
45
46 public function testGetName() {
47 $this->assertSame("test", $this->table->getName());
48 }
49
50 public function testFind() {
51 $rowset = $this->table->find();
52 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
53 $rowset = $this->table->find(array("id = " => 1));
54 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
55 $rowset = $this->table->find(array("id = " => 0));
56 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
57 $rowset = $this->table->find(array(array("id<" => 2), array("id>" => 2)));
58 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
59 }
60
61 public function testCreate() {
62 $rowset = $this->table->create(array("id" => new \pq\Query\Expr("DEFAULT")));
63 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
64 $this->assertCount(1, $rowset);
65 }
66
67 public function testUpdate() {
68 $row = $this->table->create(array())->current();
69 $data = array(
70 "created" => new \pq\DateTime("2013-03-03 03:03:03"),
71 "counter" => 2,
72 "number" => 2.2,
73 "data" => "this is a test",
74 "list" => array(3,2,1),
75 "prop" => null
76 );
77 $row = $this->table->update(array("id = " => $row->id), $data)->current();
78 $data = array("id" => $row->id->get()) + $data;
79 $this->assertEquals($data, $row->getData());
80 }
81
82 public function testDelete() {
83 $this->table->delete(array("id!=" => 0));
84 $this->assertCount(0, $this->table->find());
85 }
86
87 public function testWith() {
88 $relation = $this->table->getRelations("test")->reftest;
89 $rowset = $this->table->with([$relation], array("another_test_id=" => 2));
90 $this->assertCount(1, $rowset);
91 $this->assertEquals(array(
92 "id" => 2,
93 "created" => new \pq\DateTime("today"),
94 "counter" => 0,
95 "number" => 0,
96 "data" => "today",
97 "list" => array(0,1,2),
98 "prop" => null
99 ), $rowset->current()->getData());
100 }
101 }