mapper test
[m6w6/pq-gateway] / tests / lib / pq / Mapper / MapperTest.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 MapperTest 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 testBasic() {
44 $this->mapper->register($this->map);
45 $this->assertSame($this->map, $this->mapper->mapOf(TestModel::class));
46 $this->assertInstanceOf(MapInterface::class, $this->mapper->mapOf(RefTestModel::class));
47 }
48
49 /**
50 * @expectedException UnexpectedValueException
51 */
52 function testMapOfException() {
53 $this->mapper->mapOf(new stdClass);
54 }
55
56 function testCreateStorage() {
57 $this->assertInstanceOf(StorageInterface::class, $this->mapper->createStorage(TestModel::class));
58 }
59
60 /**
61 * @expectedException UnexpectedValueException
62 */
63 function testCreateStorageException() {
64 $this->assertInstanceOf(StorageInterface::class, $this->mapper->createStorage(foo::class));
65 }
66
67 function testGetReflector() {
68 $o = new RefTestModel;
69 $r = $this->mapper->getReflector($o, "pk1");
70 $this->assertInstanceOf("ReflectionProperty", $r);
71 $this->assertNull($r->getValue($o));
72 $r->setValue($o, 1);
73 $this->assertSame(1, $r->getValue($o));
74 }
75 }