MongoDeleteBatchTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. class MongoDeleteBatchTest extends TestCase
  4. {
  5. public function testDeleteOne()
  6. {
  7. $collection = $this->getCollection();
  8. $batch = new \MongoDeleteBatch($collection);
  9. $document = ['foo' => 'bar'];
  10. $collection->insert($document);
  11. unset($document['_id']);
  12. $collection->insert($document);
  13. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 1]));
  14. $expected = [
  15. 'ok' => 1.0,
  16. 'nInserted' => 0,
  17. 'nMatched' => 0,
  18. 'nModified' => 0,
  19. 'nUpserted' => 0,
  20. 'nRemoved' => 1,
  21. ];
  22. $this->assertSame($expected, $batch->execute());
  23. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  24. $this->assertSame(1, $newCollection->count());
  25. }
  26. public function testDeleteMany()
  27. {
  28. $collection = $this->getCollection();
  29. $batch = new \MongoDeleteBatch($collection);
  30. $document = ['foo' => 'bar'];
  31. $collection->insert($document);
  32. unset($document['_id']);
  33. $collection->insert($document);
  34. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 0]));
  35. $expected = [
  36. 'ok' => 1.0,
  37. 'nInserted' => 0,
  38. 'nMatched' => 0,
  39. 'nModified' => 0,
  40. 'nUpserted' => 0,
  41. 'nRemoved' => 2,
  42. ];
  43. $this->assertSame($expected, $batch->execute());
  44. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  45. $this->assertSame(0, $newCollection->count());
  46. }
  47. public function testValidateItem()
  48. {
  49. $collection = $this->getCollection();
  50. $batch = new \MongoDeleteBatch($collection);
  51. $this->setExpectedException('Exception', 'invalid item');
  52. $batch->add([]);
  53. }
  54. }