Skip to content

Commit b9db7e3

Browse files
committed
Add tests for insertMany ignoreDuplicates option
- testInsertManyIgnoreDuplicates: mixed batch with existing + new docs - testInsertManyIgnoreIntraBatchDuplicates: same ID twice, first wins - testInsertManyIgnoreAllDuplicates: all duplicates, zero inserts
1 parent 08c94f4 commit b9db7e3

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

tests/MongoTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,101 @@ public function testToArrayNestedConversion()
389389
self::assertEquals([42], $client->toArray(42));
390390
}
391391

392+
public function testInsertManyIgnoreDuplicates()
393+
{
394+
$collectionName = 'ignore_duplicates';
395+
$this->getDatabase()->createCollection($collectionName);
396+
try {
397+
// Seed two documents
398+
$this->getDatabase()->insertMany($collectionName, [
399+
['_id' => 'doc1', 'name' => 'Original A'],
400+
['_id' => 'doc2', 'name' => 'Original B'],
401+
]);
402+
403+
// Without ignoreDuplicates, inserting a duplicate throws
404+
try {
405+
$this->getDatabase()->insertMany($collectionName, [
406+
['_id' => 'doc1', 'name' => 'Duplicate A'],
407+
]);
408+
self::fail('Expected duplicate key exception');
409+
} catch (Exception $e) {
410+
self::assertTrue($e->isDuplicateKeyError());
411+
}
412+
413+
// With ignoreDuplicates, duplicate is skipped and new doc is inserted
414+
$result = $this->getDatabase()->insertMany($collectionName, [
415+
['_id' => 'doc1', 'name' => 'Duplicate A'],
416+
['_id' => 'doc3', 'name' => 'New C'],
417+
], ['ignoreDuplicates' => true]);
418+
419+
self::assertCount(1, $result);
420+
self::assertEquals('doc3', $result[0]['_id']);
421+
422+
// Original doc1 unchanged
423+
$docs = $this->getDatabase()->find($collectionName, ['_id' => 'doc1'])->cursor->firstBatch ?? [];
424+
self::assertCount(1, $docs);
425+
self::assertEquals('Original A', $docs[0]->name);
426+
427+
// Total should be 3
428+
$total = $this->getDatabase()->count($collectionName);
429+
self::assertEquals(3, $total);
430+
} finally {
431+
$this->getDatabase()->dropCollection($collectionName);
432+
}
433+
}
434+
435+
public function testInsertManyIgnoreIntraBatchDuplicates()
436+
{
437+
$collectionName = 'ignore_intra_batch';
438+
$this->getDatabase()->createCollection($collectionName);
439+
try {
440+
// Same ID twice in one batch — first wins
441+
$result = $this->getDatabase()->insertMany($collectionName, [
442+
['_id' => 'dup', 'name' => 'First'],
443+
['_id' => 'dup', 'name' => 'Second'],
444+
['_id' => 'unique1', 'name' => 'Unique'],
445+
], ['ignoreDuplicates' => true]);
446+
447+
self::assertCount(2, $result);
448+
449+
$doc = $this->getDatabase()->find($collectionName, ['_id' => 'dup'])->cursor->firstBatch ?? [];
450+
self::assertCount(1, $doc);
451+
self::assertEquals('First', $doc[0]->name);
452+
453+
$total = $this->getDatabase()->count($collectionName);
454+
self::assertEquals(2, $total);
455+
} finally {
456+
$this->getDatabase()->dropCollection($collectionName);
457+
}
458+
}
459+
460+
public function testInsertManyIgnoreAllDuplicates()
461+
{
462+
$collectionName = 'ignore_all_dups';
463+
$this->getDatabase()->createCollection($collectionName);
464+
try {
465+
// Seed one document
466+
$this->getDatabase()->insert($collectionName, ['_id' => 'existing', 'name' => 'Original']);
467+
468+
// Insert only duplicates with ignoreDuplicates
469+
$result = $this->getDatabase()->insertMany($collectionName, [
470+
['_id' => 'existing', 'name' => 'Duplicate'],
471+
], ['ignoreDuplicates' => true]);
472+
473+
self::assertCount(0, $result);
474+
475+
// Original unchanged
476+
$docs = $this->getDatabase()->find($collectionName, ['_id' => 'existing'])->cursor->firstBatch ?? [];
477+
self::assertEquals('Original', $docs[0]->name);
478+
479+
// Still only 1 document
480+
$total = $this->getDatabase()->count($collectionName);
481+
self::assertEquals(1, $total);
482+
} finally {
483+
$this->getDatabase()->dropCollection($collectionName);
484+
}
485+
}
486+
392487
public function testCountMethod()
393488
{
394489
$collectionName = 'count_test';

0 commit comments

Comments
 (0)