1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace TestsDb\AnyDataset; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Dataset\IteratorFilter; |
6
|
|
|
use ByJG\AnyDataset\Enum\Relation; |
7
|
|
|
use ByJG\AnyDataset\Factory; |
8
|
|
|
use ByJG\AnyDataset\NoSqlDocument; |
9
|
|
|
use ByJG\AnyDataset\Store\MongoDbDriver; |
10
|
|
|
|
11
|
|
|
// backward compatibility |
12
|
|
|
if (!class_exists('\PHPUnit\Framework\TestCase')) { |
13
|
|
|
class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase'); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
class MongoDbDriverTest extends \PHPUnit\Framework\TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var MongoDbDriver |
20
|
|
|
*/ |
21
|
|
|
protected $dbDriver; |
22
|
|
|
|
23
|
|
|
const TEST_COLLECTION = 'collectionTest'; |
24
|
|
|
|
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->dbDriver = Factory::getNoSqlInstance('mongodb://mongodb-container/test'); |
28
|
|
|
|
29
|
|
|
$this->dbDriver->save( |
30
|
|
|
new NoSqlDocument( |
31
|
|
|
null, |
32
|
|
|
self::TEST_COLLECTION, |
33
|
|
|
['name' => 'Hilux', 'brand' => 'Toyota', 'price' => 120000] |
34
|
|
|
) |
35
|
|
|
); |
36
|
|
|
$this->dbDriver->save( |
37
|
|
|
new NoSqlDocument( |
38
|
|
|
null, |
39
|
|
|
self::TEST_COLLECTION, |
40
|
|
|
['name' => 'A3', 'brand' => 'Audi', 'price' => 90000] |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
$this->dbDriver->save( |
44
|
|
|
new NoSqlDocument( |
45
|
|
|
null, |
46
|
|
|
self::TEST_COLLECTION, |
47
|
|
|
['name' => 'Fox', 'brand' => 'Volkswagen', 'price' => 40000] |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
$this->dbDriver->save( |
51
|
|
|
new NoSqlDocument( |
52
|
|
|
null, |
53
|
|
|
self::TEST_COLLECTION, |
54
|
|
|
['name' => 'Corolla', 'brand' => 'Toyota', 'price' => 80000] |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
$this->dbDriver->save( |
58
|
|
|
new NoSqlDocument( |
59
|
|
|
null, |
60
|
|
|
self::TEST_COLLECTION, |
61
|
|
|
['name' => 'Cobalt', 'brand' => 'Chevrolet', 'price' => 60000] |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
$this->dbDriver->save( |
65
|
|
|
new NoSqlDocument( |
66
|
|
|
null, |
67
|
|
|
self::TEST_COLLECTION, |
68
|
|
|
['name' => 'Uno', 'brand' => 'Fiat', 'price' =>35000] |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function tearDown() |
74
|
|
|
{ |
75
|
|
|
$this->dbDriver->deleteDocuments(new IteratorFilter(), self::TEST_COLLECTION); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testSaveDocument() |
79
|
|
|
{ |
80
|
|
|
// Get the Object to test |
81
|
|
|
$filter = new IteratorFilter(); |
82
|
|
|
$filter->addRelation('name', Relation::EQUAL, 'Hilux'); |
83
|
|
|
$document = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
84
|
|
|
|
85
|
|
|
// Check if get ONe |
86
|
|
|
$this->assertEquals(1, count($document)); |
87
|
|
|
|
88
|
|
|
// Check if the default fields are here |
89
|
|
|
$data = $document[0]->getDocument(); |
90
|
|
|
$this->assertNotEmpty($data['_id']); |
91
|
|
|
$this->assertNotEmpty($data['created']); |
92
|
|
|
$this->assertNotEmpty($data['updated']); |
93
|
|
|
$this->assertEquals($data['created']->toDatetime(), $data['updated']->toDatetime()); |
94
|
|
|
unset($data['_id']); |
95
|
|
|
unset($data['created']); |
96
|
|
|
unset($data['updated']); |
97
|
|
|
|
98
|
|
|
// Check if the context is the expected |
99
|
|
|
$this->assertEquals( |
100
|
|
|
['name' => 'Hilux', 'brand' => 'Toyota', 'price' => 120000], |
101
|
|
|
$data |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
// Create a new document with a partial field to update |
105
|
|
|
$documentToUpdate = new NoSqlDocument( |
106
|
|
|
$document[0]->getIdDocument(), |
107
|
|
|
self::TEST_COLLECTION, |
108
|
|
|
[ 'price' => 150000 ] |
109
|
|
|
); |
110
|
|
|
sleep(1); // Just to force a new Update DateTime |
111
|
|
|
$documentSaved = $this->dbDriver->save($documentToUpdate); |
112
|
|
|
|
113
|
|
|
// Get the saved document |
114
|
|
|
$documentFromDb = $this->dbDriver->getDocumentById($document[0]->getIdDocument(), self::TEST_COLLECTION); |
115
|
|
|
|
116
|
|
|
// Check if the document have the same ID (Update) and Have the updated data |
117
|
|
|
$data = $documentFromDb->getDocument(); |
118
|
|
|
$this->assertEquals($documentSaved->getIdDocument(), $document[0]->getIdDocument()); |
119
|
|
|
$this->assertEquals($data['_id'], $document[0]->getIdDocument()); |
120
|
|
|
$this->assertNotEmpty($data['created']); |
121
|
|
|
$this->assertNotEmpty($data['updated']); |
122
|
|
|
$this->assertNotEquals($data['created']->toDatetime(), $data['updated']->toDatetime()); |
123
|
|
|
unset($data['_id']); |
124
|
|
|
unset($data['created']); |
125
|
|
|
unset($data['updated']); |
126
|
|
|
$this->assertEquals( |
127
|
|
|
['name' => 'Hilux', 'brand' => 'Toyota', 'price' => 150000], |
128
|
|
|
$data |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testDelete() |
133
|
|
|
{ |
134
|
|
|
// Get the Object to test |
135
|
|
|
$filter = new IteratorFilter(); |
136
|
|
|
$filter->addRelation('name', Relation::EQUAL, 'Uno'); |
137
|
|
|
$document = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
138
|
|
|
$this->assertEquals(1, count($document)); |
139
|
|
|
|
140
|
|
|
// Delete |
141
|
|
|
$this->dbDriver->deleteDocuments($filter, self::TEST_COLLECTION); |
142
|
|
|
|
143
|
|
|
// Check if object does not exists |
144
|
|
|
$document = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
145
|
|
|
$this->assertEmpty($document); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testGetDocuments() |
149
|
|
|
{ |
150
|
|
|
$filter = new IteratorFilter(); |
151
|
|
|
$filter->addRelation('price', Relation::LESS_OR_EQUAL_THAN, 40000); |
152
|
|
|
$documents = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
153
|
|
|
$this->assertEquals(2, count($documents)); |
154
|
|
|
$this->assertEquals('Fox', $documents[0]->getDocument()['name']); |
155
|
|
|
$this->assertEquals('Volkswagen', $documents[0]->getDocument()['brand']); |
156
|
|
|
$this->assertEquals('40000', $documents[0]->getDocument()['price']); |
157
|
|
|
$this->assertEquals('Uno', $documents[1]->getDocument()['name']); |
158
|
|
|
$this->assertEquals('Fiat', $documents[1]->getDocument()['brand']); |
159
|
|
|
$this->assertEquals('35000', $documents[1]->getDocument()['price']); |
160
|
|
|
|
161
|
|
|
$filter = new IteratorFilter(); |
162
|
|
|
$filter->addRelation('price', Relation::LESS_THAN, 40000); |
163
|
|
|
$documents = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
164
|
|
|
$this->assertEquals(1, count($documents)); |
165
|
|
|
$this->assertEquals('Uno', $documents[0]->getDocument()['name']); |
166
|
|
|
$this->assertEquals('Fiat', $documents[0]->getDocument()['brand']); |
167
|
|
|
$this->assertEquals('35000', $documents[0]->getDocument()['price']); |
168
|
|
|
|
169
|
|
|
$filter = new IteratorFilter(); |
170
|
|
|
$filter->addRelation('price', Relation::GREATER_OR_EQUAL_THAN, 90000); |
171
|
|
|
$documents = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
172
|
|
|
$this->assertEquals(2, count($documents)); |
173
|
|
|
$this->assertEquals('Hilux', $documents[0]->getDocument()['name']); |
174
|
|
|
$this->assertEquals('Toyota', $documents[0]->getDocument()['brand']); |
175
|
|
|
$this->assertEquals('120000', $documents[0]->getDocument()['price']); |
176
|
|
|
$this->assertEquals('A3', $documents[1]->getDocument()['name']); |
177
|
|
|
$this->assertEquals('Audi', $documents[1]->getDocument()['brand']); |
178
|
|
|
$this->assertEquals('90000', $documents[1]->getDocument()['price']); |
179
|
|
|
|
180
|
|
|
$filter = new IteratorFilter(); |
181
|
|
|
$filter->addRelation('price', Relation::GREATER_THAN, 90000); |
182
|
|
|
$documents = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
183
|
|
|
$this->assertEquals(1, count($documents)); |
184
|
|
|
$this->assertEquals('Hilux', $documents[0]->getDocument()['name']); |
185
|
|
|
$this->assertEquals('Toyota', $documents[0]->getDocument()['brand']); |
186
|
|
|
$this->assertEquals('120000', $documents[0]->getDocument()['price']); |
187
|
|
|
|
188
|
|
|
$filter = new IteratorFilter(); |
189
|
|
|
$filter->addRelation('name', Relation::STARTS_WITH, 'Co'); |
190
|
|
|
$documents = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
191
|
|
|
$this->assertEquals(2, count($documents)); |
192
|
|
|
$this->assertEquals('Corolla', $documents[0]->getDocument()['name']); |
193
|
|
|
$this->assertEquals('Toyota', $documents[0]->getDocument()['brand']); |
194
|
|
|
$this->assertEquals('80000', $documents[0]->getDocument()['price']); |
195
|
|
|
$this->assertEquals('Cobalt', $documents[1]->getDocument()['name']); |
196
|
|
|
$this->assertEquals('Chevrolet', $documents[1]->getDocument()['brand']); |
197
|
|
|
$this->assertEquals('60000', $documents[1]->getDocument()['price']); |
198
|
|
|
|
199
|
|
|
$filter = new IteratorFilter(); |
200
|
|
|
$filter->addRelation('name', Relation::CONTAINS, 'oba'); |
201
|
|
|
$documents = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION); |
202
|
|
|
$this->assertEquals(1, count($documents)); |
203
|
|
|
$this->assertEquals('Cobalt', $documents[0]->getDocument()['name']); |
204
|
|
|
$this->assertEquals('Chevrolet', $documents[0]->getDocument()['brand']); |
205
|
|
|
$this->assertEquals('60000', $documents[0]->getDocument()['price']); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.