mapper test
[m6w6/pq-gateway] / tests / lib / pq / Mapper / ObjectManagerTest.php
1 <?php
2
3 namespace pq\Mapper;
4
5 require_once __DIR__."/../../../setup.inc";
6
7 use BadMethodCallException;
8 use pq\Connection;
9 use pq\Gateway\Row;
10 use pq\Gateway\Table;
11 use pq\Mapper\ObjectManager;
12 use QueryLogger;
13 use TestModel;
14
15 class ObjectManagerTest extends \PHPUnit_Framework_TestCase
16 {
17 /**
18 * @var Connection
19 */
20 protected $conn;
21
22 /**
23 * @var Mapper
24 */
25 protected $mapper;
26
27 /**
28 * @var ObjectManager
29 */
30 protected $objectManager;
31
32 protected function setUp() {
33 $this->conn = new Connection(PQ_TEST_DSN);
34 $this->conn->exec(PQ_TEST_SETUP_SQL);
35 Table::$defaultConnection = $this->conn;
36 $this->mapper = new Mapper;
37 $mapping = TestModel::mapAs($this->mapper);
38 $mapping->getGateway()->getQueryExecutor()->attach(new QueryLogger());
39 $this->objectManager = $mapping->getObjects();
40 }
41
42 protected function tearDown() {
43 $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
44 }
45
46 function testBasic() {
47 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
48 $row_id = $this->objectManager->rowId($row);
49 $this->assertFalse($this->objectManager->hasObject($row_id));
50 $this->objectManager->createObject($row);
51 $this->assertTrue($this->objectManager->hasObject($row_id));
52 }
53
54 function testGetObject() {
55 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
56 $row_id = $this->objectManager->rowId($row);
57 $this->objectManager->createObject($row);
58 $this->assertTrue($this->objectManager->hasObject($row_id));
59 $this->assertInstanceof(TestModel::class, $this->objectManager->getObject($row));
60 }
61
62 /**
63 * @expectedException BadMethodCallException
64 */
65 function testGetObjectException() {
66 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
67 $this->objectManager->getObject($row);
68 }
69
70 function testReset() {
71 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
72 $row_id = $this->objectManager->rowId($row);
73 $this->assertFalse($this->objectManager->hasObject($row_id));
74 $this->objectManager->createObject($row);
75 $this->assertTrue($this->objectManager->hasObject($row_id));
76 $this->objectManager->reset();
77 $this->assertFalse($this->objectManager->hasObject($row_id));
78 }
79
80 function testResetObject() {
81 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
82 $row_id = $this->objectManager->rowId($row);
83 $this->assertFalse($this->objectManager->hasObject($row_id));
84 $this->objectManager->createObject($row);
85 $this->assertTrue($this->objectManager->hasObject($row_id));
86 $this->objectManager->resetObject($row);
87 $this->assertFalse($this->objectManager->hasObject($row_id));
88 }
89
90 function testFalseRowId() {
91 $this->assertFalse($this->objectManager->rowId(new Row($this->objectManager->getMap()->getGateway())));
92 }
93
94 function testExtractRowId() {
95 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
96 $row_id = $this->objectManager->rowId($row);
97 $object = $this->objectManager->getMap()->map($row);
98 $this->assertEquals($row_id, $this->objectManager->extractRowId($object));
99 }
100
101 function testSerializeRowIdScalar() {
102 $this->assertEquals(
103 $this->objectManager->serializeRowId(["id" => 1]),
104 $this->objectManager->serializeRowId($this->objectManager->serializeRowId(["id"=>1]))
105 );
106 }
107
108 function testSerializeRowIdNull() {
109 $this->assertEquals("null", $this->objectManager->serializeRowId(null));
110 $this->assertFalse($this->objectManager->serializeRowId(null, true));
111 $this->assertFalse($this->objectManager->serializeRowId(["id"=>null], true));
112 }
113
114 function testGetRow() {
115 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
116 $obj = $this->objectManager->getMap()->map($row);
117 $this->assertSame($row, $this->objectManager->getRow($obj));
118 }
119
120 function testAsRow() {
121 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
122 $obj = $this->objectManager->getMap()->map($row);
123 $this->assertSame($row, $this->objectManager->asRow($obj));
124 }
125
126 function testHasRow() {
127 $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
128 $obj = $this->objectManager->getMap()->map($row);
129 $this->assertSame($row, $this->objectManager->asRow($obj));
130 $this->assertTrue($this->objectManager->hasRow($this->objectManager->objectId($obj)));
131 $this->objectManager->resetRow($obj);
132 $this->assertFalse($this->objectManager->hasRow($this->objectManager->objectId($obj)));
133 $this->assertNotSame($row, $this->objectManager->asRow($obj));
134 }
135
136 /**
137 * @expectedException \BadMethodCallException
138 */
139 function testGetRowException() {
140 $this->objectManager->getRow(new \stdClass);
141 }
142 }