e1b66870c706fe1e9f7518e1bef1ed8722c2435e
[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_TABLE_CREATE);
22 $this->conn->exec(PQ_TEST_REFTABLE_CREATE);
23 $this->conn->exec(PQ_TEST_DATA);
24 Table::$defaultConnection = $this->conn;
25 $this->table = new Table("test");
26 $this->table->getQueryExecutor()->attach(new \QueryLogger());
27 }
28
29 protected function tearDown() {
30 $this->conn->exec(PQ_TEST_REFTABLE_DROP);
31 $this->conn->exec(PQ_TEST_TABLE_DROP);
32 }
33
34 public function testSetRowsetPrototype() {
35 $prop = new \ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
36 $prop->setAccessible(true);
37 $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table));
38 $this->table->setRowsetPrototype(null);
39 $this->assertNull($prop->getValue($this->table));
40 $rowset = new \pq\Gateway\Rowset($this->table);
41 $this->table->setRowsetPrototype($rowset);
42 $this->assertSame($rowset, $prop->getValue($this->table));
43 }
44
45 public function testGetConnection() {
46 $this->assertSame($this->conn, $this->table->getConnection());
47 }
48
49 public function testGetName() {
50 $this->assertSame("test", $this->table->getName());
51 }
52
53 public function testFind() {
54 $rowset = $this->table->find();
55 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
56 $rowset = $this->table->find(array("id = " => 1));
57 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
58 $rowset = $this->table->find(array("id = " => 0));
59 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
60 $rowset = $this->table->find(array(array("id<" => 2), array("id>" => 2)));
61 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
62 }
63
64 public function testCreate() {
65 $rowset = $this->table->create(array("id" => new \pq\Query\Expr("DEFAULT")));
66 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
67 $this->assertCount(1, $rowset);
68 }
69
70 public function testUpdate() {
71 $row = $this->table->create(array())->current();
72 $data = array(
73 "created" => "2013-03-03 03:03:03",
74 "counter" => 2,
75 "number" => 2.2,
76 "data" => "this is a test",
77 );
78 $row = $this->table->update(array("id = " => $row->id), $data)->current();
79 $data = array("id" => $row->id->get()) + $data;
80 $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData());
81 }
82
83 public function testDelete() {
84 $this->table->delete(array("id!=" => 0));
85 $this->assertCount(0, $this->table->find());
86 }
87 }