MongoDeleteBatchTest.php 1.7 KB

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