Skip to content

Commit 2e99cd8

Browse files
authored
PHPLIB-909: expectedError.errorResponse assertion (#989)
Synced with mongodb/specifications@5f8d058
1 parent 1043a3a commit 2e99cd8

13 files changed

+781
-5
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/UnifiedSpecTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class UnifiedSpecTest extends FunctionalTestCase
6565
'valid-pass/createEntities-operation: createEntities operation' => 'CSOT is not yet implemented (PHPC-1760)',
6666
'valid-pass/entity-cursor-iterateOnce: iterateOnce' => 'CSOT is not yet implemented (PHPC-1760)',
6767
'valid-pass/matches-lte-operator: special lte matching operator' => 'CSOT is not yet implemented (PHPC-1760)',
68+
// BulkWriteException cannot access server response
69+
'crud/bulkWrite-errorResponse: bulkWrite operations support errorResponse assertions' => 'BulkWriteException cannot access server response',
70+
'crud/deleteOne-errorResponse: delete operations support errorResponse assertions' => 'BulkWriteException cannot access server response',
71+
'crud/insertOne-errorResponse: insert operations support errorResponse assertions' => 'BulkWriteException cannot access server response',
72+
'crud/updateOne-errorResponse: update operations support errorResponse assertions' => 'BulkWriteException cannot access server response',
6873
];
6974

7075
/** @var UnifiedTestRunner */

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'],
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"description": "modifyCollection-errorResponse",
3+
"schemaVersion": "1.12",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0",
8+
"observeEvents": [
9+
"commandStartedEvent"
10+
]
11+
}
12+
},
13+
{
14+
"database": {
15+
"id": "database0",
16+
"client": "client0",
17+
"databaseName": "collMod-tests"
18+
}
19+
},
20+
{
21+
"collection": {
22+
"id": "collection0",
23+
"database": "database0",
24+
"collectionName": "test"
25+
}
26+
}
27+
],
28+
"initialData": [
29+
{
30+
"collectionName": "test",
31+
"databaseName": "collMod-tests",
32+
"documents": [
33+
{
34+
"_id": 1,
35+
"x": 1
36+
},
37+
{
38+
"_id": 2,
39+
"x": 1
40+
}
41+
]
42+
}
43+
],
44+
"tests": [
45+
{
46+
"description": "modifyCollection prepareUnique violations are accessible",
47+
"runOnRequirements": [
48+
{
49+
"minServerVersion": "5.2"
50+
}
51+
],
52+
"operations": [
53+
{
54+
"name": "createIndex",
55+
"object": "collection0",
56+
"arguments": {
57+
"keys": {
58+
"x": 1
59+
}
60+
}
61+
},
62+
{
63+
"name": "modifyCollection",
64+
"object": "database0",
65+
"arguments": {
66+
"collection": "test",
67+
"index": {
68+
"keyPattern": {
69+
"x": 1
70+
},
71+
"prepareUnique": true
72+
}
73+
}
74+
},
75+
{
76+
"name": "insertOne",
77+
"object": "collection0",
78+
"arguments": {
79+
"document": {
80+
"_id": 3,
81+
"x": 1
82+
}
83+
},
84+
"expectError": {
85+
"errorCode": 11000
86+
}
87+
},
88+
{
89+
"name": "modifyCollection",
90+
"object": "database0",
91+
"arguments": {
92+
"collection": "test",
93+
"index": {
94+
"keyPattern": {
95+
"x": 1
96+
},
97+
"unique": true
98+
}
99+
},
100+
"expectError": {
101+
"isClientError": false,
102+
"errorCode": 359,
103+
"errorResponse": {
104+
"violations": [
105+
{
106+
"ids": [
107+
1,
108+
2
109+
]
110+
}
111+
]
112+
}
113+
}
114+
}
115+
]
116+
}
117+
]
118+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"description": "aggregate-merge-errorResponse",
3+
"schemaVersion": "1.12",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "crud-tests"
15+
}
16+
},
17+
{
18+
"collection": {
19+
"id": "collection0",
20+
"database": "database0",
21+
"collectionName": "test"
22+
}
23+
}
24+
],
25+
"initialData": [
26+
{
27+
"collectionName": "test",
28+
"databaseName": "crud-tests",
29+
"documents": [
30+
{
31+
"_id": 1,
32+
"x": 1
33+
},
34+
{
35+
"_id": 2,
36+
"x": 1
37+
}
38+
]
39+
}
40+
],
41+
"tests": [
42+
{
43+
"description": "aggregate $merge DuplicateKey error is accessible",
44+
"runOnRequirements": [
45+
{
46+
"minServerVersion": "5.1",
47+
"topologies": [
48+
"single",
49+
"replicaset"
50+
]
51+
}
52+
],
53+
"operations": [
54+
{
55+
"name": "aggregate",
56+
"object": "database0",
57+
"arguments": {
58+
"pipeline": [
59+
{
60+
"$documents": [
61+
{
62+
"_id": 2,
63+
"x": 1
64+
}
65+
]
66+
},
67+
{
68+
"$merge": {
69+
"into": "test",
70+
"whenMatched": "fail"
71+
}
72+
}
73+
]
74+
},
75+
"expectError": {
76+
"errorCode": 11000,
77+
"errorResponse": {
78+
"keyPattern": {
79+
"_id": 1
80+
},
81+
"keyValue": {
82+
"_id": 2
83+
}
84+
}
85+
}
86+
}
87+
]
88+
}
89+
]
90+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"description": "bulkWrite-errorResponse",
3+
"schemaVersion": "1.12",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "crud-tests"
15+
}
16+
},
17+
{
18+
"collection": {
19+
"id": "collection0",
20+
"database": "database0",
21+
"collectionName": "test"
22+
}
23+
}
24+
],
25+
"tests": [
26+
{
27+
"description": "bulkWrite operations support errorResponse assertions",
28+
"runOnRequirements": [
29+
{
30+
"minServerVersion": "4.0.0",
31+
"topologies": [
32+
"single",
33+
"replicaset"
34+
]
35+
},
36+
{
37+
"minServerVersion": "4.2.0",
38+
"topologies": [
39+
"sharded"
40+
]
41+
}
42+
],
43+
"operations": [
44+
{
45+
"name": "failPoint",
46+
"object": "testRunner",
47+
"arguments": {
48+
"client": "client0",
49+
"failPoint": {
50+
"configureFailPoint": "failCommand",
51+
"mode": {
52+
"times": 1
53+
},
54+
"data": {
55+
"failCommands": [
56+
"insert"
57+
],
58+
"errorCode": 8
59+
}
60+
}
61+
}
62+
},
63+
{
64+
"name": "bulkWrite",
65+
"object": "collection0",
66+
"arguments": {
67+
"requests": [
68+
{
69+
"insertOne": {
70+
"document": {
71+
"_id": 1
72+
}
73+
}
74+
}
75+
]
76+
},
77+
"expectError": {
78+
"errorCode": 8,
79+
"errorResponse": {
80+
"code": 8
81+
}
82+
}
83+
}
84+
]
85+
}
86+
]
87+
}

0 commit comments

Comments
 (0)