5 use PHPUnit_Framework_TestCase
;
12 require_once __DIR__
."/../../../setup.inc";
14 class StorageTest
extends PHPUnit_Framework_TestCase
31 protected function setUp() {
32 $this->conn
= new Connection(PQ_TEST_DSN
);
33 $this->conn
->exec(PQ_TEST_SETUP_SQL
);
34 Table
::$defaultConnection = $this->conn
;
35 $this->mapper
= new Mapper
;
36 $this->mapper
->mapOf(TestModel
::class)->getGateway()->getQueryExecutor()->attach(new QueryLogger());
37 $this->mapper
->mapOf(RefTestModel
::class)->getGateway()->getQueryExecutor()->attach(new QueryLogger());
38 $this->storage
= $this->mapper
->createStorage(TestModel
::class);
41 protected function tearDown() {
42 $this->conn
->exec(PQ_TEST_TEARDOWN_SQL
);
46 $objects = $this->storage
->find();
47 for ($i = 0; $i < count($objects); ++
$i) {
48 $this->assertSame($i+
1, $objects[$i]->id
);
53 $test = new TestModel
;
54 $test->ref1
= $test->ref2
= [
57 $this->storage
->save($test);
59 $this->mapper
->mapOf(TestModel
::class)->getObjects()->reset();
60 $this->mapper
->mapOf(RefTestModel
::class)->getObjects()->reset();
62 $this->assertEquals([$test], $this->storage
->find(["id="=>$test->id
]));
65 function testDelete() {
66 $obj = current($this->storage
->find());
67 $this->storage
->delete($obj);
68 $this->mapper
->mapOf(TestModel
::class)->getObjects()->resetRow($obj);
69 $this->assertCount(0, $this->storage
->find(["id="=>$obj->id
]));
72 function testBuffer() {
73 $this->storage
->buffer();
74 $this->assertEquals("yesterday", $this->storage
->get(1)->data
);
75 $this->mapper
->mapOf(TestModel
::class)->getObjects()->reset();
76 $exec = $this->mapper
->mapOf(TestModel
::class)->getGateway()->getQueryExecutor();
77 $xact = executeInConcurrentTransaction($exec, "UPDATE test SET data=\$2 WHERE id=\$1", [1, "the day before"]);
78 $this->assertEquals("yesterday", $this->storage
->get(1)->data
);
80 $this->storage
->discard();
81 $this->assertEquals("the day before", $this->storage
->get(1)->data
);
85 $obj = $this->storage
->get(1);
86 $ref = new Storage($this->mapper
, RefTestModel
::class);
87 $this->assertSame($obj->ref1
, $ref->of($obj, "test"));
88 $this->assertSame($obj->ref2
, $ref->of($obj, "another_test"));
92 $ref = new Storage($this->mapper
, RefTestModel
::class);
93 $obj = $ref->get([2,2]);
94 $this->assertSame($obj->one
, $this->storage
->by($obj, "test"));
95 $this->assertSame($obj->two
, $this->storage
->by($obj, "another_test"));