|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace As3\Modlr\Persister\MongoDb\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
use Doctrine\MongoDB\Configuration; |
|
7
|
|
|
use Doctrine\MongoDB\Connection; |
|
8
|
|
|
use As3\Modlr; |
|
9
|
|
|
|
|
10
|
|
|
class PersisterTest extends PHPUnit_Framework_TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
protected static $dbName = 'modlr_mongodb'; |
|
13
|
|
|
|
|
14
|
|
|
protected $connection; |
|
15
|
|
|
protected $smf; |
|
16
|
|
|
protected $persister; |
|
17
|
|
|
|
|
18
|
|
|
public function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
// Initialize the doctrine connection |
|
21
|
|
|
$config = new Configuration(); |
|
22
|
|
|
$config->setLoggerCallable(function($msg) {}); |
|
|
|
|
|
|
23
|
|
|
$this->connection = new Connection(null, array(), $config); |
|
24
|
|
|
|
|
25
|
|
|
// Initialize the metadata factory |
|
26
|
|
|
$typeFactory = new Modlr\DataTypes\TypeFactory; |
|
27
|
|
|
$validator = new Modlr\Util\Validator; |
|
28
|
|
|
|
|
29
|
|
|
$config = new Modlr\Rest\RestConfiguration($validator); |
|
30
|
|
|
$config->setRootEndpoint('/modlr/api/'); |
|
31
|
|
|
|
|
32
|
|
|
$entityUtil = new Modlr\Util\EntityUtility($config, $typeFactory); |
|
33
|
|
|
$this->smf = new Modlr\Persister\MongoDb\StorageMetadataFactory($entityUtil); |
|
34
|
|
|
$this->persister = new Modlr\Persister\MongoDb\Persister($this->connection, $this->smf); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function tearDown() |
|
38
|
|
|
{ |
|
39
|
|
|
$collections = $this->connection->selectDatabase(self::$dbName)->listCollections(); |
|
40
|
|
|
foreach ($collections as $collection) { |
|
41
|
|
|
$collection->drop(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->connection->close(); |
|
45
|
|
|
unset($this->connection); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testPersisterValues() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->assertEquals('mongodb', $this->persister->getPersisterKey()); |
|
51
|
|
|
$this->assertEquals('_type', $this->persister->getPolymorphicKey()); |
|
52
|
|
|
$this->assertEquals('_id', $this->persister->getIdentifierKey()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testMDFInstance() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->assertInstanceOf( |
|
58
|
|
|
'As3\Modlr\Metadata\Interfaces\StorageMetadataFactoryInterface', |
|
59
|
|
|
$this->persister->getPersistenceMetadataFactory() |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @expectedException As3\Modlr\Persister\PersisterException |
|
65
|
|
|
* @expectedExceptionMessage ID conversion currently only supports an object strategy, or none at all. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testConvertIdInvalidStrategy() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->assertInstanceOf('MongoId', $this->persister->convertId('test', 'blag')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @expectedException As3\Modlr\Persister\PersisterException |
|
74
|
|
|
* @expectedExceptionMessage ID generation currently only supports an object strategy, or none at all. |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testGenerateIdInvalidStrategy() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->assertInstanceOf('MongoId', $this->persister->generateId('blag')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testConvertIdObject() |
|
82
|
|
|
{ |
|
83
|
|
|
$test = '49a7011a05c677b9a916612a'; |
|
84
|
|
|
$id = new \MongoId($test); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals($id, $this->persister->convertId($id)); |
|
87
|
|
|
$this->assertEquals($id, $this->persister->convertId($id, 'object')); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testGenerateIdObject() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->assertInstanceOf('MongoId', $this->persister->generateId()); |
|
93
|
|
|
$this->assertInstanceOf('MongoId', $this->persister->generateId('object')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @expectedException As3\Modlr\Persister\PersisterException |
|
98
|
|
|
* @expectedExceptionMessage ID conversion currently only supports an object strategy, or none at all. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testConvertIdIncrementId() |
|
101
|
|
|
{ |
|
102
|
|
|
$test = 12345; |
|
103
|
|
|
$this->assertEquals($test, $this->persister->convertId($test, 'incrementId')); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @expectedException As3\Modlr\Persister\PersisterException |
|
108
|
|
|
* @expectedExceptionMessage ID generation currently only supports an object strategy, or none at all. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function testGenerateIdIncrementId() |
|
111
|
|
|
{ |
|
112
|
|
|
$val = $this->persister->generateId('incrementId'); |
|
113
|
|
|
$this->assertGreaterThan($val, $this->persister->generateId('incrementId')); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.