|
| 1 | +--TEST-- |
| 2 | +MongoDB\Driver\WriteError::getInfo() exposes writeError.errInfo |
| 3 | +--DESCRIPTION-- |
| 4 | +CRUD spec prose test #2 |
| 5 | +https://github.com/mongodb/specifications/blob/master/source/crud/tests/README.rst#writeerror-details-exposes-writeerrors-errinfo |
| 6 | +--SKIPIF-- |
| 7 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> |
| 8 | +<?php skip_if_server_version('<', '5.0'); ?> |
| 9 | +<?php skip_if_not_clean(); ?> |
| 10 | +--FILE-- |
| 11 | +<?php |
| 12 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 13 | + |
| 14 | +class Test implements MongoDB\Driver\Monitoring\CommandSubscriber |
| 15 | +{ |
| 16 | + private $errInfo; |
| 17 | + |
| 18 | + public function execute() |
| 19 | + { |
| 20 | + $manager = create_test_manager(); |
| 21 | + |
| 22 | + $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command([ |
| 23 | + 'create' => COLLECTION_NAME, |
| 24 | + 'validator' => ['x' => ['$type' => 'string']], |
| 25 | + ])); |
| 26 | + |
| 27 | + $bulk = new MongoDB\Driver\BulkWrite; |
| 28 | + $bulk->insert(['x' => 1]); |
| 29 | + |
| 30 | + MongoDB\Driver\Monitoring\addSubscriber($this); |
| 31 | + |
| 32 | + try { |
| 33 | + $manager->executeBulkWrite(NS, $bulk); |
| 34 | + } catch (MongoDB\Driver\Exception\BulkWriteException $e) { |
| 35 | + $writeError = $e->getWriteResult()->getWriteErrors()[0]; |
| 36 | + |
| 37 | + var_dump($writeError->getCode()); // DocumentValidationFailure(121) |
| 38 | + |
| 39 | + /* Note: we intentionally do not assert the contents of errInfo |
| 40 | + * since its structure could change between server versions. */ |
| 41 | + var_dump($writeError->getInfo() instanceof stdClass); |
| 42 | + var_dump($this->errInfo instanceof stdClass); |
| 43 | + var_dump($writeError->getInfo() == $this->errInfo); |
| 44 | + } |
| 45 | + |
| 46 | + MongoDB\Driver\Monitoring\removeSubscriber($this); |
| 47 | + } |
| 48 | + |
| 49 | + public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event) |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event) |
| 54 | + { |
| 55 | + if ($event->getCommandName() === 'insert') { |
| 56 | + $this->errInfo = $event->getReply()->writeErrors[0]->errInfo ?? null; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event) |
| 61 | + { |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +(new Test)->execute(); |
| 66 | + |
| 67 | +?> |
| 68 | +===DONE=== |
| 69 | +--EXPECTF-- |
| 70 | +int(121) |
| 71 | +bool(true) |
| 72 | +bool(true) |
| 73 | +bool(true) |
| 74 | +===DONE=== |
0 commit comments