|
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
|
|
|
|
|
35
|
|
|
$formatter = new Modlr\Persister\MongoDb\Formatter(); |
|
36
|
|
|
$query = new Modlr\Persister\MongoDb\Query($this->connection, $formatter); |
|
37
|
|
|
|
|
38
|
|
|
$hydrator = new Modlr\Persister\MongoDb\Hydrator(); |
|
39
|
|
|
$schemaManager = new Modlr\Persister\MongoDb\SchemaManager(); |
|
40
|
|
|
|
|
41
|
|
|
$this->persister = new Modlr\Persister\MongoDb\Persister($query, $this->smf, $hydrator, $schemaManager); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function tearDown() |
|
45
|
|
|
{ |
|
46
|
|
|
$collections = $this->connection->selectDatabase(self::$dbName)->listCollections(); |
|
47
|
|
|
foreach ($collections as $collection) { |
|
48
|
|
|
$collection->drop(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->connection->close(); |
|
52
|
|
|
unset($this->connection); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testPersisterValues() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->assertEquals('mongodb', $this->persister->getPersisterKey()); |
|
58
|
|
|
$this->assertEquals('_type', $this->persister->getPolymorphicKey()); |
|
59
|
|
|
$this->assertEquals('_id', $this->persister->getIdentifierKey()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testMDFInstance() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->assertInstanceOf( |
|
65
|
|
|
'As3\Modlr\Metadata\Interfaces\StorageMetadataFactoryInterface', |
|
66
|
|
|
$this->persister->getPersistenceMetadataFactory() |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @expectedException As3\Modlr\Persister\PersisterException |
|
72
|
|
|
* @expectedExceptionMessage ID conversion currently only supports an object strategy, or none at all. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testConvertIdInvalidStrategy() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->assertInstanceOf('MongoId', $this->persister->convertId('test', 'blag')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @expectedException As3\Modlr\Persister\PersisterException |
|
81
|
|
|
* @expectedExceptionMessage ID generation currently only supports an object strategy, or none at all. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function testGenerateIdInvalidStrategy() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->assertInstanceOf('MongoId', $this->persister->generateId('blag')); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testConvertIdObject() |
|
89
|
|
|
{ |
|
90
|
|
|
$test = '49a7011a05c677b9a916612a'; |
|
91
|
|
|
$id = new \MongoId($test); |
|
92
|
|
|
|
|
93
|
|
|
$this->assertEquals($id, $this->persister->convertId($id)); |
|
94
|
|
|
$this->assertEquals($id, $this->persister->convertId($id, 'object')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testGenerateIdObject() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->assertInstanceOf('MongoId', $this->persister->generateId()); |
|
100
|
|
|
$this->assertInstanceOf('MongoId', $this->persister->generateId('object')); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @expectedException As3\Modlr\Persister\PersisterException |
|
105
|
|
|
* @expectedExceptionMessage ID conversion currently only supports an object strategy, or none at all. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testConvertIdIncrementId() |
|
108
|
|
|
{ |
|
109
|
|
|
$test = 12345; |
|
110
|
|
|
$this->assertEquals($test, $this->persister->convertId($test, 'incrementId')); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @expectedException As3\Modlr\Persister\PersisterException |
|
115
|
|
|
* @expectedExceptionMessage ID generation currently only supports an object strategy, or none at all. |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testGenerateIdIncrementId() |
|
118
|
|
|
{ |
|
119
|
|
|
$val = $this->persister->generateId('incrementId'); |
|
120
|
|
|
$this->assertGreaterThan($val, $this->persister->generateId('incrementId')); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testNameIsAppendedToSchemata() |
|
124
|
|
|
{ |
|
125
|
|
|
$metadata = $this->getMetadata()->persistence; |
|
126
|
|
|
|
|
127
|
|
|
foreach ($metadata->schemata as $schema) { |
|
|
|
|
|
|
128
|
|
|
$this->assertTrue(isset($schema['options']['name']) && !empty($schema['options']['name']), 'index name was not applied to schema'); |
|
129
|
|
|
$this->assertSame(stripos($schema['options']['name'], 'modlr_'), 0, '`modlr_` prefix missing from schema name'); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @expectedException As3\Modlr\Exception\MetadataException |
|
135
|
|
|
* @expectedExceptionMessage At least one key must be specified to define an index. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function testSchemaRequiresKeys() |
|
138
|
|
|
{ |
|
139
|
|
|
$schemata = [['options' => ['unique' => true]]]; |
|
140
|
|
|
$metadata = $this->getMetadata($schemata); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @expectedException As3\Modlr\Exception\MetadataException |
|
145
|
|
|
* @expectedExceptionMessage At least one key must be specified to define an index. |
|
146
|
|
|
*/ |
|
147
|
|
|
public function testSchemaRequiresAtLeastOneKey() |
|
148
|
|
|
{ |
|
149
|
|
|
$schemata = ['keys' => [], ['options' => ['unique' => true]]]; |
|
150
|
|
|
$metadata = $this->getMetadata($schemata); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @expectedException InvalidArgumentException |
|
155
|
|
|
* @expectedExceptionMessage Cannot create an index with no keys defined. |
|
156
|
|
|
*/ |
|
157
|
|
View Code Duplication |
public function testSchemaCreateRequiresKeys() |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$collection = $this->connection->selectCollection(self::$dbName, 'test-model'); |
|
160
|
|
|
$manager = new Modlr\Persister\MongoDb\SchemaManager; |
|
161
|
|
|
|
|
162
|
|
|
$schemata = [ |
|
163
|
|
|
['options' => ['unique' => true]] |
|
164
|
|
|
]; |
|
165
|
|
|
|
|
166
|
|
|
$manager->createSchemata($collection, $schemata); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @expectedException InvalidArgumentException |
|
171
|
|
|
* @expectedExceptionMessage Cannot create an index with no keys defined. |
|
172
|
|
|
*/ |
|
173
|
|
View Code Duplication |
public function testSchemaCreateRequiresAtLeastOneKey() |
|
|
|
|
|
|
174
|
|
|
{ |
|
175
|
|
|
$collection = $this->connection->selectCollection(self::$dbName, 'test-model'); |
|
176
|
|
|
$manager = new Modlr\Persister\MongoDb\SchemaManager; |
|
177
|
|
|
|
|
178
|
|
|
$schemata = [ |
|
179
|
|
|
['keys' => [], 'options' => ['unique' => true]] |
|
180
|
|
|
]; |
|
181
|
|
|
|
|
182
|
|
|
$manager->createSchemata($collection, $schemata); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function testSchemaCreation() |
|
186
|
|
|
{ |
|
187
|
|
|
$metadata = $this->getMetadata(); |
|
188
|
|
|
$this->persister->createSchemata($metadata); |
|
189
|
|
|
|
|
190
|
|
|
$collection = $this->connection->selectCollection(self::$dbName, 'test-model'); |
|
191
|
|
|
$indices = $collection->getIndexInfo(); |
|
192
|
|
|
|
|
193
|
|
|
foreach ($metadata->persistence->schemata as $schema) { |
|
|
|
|
|
|
194
|
|
|
$found = false; |
|
195
|
|
|
foreach ($indices as $index) { |
|
196
|
|
|
if ($index['name'] === $schema['options']['name']) { |
|
197
|
|
|
$found = true; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
$this->assertTrue($found, sprintf('Index for "%s" was not created!', $schema['options']['name'])); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
private function getMetadata(array $schemata = []) |
|
205
|
|
|
{ |
|
206
|
|
|
$mapping = [ |
|
207
|
|
|
'type' => 'test-model', |
|
208
|
|
|
'attributes' => [ |
|
209
|
|
|
'name' => ['data_type' => 'string'], |
|
210
|
|
|
'active' => ['data_type' => 'boolean'] |
|
211
|
|
|
], |
|
212
|
|
|
'persistence' => [ |
|
213
|
|
|
'db' => self::$dbName, |
|
214
|
|
|
'collection' => 'test-model', |
|
215
|
|
|
'schemata' => [ |
|
216
|
|
|
['keys' => ['name' => 1]], |
|
217
|
|
|
['keys' => ['active' => 1], ['options' => ['unique' => true]]] |
|
218
|
|
|
] |
|
219
|
|
|
] |
|
220
|
|
|
]; |
|
221
|
|
|
if (!empty($schemata)) { |
|
222
|
|
|
$mapping['persistence']['schemata'] = $schemata; |
|
223
|
|
|
} |
|
224
|
|
|
$metadata = new Modlr\Metadata\EntityMetadata($mapping['type']); |
|
225
|
|
|
$pmd = $this->smf->createInstance($mapping['persistence']); |
|
226
|
|
|
$metadata->setPersistence($pmd); |
|
227
|
|
|
$this->smf->handleValidate($metadata); |
|
228
|
|
|
return $metadata; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: