map test
[m6w6/pq-gateway] / tests / lib / pq / Mapper / MapTest.php
1 <?php
2
3 namespace pq\Mapper;
4
5 use pq\Connection;
6 use pq\Gateway\Table;
7 use RefTestModel;
8 use stdClass;
9 use TestModel;
10 use UnexpectedValueException;
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 = TestModel::mapAs($this->mapper);
37 }
38
39 protected function tearDown() {
40 $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
41 }
42
43 function testMap() {
44 $row = $this->map->getGateway()->find(["id="=>1])->current();
45 $obj = $this->map->map($row);
46 $this->assertEquals($row->data->get(), $obj->data);
47 }
48
49 function testUnmap() {
50 $obj = new \TestModel;
51 $obj->data = "this is a test";
52 $this->map->unmap($obj);
53 $this->assertSame(4, $obj->id);
54 }
55
56 function testUnmapRef() {
57 $obj = new \TestModel;
58 $obj->ref1 = $obj->ref2 = [
59 new \RefTestModel
60 ];
61 $this->map->unmap($obj);
62 }
63 }
64