Skip to content

Commit b114c16

Browse files
committed
PHPLIB-909: expectedError.errorResponse assertion
1 parent 356b89c commit b114c16

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

tests/UnifiedSpecTests/ExpectedError.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Driver\Exception\ExecutionTimeoutException;
88
use MongoDB\Driver\Exception\RuntimeException;
99
use MongoDB\Driver\Exception\ServerException;
10+
use MongoDB\Tests\UnifiedSpecTests\Constraint\Matches;
1011
use PHPUnit\Framework\Assert;
1112
use stdClass;
1213
use Throwable;
@@ -19,13 +20,15 @@
1920
use function PHPUnit\Framework\assertIsArray;
2021
use function PHPUnit\Framework\assertIsBool;
2122
use function PHPUnit\Framework\assertIsInt;
23+
use function PHPUnit\Framework\assertIsObject;
2224
use function PHPUnit\Framework\assertIsString;
2325
use function PHPUnit\Framework\assertNotInstanceOf;
2426
use function PHPUnit\Framework\assertNotNull;
2527
use function PHPUnit\Framework\assertNull;
2628
use function PHPUnit\Framework\assertObjectHasAttribute;
2729
use function PHPUnit\Framework\assertSame;
2830
use function PHPUnit\Framework\assertStringContainsStringIgnoringCase;
31+
use function PHPUnit\Framework\assertThat;
2932
use function PHPUnit\Framework\assertTrue;
3033
use function property_exists;
3134
use function sprintf;
@@ -59,6 +62,9 @@ final class ExpectedError
5962
/** @var string|null */
6063
private $codeName;
6164

65+
/** @var Matches|null */
66+
private $matchesResultDocument;
67+
6268
/** @var array */
6369
private $includedLabels = [];
6470

@@ -100,6 +106,11 @@ public function __construct(?stdClass $o, EntityMap $entityMap)
100106
$this->codeName = $o->errorCodeName;
101107
}
102108

109+
if (isset($o->errorResponse)) {
110+
assertIsObject($o->errorResponse);
111+
$this->matchesResultDocument = new Matches($o->errorResponse, $entityMap);
112+
}
113+
103114
if (isset($o->errorLabelsContain)) {
104115
assertIsArray($o->errorLabelsContain);
105116
assertContainsOnly('string', $o->errorLabelsContain);
@@ -154,6 +165,11 @@ public function assert(?Throwable $e = null): void
154165
$this->assertCodeName($e);
155166
}
156167

168+
if (isset($this->matchesResultDocument)) {
169+
assertInstanceOf(CommandException::class, $e);
170+
assertThat($e->getResultDocument(), $this->matchesResultDocument, 'CommandException result document matches');
171+
}
172+
157173
if (! empty($this->excludedLabels) || ! empty($this->includedLabels)) {
158174
assertInstanceOf(RuntimeException::class, $e);
159175

tests/UnifiedSpecTests/UnifiedTestRunner.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ final class UnifiedTestRunner
6464

6565
public const MIN_SCHEMA_VERSION = '1.0';
6666

67-
public const MAX_SCHEMA_VERSION = '1.8';
67+
/* Note: This is necessary to support expectedError.errorResponse from 1.12;
68+
* however, syntax from 1.9, 1.10, and 1.11 has not been implemented. */
69+
public const MAX_SCHEMA_VERSION = '1.12';
6870

6971
/** @var MongoDB\Client */
7072
private $internalClient;

tests/UnifiedSpecTests/Util.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ final class Util
7777
'dropCollection' => ['collection', 'session'],
7878
'listCollectionNames' => ['authorizedCollections', 'filter', 'maxTimeMS', 'session'],
7979
'listCollections' => ['authorizedCollections', 'filter', 'maxTimeMS', 'session'],
80-
'modifyCollection' => ['collection', 'changeStreamPreAndPostImages'],
80+
'modifyCollection' => ['collection', 'changeStreamPreAndPostImages', 'index', 'validator'],
8181
// Note: commandName is not used by PHP
8282
'runCommand' => ['command', 'session', 'commandName'],
8383
],
@@ -86,7 +86,7 @@ final class Util
8686
'bulkWrite' => ['let', 'requests', 'session', 'ordered', 'bypassDocumentValidation', 'comment'],
8787
'createChangeStream' => ['pipeline', 'session', 'fullDocument', 'fullDocumentBeforeChange', 'resumeAfter', 'startAfter', 'startAtOperationTime', 'batchSize', 'collation', 'maxAwaitTimeMS', 'comment', 'showExpandedEvents'],
8888
'createFindCursor' => ['filter', 'session', 'allowDiskUse', 'allowPartialResults', 'batchSize', 'collation', 'comment', 'cursorType', 'hint', 'limit', 'max', 'maxAwaitTimeMS', 'maxScan', 'maxTimeMS', 'min', 'modifiers', 'noCursorTimeout', 'oplogReplay', 'projection', 'returnKey', 'showRecordId', 'skip', 'snapshot', 'sort'],
89-
'createIndex' => ['keys', 'commitQuorum', 'maxTimeMS', 'name', 'session', 'comment'],
89+
'createIndex' => ['keys', 'comment', 'commitQuorum', 'maxTimeMS', 'name', 'session', 'unique'],
9090
'dropIndex' => ['name', 'session', 'maxTimeMS', 'comment'],
9191
'count' => ['filter', 'session', 'collation', 'hint', 'limit', 'maxTimeMS', 'skip', 'comment'],
9292
'countDocuments' => ['filter', 'session', 'limit', 'skip', 'collation', 'hint', 'maxTimeMS', 'comment'],

0 commit comments

Comments
 (0)