5 require_once __DIR__
."/../../../setup.inc";
7 use BadMethodCallException
;
11 use pq\Mapper\ObjectManager
;
15 class ObjectManagerTest
extends \PHPUnit_Framework_TestCase
30 protected $objectManager;
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();
42 protected function tearDown() {
43 $this->conn
->exec(PQ_TEST_TEARDOWN_SQL
);
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));
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));
63 * @expectedException BadMethodCallException
65 function testGetObjectException() {
66 $row = $this->objectManager
->getMap()->getGateway()->find(["id="=>1])->current();
67 $this->objectManager
->getObject($row);
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));
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));
90 function testFalseRowId() {
91 $this->assertFalse($this->objectManager
->rowId(new Row($this->objectManager
->getMap()->getGateway())));
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));
101 function testSerializeRowIdScalar() {
103 $this->objectManager
->serializeRowId(["id" => 1]),
104 $this->objectManager
->serializeRowId($this->objectManager
->serializeRowId(["id"=>1]))
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));
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));
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));
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));
137 * @expectedException \BadMethodCallException
139 function testGetRowException() {
140 $this->objectManager
->getRow(new \stdClass
);