Skip to content

Commit 5523028

Browse files
Merge pull request #46 from kevinkl3/fix/delete-one
deleteOne must return a DeleteResult
2 parents 7fbf641 + d2e041b commit 5523028

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/MockCollection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,18 @@ public function createIndexes(array $indexes, array $options = [])
374374
public function deleteOne($filter, array $options = [])
375375
{
376376
$matcher = $this->matcherFromQuery($filter);
377+
$count = 0;
378+
$deletedIds = [];
377379
foreach ($this->documents as $i => $doc) {
378380
if ($matcher($doc)) {
381+
$deletedIds []= $doc['_id'];
379382
unset($this->documents[$i]);
380383
$this->documents = array_values($this->documents);
381-
return;
384+
$count++;
385+
break;
382386
}
383387
}
388+
return new MockDeleteResult($count, $count, $deletedIds);
384389
}
385390

386391
public function distinct($fieldName, $filter = [], array $options = [])

tests/MockCollectionTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,15 @@ public function testDeleteOneDeletesJustOneObject()
311311
['foo' => 'bar', 'bar' => 1],
312312
['foo' => 'baz', 'bar' => 2],
313313
]);
314-
$this->col->deleteOne(['bar' => 1]);
314+
315+
$deleteResult = $this->col->deleteOne(['bar' => 1]);
315316

316317
self::assertThat($this->col->count(['bar' => 1]), self::equalTo(1));
317318
self::assertThat($this->col->count(['bar' => 2]), self::equalTo(1));
319+
320+
self::assertInstanceOf(\Helmich\MongoMock\MockDeleteResult::class, $deleteResult);
321+
322+
self::assertEquals(1,$deleteResult->getDeletedCount());
318323
}
319324

320325
/**

0 commit comments

Comments
 (0)