Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-909: Spec tests for expectedError.errorResponse assertions #989

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/UnifiedSpecTests/ExpectedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MongoDB\Driver\Exception\ExecutionTimeoutException;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Tests\UnifiedSpecTests\Constraint\Matches;
use PHPUnit\Framework\Assert;
use stdClass;
use Throwable;
Expand All @@ -19,13 +20,15 @@
use function PHPUnit\Framework\assertIsArray;
use function PHPUnit\Framework\assertIsBool;
use function PHPUnit\Framework\assertIsInt;
use function PHPUnit\Framework\assertIsObject;
use function PHPUnit\Framework\assertIsString;
use function PHPUnit\Framework\assertNotInstanceOf;
use function PHPUnit\Framework\assertNotNull;
use function PHPUnit\Framework\assertNull;
use function PHPUnit\Framework\assertObjectHasAttribute;
use function PHPUnit\Framework\assertSame;
use function PHPUnit\Framework\assertStringContainsStringIgnoringCase;
use function PHPUnit\Framework\assertThat;
use function PHPUnit\Framework\assertTrue;
use function property_exists;
use function sprintf;
Expand Down Expand Up @@ -59,6 +62,9 @@ final class ExpectedError
/** @var string|null */
private $codeName;

/** @var Matches|null */
private $matchesResultDocument;

/** @var array */
private $includedLabels = [];

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

if (isset($o->errorResponse)) {
assertIsObject($o->errorResponse);
$this->matchesResultDocument = new Matches($o->errorResponse, $entityMap);
}

if (isset($o->errorLabelsContain)) {
assertIsArray($o->errorLabelsContain);
assertContainsOnly('string', $o->errorLabelsContain);
Expand Down Expand Up @@ -154,6 +165,11 @@ public function assert(?Throwable $e = null): void
$this->assertCodeName($e);
}

if (isset($this->matchesResultDocument)) {
assertInstanceOf(CommandException::class, $e);
assertThat($e->getResultDocument(), $this->matchesResultDocument, 'CommandException result document matches');
}

if (! empty($this->excludedLabels) || ! empty($this->includedLabels)) {
assertInstanceOf(RuntimeException::class, $e);

Expand Down
5 changes: 5 additions & 0 deletions tests/UnifiedSpecTests/UnifiedSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class UnifiedSpecTest extends FunctionalTestCase
'valid-pass/createEntities-operation: createEntities operation' => 'CSOT is not yet implemented (PHPC-1760)',
'valid-pass/entity-cursor-iterateOnce: iterateOnce' => 'CSOT is not yet implemented (PHPC-1760)',
'valid-pass/matches-lte-operator: special lte matching operator' => 'CSOT is not yet implemented (PHPC-1760)',
// BulkWriteException cannot access server response
'crud/bulkWrite-errorResponse: bulkWrite operations support errorResponse assertions' => 'BulkWriteException cannot access server response',
'crud/deleteOne-errorResponse: delete operations support errorResponse assertions' => 'BulkWriteException cannot access server response',
'crud/insertOne-errorResponse: insert operations support errorResponse assertions' => 'BulkWriteException cannot access server response',
'crud/updateOne-errorResponse: update operations support errorResponse assertions' => 'BulkWriteException cannot access server response',
];

/** @var UnifiedTestRunner */
Expand Down
4 changes: 3 additions & 1 deletion tests/UnifiedSpecTests/UnifiedTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ final class UnifiedTestRunner

public const MIN_SCHEMA_VERSION = '1.0';

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

/** @var MongoDB\Client */
private $internalClient;
Expand Down
4 changes: 2 additions & 2 deletions tests/UnifiedSpecTests/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ final class Util
'dropCollection' => ['collection', 'session'],
'listCollectionNames' => ['authorizedCollections', 'filter', 'maxTimeMS', 'session'],
'listCollections' => ['authorizedCollections', 'filter', 'maxTimeMS', 'session'],
'modifyCollection' => ['collection', 'changeStreamPreAndPostImages'],
'modifyCollection' => ['collection', 'changeStreamPreAndPostImages', 'index', 'validator'],
// Note: commandName is not used by PHP
'runCommand' => ['command', 'session', 'commandName'],
],
Expand All @@ -86,7 +86,7 @@ final class Util
'bulkWrite' => ['let', 'requests', 'session', 'ordered', 'bypassDocumentValidation', 'comment'],
'createChangeStream' => ['pipeline', 'session', 'fullDocument', 'fullDocumentBeforeChange', 'resumeAfter', 'startAfter', 'startAtOperationTime', 'batchSize', 'collation', 'maxAwaitTimeMS', 'comment', 'showExpandedEvents'],
'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'],
'createIndex' => ['keys', 'commitQuorum', 'maxTimeMS', 'name', 'session', 'comment'],
'createIndex' => ['keys', 'comment', 'commitQuorum', 'maxTimeMS', 'name', 'session', 'unique'],
'dropIndex' => ['name', 'session', 'maxTimeMS', 'comment'],
'count' => ['filter', 'session', 'collation', 'hint', 'limit', 'maxTimeMS', 'skip', 'comment'],
'countDocuments' => ['filter', 'session', 'limit', 'skip', 'collation', 'hint', 'maxTimeMS', 'comment'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"description": "modifyCollection-errorResponse",
"schemaVersion": "1.12",
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "collMod-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "test"
}
}
],
"initialData": [
{
"collectionName": "test",
"databaseName": "collMod-tests",
"documents": [
{
"_id": 1,
"x": 1
},
{
"_id": 2,
"x": 1
}
]
}
],
"tests": [
{
"description": "modifyCollection prepareUnique violations are accessible",
"runOnRequirements": [
{
"minServerVersion": "5.2"
}
],
"operations": [
{
"name": "createIndex",
"object": "collection0",
"arguments": {
"keys": {
"x": 1
}
}
},
{
"name": "modifyCollection",
"object": "database0",
"arguments": {
"collection": "test",
"index": {
"keyPattern": {
"x": 1
},
"prepareUnique": true
}
}
},
{
"name": "insertOne",
"object": "collection0",
"arguments": {
"document": {
"_id": 3,
"x": 1
}
},
"expectError": {
"errorCode": 11000
}
},
{
"name": "modifyCollection",
"object": "database0",
"arguments": {
"collection": "test",
"index": {
"keyPattern": {
"x": 1
},
"unique": true
}
},
"expectError": {
"isClientError": false,
"errorCode": 359,
"errorResponse": {
"violations": [
{
"ids": [
1,
2
]
}
]
}
}
}
]
}
]
}
90 changes: 90 additions & 0 deletions tests/UnifiedSpecTests/crud/aggregate-merge-errorResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"description": "aggregate-merge-errorResponse",
"schemaVersion": "1.12",
"createEntities": [
{
"client": {
"id": "client0"
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "crud-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "test"
}
}
],
"initialData": [
{
"collectionName": "test",
"databaseName": "crud-tests",
"documents": [
{
"_id": 1,
"x": 1
},
{
"_id": 2,
"x": 1
}
]
}
],
"tests": [
{
"description": "aggregate $merge DuplicateKey error is accessible",
"runOnRequirements": [
{
"minServerVersion": "5.1",
"topologies": [
"single",
"replicaset"
]
}
],
"operations": [
{
"name": "aggregate",
"object": "database0",
"arguments": {
"pipeline": [
{
"$documents": [
{
"_id": 2,
"x": 1
}
]
},
{
"$merge": {
"into": "test",
"whenMatched": "fail"
}
}
]
},
"expectError": {
"errorCode": 11000,
"errorResponse": {
"keyPattern": {
"_id": 1
},
"keyValue": {
"_id": 2
}
}
}
}
]
}
]
}
87 changes: 87 additions & 0 deletions tests/UnifiedSpecTests/crud/bulkWrite-errorResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"description": "bulkWrite-errorResponse",
"schemaVersion": "1.12",
"createEntities": [
{
"client": {
"id": "client0"
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "crud-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "test"
}
}
],
"tests": [
{
"description": "bulkWrite operations support errorResponse assertions",
"runOnRequirements": [
{
"minServerVersion": "4.0.0",
"topologies": [
"single",
"replicaset"
]
},
{
"minServerVersion": "4.2.0",
"topologies": [
"sharded"
]
}
],
"operations": [
{
"name": "failPoint",
"object": "testRunner",
"arguments": {
"client": "client0",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 1
},
"data": {
"failCommands": [
"insert"
],
"errorCode": 8
}
}
}
},
{
"name": "bulkWrite",
"object": "collection0",
"arguments": {
"requests": [
{
"insertOne": {
"document": {
"_id": 1
}
}
}
]
},
"expectError": {
"errorCode": 8,
"errorResponse": {
"code": 8
}
}
}
]
}
]
}
Loading