Parcourir la source

Harden tests to expect exception

Andreas Braun il y a 9 ans
Parent
commit
ebaff33b96

+ 0 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDeleteBatchTest.php

@@ -58,7 +58,6 @@ class MongoDeleteBatchTest extends TestCase
         $this->assertSame(0, $newCollection->count());
     }
 
-
     public function testValidateItem()
     {
         $collection = $this->getCollection();

+ 2 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoInsertBatchTest.php

@@ -56,8 +56,10 @@ class MongoInsertBatchTest extends TestCase
 
         try {
             $batch->execute();
+            $this->fail('Expected MongoWriteConcernException');
         } catch (\MongoWriteConcernException $e) {
             $this->assertSame('Failed write', $e->getMessage());
+            $this->assertSame(911, $e->getCode());
             $this->assertArraySubset($expected, $e->getDocument());
         }
     }

+ 72 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoUpdateBatchTest.php

@@ -39,6 +39,42 @@ class MongoUpdateBatchTest extends TestCase
         $this->assertAttributeSame('foo', 'foo', $record);
     }
 
+    public function testUpdateOneException()
+    {
+        $collection = $this->getCollection();
+        $batch = new \MongoUpdateBatch($collection);
+
+        $document = ['foo' => 'bar'];
+        $collection->insert($document);
+        $document = ['foo' => 'foo'];
+        $collection->insert($document);
+        $collection->createIndex(['foo' => 1], ['unique' => true]);
+
+        $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']]]));
+
+        $expected = [
+            'writeErrors' => [
+                [
+                    'index' => 0,
+                    'code' => 11000,
+                ]
+            ],
+            'nMatched' => 0,
+            'nModified' => 0,
+            'nUpserted' => 0,
+            'ok' => true,
+        ];
+
+        try {
+            $batch->execute();
+            $this->fail('Expected MongoWriteConcernException');
+        } catch (\MongoWriteConcernException $e) {
+            $this->assertSame('Failed write', $e->getMessage());
+            $this->assertSame(911, $e->getCode());
+            $this->assertArraySubset($expected, $e->getDocument());
+        }
+    }
+
     public function testUpdateMany()
     {
         $collection = $this->getCollection();
@@ -49,7 +85,6 @@ class MongoUpdateBatchTest extends TestCase
         unset($document['_id']);
         $collection->insert($document);
 
-
         $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]));
 
         $expected = [
@@ -69,6 +104,42 @@ class MongoUpdateBatchTest extends TestCase
         $this->assertAttributeSame('foo', 'foo', $record);
     }
 
+    public function testUpdateManyException()
+    {
+        $collection = $this->getCollection();
+        $batch = new \MongoUpdateBatch($collection);
+
+        $document = ['foo' => 'bar', 'bar' => 'bar'];
+        $collection->insert($document);
+        $document = ['foo' => 'foobar', 'bar' => 'bar'];
+        $collection->insert($document);
+        $collection->createIndex(['foo' => 1], ['unique' => true]);
+
+        $batch->add(['q' => ['bar' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]);
+
+        $expected = [
+            'writeErrors' => [
+                [
+                    'index' => 0,
+                    'code' => 11000,
+                ]
+            ],
+            'nMatched' => 0,
+            'nModified' => 0,
+            'nUpserted' => 0,
+            'ok' => true,
+        ];
+
+        try {
+            $batch->execute();
+            $this->fail('Expected MongoWriteConcernException');
+        } catch (\MongoWriteConcernException $e) {
+            $this->assertSame('Failed write', $e->getMessage());
+            $this->assertSame(911, $e->getCode());
+            $this->assertArraySubset($expected, $e->getDocument());
+        }
+    }
+
     public function testUpsert()
     {
         $document = ['foo' => 'foo'];