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