From c457ecec71f0c7eea24373be199e71c56d520b55 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 2 Apr 2020 10:11:30 +0200 Subject: [PATCH] PHPLIB-540: Ensure that the WriteConcernError "errInfo" object is propagated --- tests/SpecTests/CrudSpecTest.php | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/SpecTests/CrudSpecTest.php b/tests/SpecTests/CrudSpecTest.php index a4a420c6a..8835b92e6 100644 --- a/tests/SpecTests/CrudSpecTest.php +++ b/tests/SpecTests/CrudSpecTest.php @@ -2,6 +2,8 @@ namespace MongoDB\Tests\SpecTests; +use MongoDB\Client; +use MongoDB\Driver\Exception\BulkWriteException; use stdClass; use function basename; use function file_get_contents; @@ -111,4 +113,44 @@ public function provideTests() return $testArgs; } + + /** + * Prose test 1: "errInfo" is propagated + */ + public function testErrInfoIsPropagated() + { + $runOn = [(object) ['minServerVersion' => '4.0.0']]; + $this->checkServerRequirements($runOn); + + $errInfo = (object) [ + 'writeConcern' => (object) [ + 'w' => 2, + 'wtimeout' => 0, + 'provenance' => 'clientSupplied', + ], + ]; + + $this->configureFailPoint([ + 'configureFailPoint' => 'failCommand', + 'mode' => ['times' => 1], + 'data' => [ + 'failCommands' => ['insert'], + 'writeConcernError' => [ + 'code' => 100, + 'codeName' => 'UnsatisfiableWriteConcern', + 'errmsg' => 'Not enough data-bearing nodes', + 'errInfo' => $errInfo, + ], + ], + ]); + + $client = new Client(static::getUri()); + + try { + $client->selectCollection($this->getDatabaseName(), $this->getCollectionName())->insertOne(['fail' => 1]); + $this->fail('Expected insert command to fail'); + } catch (BulkWriteException $e) { + self::assertEquals($errInfo, $e->getWriteResult()->getWriteConcernError()->getInfo()); + } + } }