Skip to content

Commit 6ff6ffd

Browse files
authored
Merge pull request jasny#49 from Minstel/Change_Document_BasicImplementation_to_use_MongoDB_ext
Change document basic implementation to use mongo db ext
2 parents 4e6fc86 + d6c7ad7 commit 6ff6ffd

10 files changed

+451
-19
lines changed

src/Collection.php

+48
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,54 @@ public function save($doc, array $options = [])
173173
return $this->insertOne($doc, $options);
174174
}
175175

176+
/**
177+
* Use result id for processed document
178+
*
179+
* @param array|object $doc
180+
* @param string $idName Name of document id property
181+
* @param object $queryResult
182+
*/
183+
public function useResultId(&$doc, $idName, $queryResult)
184+
{
185+
$id = null;
186+
187+
if ($queryResult instanceof \MongoDB\InsertManyResult) {
188+
$id = $queryResult->getInsertedIds();
189+
} else if ($queryResult instanceof \MongoDB\UpdateResult) {
190+
$id = $queryResult->getUpsertedId();
191+
} else if ($queryResult instanceof \MongoDB\InsertOneResult) {
192+
$id = $queryResult->getInsertedId();
193+
}
194+
195+
if (is_array($id)) {
196+
foreach ($id as $i => $oneId) {
197+
$this->setDocId($doc[$i], $idName, $oneId);
198+
}
199+
} else {
200+
$this->setDocId($doc, $idName, $id);
201+
}
202+
}
203+
204+
/**
205+
* Update the identifier
206+
*
207+
* @param array|object $doc
208+
* @param string $idName Name of document id property
209+
* @param MongoDB\BSON\ObjectId $id
210+
*/
211+
protected function setDocId(&$doc, $idName, $id)
212+
{
213+
if (!$id) {
214+
return;
215+
}
216+
217+
if (is_array($doc)) {
218+
$doc[$idName] = $id;
219+
} else {
220+
$doc->$idName = $id;
221+
}
222+
}
223+
176224
/**
177225
* Create filter for replacing document
178226
*

src/Document/BasicImplementation.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ public function toData()
101101
* Convert child to an id
102102
*
103103
* @param Entity $item
104-
* @return \MongoId|mixed
104+
* @return \MongoDB\BSON\ObjectId|mixed
105105
*/
106106
protected function childEntityToId(Entity $item)
107107
{
108108
if (
109109
$item instanceof Meta\Introspection &&
110110
is_scalar($item::getIdProperty()) &&
111-
$item::meta()->ofProperty($item::getIdProperty())['dbFieldType'] === '\MongoId'
111+
$item::meta()->ofProperty($item::getIdProperty())['dbFieldType'] === '\\MongoDB\\BSON\\ObjectId'
112112
) {
113-
$id = new \MongoId($item->getId());
113+
$id = new \MongoDB\BSON\ObjectId($item->getId());
114114
} else {
115115
$id = $item->getId();
116116
}
@@ -132,15 +132,17 @@ public function save(array $opts = [])
132132
}
133133

134134
$data = $this->toData();
135+
$collection = static::getCollection();
135136

136137
if ($this instanceof ChangeAware && $this->isNew()) {
137-
static::getCollection()->insert($data);
138+
$result = $collection->insertOne($data, $opts);
138139
} else {
139-
static::getCollection()->save($data);
140+
$result = $collection->save($data, $opts);
140141
}
141142

142-
$idProp = static::getIdProperty();
143-
$this->$idProp = $data['_id'];
143+
$idName = static::getIdProperty();
144+
$collection->useResultId($this, $idName, $result);
145+
144146
$this->cast();
145147

146148
return $this;
@@ -149,7 +151,6 @@ public function save(array $opts = [])
149151
/**
150152
* Delete the document
151153
*
152-
* @codeCoverageIgnore
153154
* @param array $opts
154155
* @return $this
155156
*/
@@ -159,14 +160,13 @@ public function delete(array $opts = [])
159160
$filter = static::castForDB($properties);
160161
$query = static::mapToFields($filter);
161162

162-
static::getCollection()->remove($query);
163+
static::getCollection()->deleteOne($query, $opts);
163164
return $this;
164165
}
165166

166167
/**
167168
* Reload the entity from the DB
168169
*
169-
* @codeCoverageIgnore
170170
* @param array $opts
171171
* @return $this|false
172172
*/
@@ -190,7 +190,6 @@ public function reload(array $opts = [])
190190
/**
191191
* Check no other document with the same value of the property exists
192192
*
193-
* @codeCoverageIgnore
194193
* @param string $property
195194
* @param array|string $group List of properties that should match
196195
* @param array $opts
@@ -226,7 +225,7 @@ public function jsonSerialize()
226225
foreach ($values as &$value) {
227226
if ($value instanceof \DateTime) {
228227
$value = $value->format(\DateTime::ISO8601);
229-
} elseif ($value instanceof \MongoId) {
228+
} elseif ($value instanceof \MongoDB\BSON\ObjectId) {
230229
$value = (string)$value;
231230
}
232231
}

tests/CollectionTest.php

+97
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,101 @@ public function testFindOneNoCast($values, $documentClass)
513513
$result = $collection->findOne($filter, $options);
514514
$this->assertSame($values, $result);
515515
}
516+
517+
/**
518+
* Provide data for testing 'useResultId' method, if single id is returned
519+
*
520+
* @return array
521+
*/
522+
public function useResultIdSingleProvider()
523+
{
524+
return [
525+
[['foo' => 'bar'], $this->createMock(\MongoDB\InsertOneResult::class), 'getInsertedId'],
526+
[['foo' => 'bar'], $this->createMock(\MongoDB\UpdateResult::class), 'getUpsertedId'],
527+
[(object)['foo' => 'bar'], $this->createMock(\MongoDB\InsertOneResult::class), 'getInsertedId'],
528+
[(object)['foo' => 'bar'], $this->createMock(\MongoDB\UpdateResult::class), 'getUpsertedId'],
529+
];
530+
}
531+
532+
/**
533+
* Test 'useResultId' method, if single id is returned
534+
*
535+
* @dataProvider useResultIdSingleProvider
536+
*/
537+
public function testUseResultIdSingle($document, $queryResult, $method)
538+
{
539+
$collection = $this->createPartialMock(Collection::class, []);
540+
$queryResult->expects($this->once())->method($method)->willReturn('a');
541+
542+
$collection->useResultId($document, '_idCustom', $queryResult);
543+
544+
$document = (array)$document;
545+
$this->assertSame('a', $document['_idCustom']);
546+
}
547+
548+
/**
549+
* Provide data for testing 'useResultId' method, if multiple ids are returned
550+
*
551+
* @return array
552+
*/
553+
public function useResultIdMultipleProvider()
554+
{
555+
return [
556+
[[['foo' => 'bar'], ['zoo' => 'baz']], $this->createMock(\MongoDB\InsertManyResult::class), 'getInsertedIds'],
557+
[[['foo' => 'bar'], ['zoo' => 'baz']], $this->createMock(\MongoDB\UpdateResult::class), 'getUpsertedId'],
558+
[[(object)['foo' => 'bar'], (object)['zoo' => 'baz']], $this->createMock(\MongoDB\InsertManyResult::class), 'getInsertedIds'],
559+
[[(object)['foo' => 'bar'], (object)['zoo' => 'baz']], $this->createMock(\MongoDB\UpdateResult::class), 'getUpsertedId'],
560+
];
561+
}
562+
563+
/**
564+
* Test 'useResultId' method, if multiple ids are returned
565+
*
566+
* @dataProvider useResultIdMultipleProvider
567+
*/
568+
public function testUseResultIdMultiple($documents, $queryResult, $method)
569+
{
570+
$collection = $this->createPartialMock(Collection::class, []);
571+
$queryResult->expects($this->once())->method($method)->willReturn(['a', 'b']);
572+
573+
$collection->useResultId($documents, '_idCustom', $queryResult);
574+
575+
$documents[0] = (array)$documents[0];
576+
$documents[1] = (array)$documents[1];
577+
578+
$this->assertSame('a', $documents[0]['_idCustom']);
579+
$this->assertSame('b', $documents[1]['_idCustom']);
580+
}
581+
582+
/**
583+
* Provide data for testing 'useResultId' method, if no id is returned
584+
*
585+
* @return array
586+
*/
587+
public function useResultIdEmptyProvider()
588+
{
589+
return [
590+
[null],
591+
[[]]
592+
];
593+
}
594+
595+
/**
596+
* Test 'useResultId' method, if no id is returned
597+
*
598+
* @dataProvider useResultIdEmptyProvider
599+
*/
600+
public function testUseResultIdEmpty($id)
601+
{
602+
$document = ['foo' => 'bar'];
603+
604+
$queryResult = $this->createMock(\MongoDB\UpdateResult::class);
605+
$queryResult->expects($this->once())->method('getUpsertedId')->willReturn($id);
606+
607+
$collection = $this->createPartialMock(Collection::class, []);
608+
609+
$collection->useResultId($document, '_idCustom', $queryResult);
610+
611+
$this->assertEquals(['foo' => 'bar'], $document);
612+
}
516613
}

tests/DBTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testCreateClientFromOptions($options, $expectedUri)
4545
$result = $this->callProtectedMethod($mock, 'createClientFromOptions', [$options]);
4646
$this->assertInstanceOf(Client::class, $result);
4747

48-
$uri = $this->getPrivatePropery($result, 'uri');
48+
$uri = $this->getPrivateProperty($result, 'uri');
4949
$this->assertSame($expectedUri, $uri);
5050
}
5151

0 commit comments

Comments
 (0)