MongoDeleteBatchTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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->assertInternalType('string', 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 testValidateItem()
  46. {
  47. $collection = $this->getCollection();
  48. $batch = new \MongoDeleteBatch($collection);
  49. $this->expectException(\Exception::class);
  50. $this->expectExceptionMessage("Expected \$item to contain 'q' key");
  51. $batch->add([]);
  52. }
  53. }