MongoDeleteBatchTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  4. class MongoDeleteBatchTest extends TestCase
  5. {
  6. public function testSerialize()
  7. {
  8. $batch = new \MongoDeleteBatch($this->getCollection());
  9. $this->assertIsString(serialize($batch));
  10. }
  11. public function testDeleteOne()
  12. {
  13. $collection = $this->getCollection();
  14. $batch = new \MongoDeleteBatch($collection);
  15. $document = ['foo' => 'bar'];
  16. $collection->insert($document);
  17. unset($document['_id']);
  18. $collection->insert($document);
  19. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 1]));
  20. $expected = [
  21. 'nRemoved' => 1,
  22. 'ok' => true,
  23. ];
  24. $this->assertSame($expected, $batch->execute());
  25. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  26. $this->assertSame(1, $newCollection->count());
  27. }
  28. public function testDeleteMany()
  29. {
  30. $collection = $this->getCollection();
  31. $batch = new \MongoDeleteBatch($collection);
  32. $document = ['foo' => 'bar'];
  33. $collection->insert($document);
  34. unset($document['_id']);
  35. $collection->insert($document);
  36. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 0]));
  37. $expected = [
  38. 'nRemoved' => 2,
  39. 'ok' => true,
  40. ];
  41. $this->assertSame($expected, $batch->execute());
  42. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  43. $this->assertSame(0, $newCollection->count());
  44. }
  45. public function testDeleteManyWithoutAck()
  46. {
  47. $collection = $this->getCollection();
  48. $batch = new \MongoDeleteBatch($collection);
  49. $document = ['foo' => 'bar'];
  50. $collection->insert($document);
  51. unset($document['_id']);
  52. $collection->insert($document);
  53. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 0]));
  54. $expected = [
  55. 'nRemoved' => 0,
  56. 'ok' => true,
  57. ];
  58. $this->assertSame($expected, $batch->execute(['w' => 0]));
  59. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  60. $this->assertSame(0, $newCollection->count());
  61. }
  62. public function testValidateItem()
  63. {
  64. $collection = $this->getCollection();
  65. $batch = new \MongoDeleteBatch($collection);
  66. $this->expectException(\Exception::class);
  67. $this->expectExceptionMessage("Expected \$item to contain 'q' key");
  68. $batch->add([]);
  69. }
  70. }