storage 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 QueryLogger;
8 use RefTestModel;
9 use stdClass;
10 use TestModel;
11 use UnexpectedValueException;
12
13 require_once __DIR__."/../../../setup.inc";
14
15 class MapperTest extends \PHPUnit_Framework_TestCase
16 {
17 /**
18 * @var Connection
19 */
20 protected $conn;
21
22 /**
23 * @var Mapper
24 */
25 protected $mapper;
26
27 /**
28 * @var Map
29 */
30 protected $map;
31
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 $this->map = TestModel::mapAs($this->mapper);
38 $this->map->getGateway()->getQueryExecutor()->attach(new QueryLogger());
39 }
40
41 protected function tearDown() {
42 $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
43 }
44
45 function testBasic() {
46 $this->mapper->register($this->map);
47 $this->assertSame($this->map, $this->mapper->mapOf(TestModel::class));
48 $this->assertInstanceOf(MapInterface::class, $this->mapper->mapOf(RefTestModel::class));
49 }
50
51 /**
52 * @expectedException UnexpectedValueException
53 */
54 function testMapOfException() {
55 $this->mapper->mapOf(new stdClass);
56 }
57
58 function testCreateStorage() {
59 $this->assertInstanceOf(StorageInterface::class, $this->mapper->createStorage(TestModel::class));
60 }
61
62 /**
63 * @expectedException UnexpectedValueException
64 */
65 function testCreateStorageException() {
66 $this->assertInstanceOf(StorageInterface::class, $this->mapper->createStorage(foo::class));
67 }
68
69 function testGetReflector() {
70 $o = new RefTestModel;
71 $r = $this->mapper->getReflector($o, "pk1");
72 $this->assertInstanceOf("ReflectionProperty", $r);
73 $this->assertNull($r->getValue($o));
74 $r->setValue($o, 1);
75 $this->assertSame(1, $r->getValue($o));
76 }
77 }