add test
[m6w6/pq-gateway] / tests / lib / pq / Gateway / TableTest.php
1 <?php
2
3 namespace pq\Gateway;
4
5 include __DIR__."/../../../setup.inc";
6
7 /**
8 * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-03-05 at 16:08:03.
9 */
10 class TableTest extends \PHPUnit_Framework_TestCase {
11
12 /**
13 * @var \pq\Connection
14 */
15 protected $conn;
16
17 /**
18 * @var Table
19 */
20 protected $object;
21
22 /**
23 * Sets up the fixture, for example, opens a network connection.
24 * This method is called before a test is executed.
25 */
26 protected function setUp() {
27 $this->conn = new \pq\Connection(PQ_DSN);
28 $this->conn->exec(PQ_TEST_DROP_TABLE);
29 $this->conn->exec(PQ_TEST_CREATE_TABLE);
30 Table::$defaultConnection = $this->conn;
31 $this->object = new Table(PQ_TEST_TABLE_NAME);
32 }
33
34 /**
35 * Tears down the fixture, for example, closes a network connection.
36 * This method is called after a test is executed.
37 */
38 protected function tearDown() {
39 $this->conn->exec(PQ_TEST_DROP_TABLE);
40 }
41
42 /**
43 * Creates test data in the test table
44 */
45 protected function createTestData() {
46 $this->conn->exec(PQ_TEST_CREATE_DATA);
47 }
48
49 /**
50 * @covers pq\Gateway\Table::setRowsetPrototype
51 */
52 public function testSetRowsetPrototype() {
53 $prop = new \ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
54 $prop->setAccessible(true);
55 $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->object));
56 $this->object->setRowsetPrototype(null);
57 $this->assertNull($prop->getValue($this->object));
58 $rowset = new \pq\Gateway\Rowset($this->object);
59 $this->object->setRowsetPrototype($rowset);
60 $this->assertSame($rowset, $prop->getValue($this->object));
61 }
62
63 /**
64 * @covers pq\Gateway\Table::getConnection
65 */
66 public function testGetConnection() {
67 $this->assertSame($this->conn, $this->object->getConnection());
68 }
69
70 /**
71 * @covers pq\Gateway\Table::getName
72 */
73 public function testGetName() {
74 $this->assertSame(PQ_TEST_TABLE_NAME, $this->object->getName());
75 }
76
77 /**
78 * @covers pq\Gateway\Table::find
79 */
80 public function testFind() {
81 $rowset = $this->object->find();
82 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
83 $rowset = $this->object->find(array("id = " => 1));
84 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
85 $rowset = $this->object->find(array("id = " => 0));
86 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
87 $rowset = $this->object->find(array(array("id<" => 2), array("id>" => 2)));
88 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
89 }
90
91 /**
92 * @covers pq\Gateway\Table::create
93 */
94 public function testCreate() {
95 $rowset = $this->object->create(array("id" => new \pq\Query\Expr("DEFAULT")));
96 $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
97 $this->assertCount(1, $rowset);
98 }
99
100 /**
101 * @covers pq\Gateway\Table::update
102 */
103 public function testUpdate() {
104 $row = $this->object->create(array())->current();
105 $data = array(
106 "created" => "2013-03-03 03:03:03",
107 "counter" => 2,
108 "number" => 2.2,
109 "data" => "this is a test",
110 );
111 $row = $this->object->update(array("id = " => $row->id), $data)->current();
112 $data = array("id" => $row->id->get()) + $data;
113 $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData());
114 }
115
116 /**
117 * @covers pq\Gateway\Table::delete
118 */
119 public function testDelete() {
120 $this->object->delete(array("id!=" => 0));
121 $this->assertCount(0, $this->object->find());
122 }
123
124 }