MongoDeleteBatchTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. 'nRemoved' => 1,
  16. 'ok' => true,
  17. ];
  18. $this->assertSame($expected, $batch->execute());
  19. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  20. $this->assertSame(1, $newCollection->count());
  21. }
  22. public function testDeleteMany()
  23. {
  24. $collection = $this->getCollection();
  25. $batch = new \MongoDeleteBatch($collection);
  26. $document = ['foo' => 'bar'];
  27. $collection->insert($document);
  28. unset($document['_id']);
  29. $collection->insert($document);
  30. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 0]));
  31. $expected = [
  32. 'nRemoved' => 2,
  33. 'ok' => true,
  34. ];
  35. $this->assertSame($expected, $batch->execute());
  36. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  37. $this->assertSame(0, $newCollection->count());
  38. }
  39. public function testValidateItem()
  40. {
  41. $collection = $this->getCollection();
  42. $batch = new \MongoDeleteBatch($collection);
  43. $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
  44. $batch->add([]);
  45. }
  46. }