storage test
[m6w6/pq-gateway] / tests / lib / pq / Mapper / MapTest.php
1 <?php
2
3 namespace pq\Mapper;
4
5 use PHPUnit_Framework_TestCase;
6 use pq\Connection;
7 use pq\Gateway\Table;
8 use QueryLogger;
9 use RefTestModel;
10 use TestModel;
11
12 require_once __DIR__."/../../../setup.inc";
13
14 class MapTest extends PHPUnit_Framework_TestCase
15 {
16 /**
17 * @var Connection
18 */
19 protected $conn;
20
21 /**
22 * @var Mapper
23 */
24 protected $mapper;
25
26 /**
27 * @var Map
28 */
29 protected $map;
30
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->map = $this->mapper->mapOf(TestModel::class);
37 $this->map->getGateway()->getQueryExecutor()->attach(new QueryLogger());
38 $this->mapper->mapOf(RefTestModel::class)->getGateway()->getQueryExecutor()->attach(new QueryLogger());
39 }
40
41 protected function tearDown() {
42 $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
43 }
44
45 function testMap() {
46 $row = $this->map->getGateway()->find(["id="=>1])->current();
47 $obj = $this->map->map($row);
48 $this->assertEquals($row->data->get(), $obj->data);
49 }
50
51 function testUnmap() {
52 $obj = new \TestModel;
53 $obj->data = "this is a test";
54 $this->map->unmap($obj);
55 $this->assertSame(4, $obj->id);
56 }
57
58 function testUnmapRef() {
59 $obj = new \TestModel;
60 $obj->ref1 = $obj->ref2 = [
61 new RefTestModel
62 ];
63 $this->map->unmap($obj);
64 }
65 }
66