MongoDeleteBatchTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $collection->insert(['foo' => 'bar']);
  10. $collection->insert(['foo' => 'bar']);
  11. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 1]));
  12. $expected = [
  13. 'ok' => 1.0,
  14. 'nInserted' => 0,
  15. 'nMatched' => 0,
  16. 'nModified' => 0,
  17. 'nUpserted' => 0,
  18. 'nRemoved' => 1,
  19. ];
  20. $this->assertSame($expected, $batch->execute());
  21. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  22. $this->assertSame(1, $newCollection->count());
  23. }
  24. public function testDeleteMany()
  25. {
  26. $collection = $this->getCollection();
  27. $batch = new \MongoDeleteBatch($collection);
  28. $collection->insert(['foo' => 'bar']);
  29. $collection->insert(['foo' => 'bar']);
  30. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 0]));
  31. $expected = [
  32. 'ok' => 1.0,
  33. 'nInserted' => 0,
  34. 'nMatched' => 0,
  35. 'nModified' => 0,
  36. 'nUpserted' => 0,
  37. 'nRemoved' => 2,
  38. ];
  39. $this->assertSame($expected, $batch->execute());
  40. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  41. $this->assertSame(0, $newCollection->count());
  42. }
  43. public function testValidateItem()
  44. {
  45. $collection = $this->getCollection();
  46. $batch = new \MongoDeleteBatch($collection);
  47. $this->setExpectedException('Exception', 'invalid item');
  48. $batch->add([]);
  49. }
  50. }