MongoInsertBatchTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. class MongoInsertBatchTest extends TestCase
  4. {
  5. public function testInsertBatch()
  6. {
  7. $batch = new \MongoInsertBatch($this->getCollection());
  8. $this->assertTrue($batch->add(['foo' => 'bar']));
  9. $this->assertTrue($batch->add(['bar' => 'foo']));
  10. $expected = [
  11. 'ok' => 1.0,
  12. 'nInserted' => 2,
  13. 'nMatched' => 0,
  14. 'nModified' => 0,
  15. 'nUpserted' => 0,
  16. 'nRemoved' => 0,
  17. ];
  18. $this->assertSame($expected, $batch->execute());
  19. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  20. $this->assertSame(2, $newCollection->count());
  21. $record = $newCollection->findOne();
  22. $this->assertNotNull($record);
  23. $this->assertObjectHasAttribute('foo', $record);
  24. $this->assertAttributeSame('bar', 'foo', $record);
  25. }
  26. public function testInsertBatchError()
  27. {
  28. $collection = $this->getCollection();
  29. $batch = new \MongoInsertBatch($collection);
  30. $collection->createIndex(['foo' => 1], ['unique' => true]);
  31. $this->assertTrue($batch->add(['foo' => 'bar']));
  32. $this->assertTrue($batch->add(['foo' => 'bar']));
  33. $expected = [
  34. 'ok' => 0.0,
  35. 'nInserted' => 1,
  36. 'nMatched' => 0,
  37. 'nModified' => 0,
  38. 'nUpserted' => 0,
  39. 'nRemoved' => 0,
  40. ];
  41. $this->assertSame($expected, $batch->execute());
  42. }
  43. }