Skip to content

PHPLIB-662: Unified test runner should error for unexpected operation arguments #898

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

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions tests/MethodsParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace MongoDB\Tests;

use MongoDB\ChangeStream;
use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Session;
use MongoDB\GridFS\Bucket;
use MongoDB\Tests\UnifiedSpecTests\Operation;

final class MethodsParams
{
/**
* Array to fill, which contains the schema of allowed attributes for operations.
*/
public static $args = [
Operation::OBJECT_TEST_RUNNER => [
'assertCollectionExists' => ['databaseName', 'collectionName'],
'assertCollectionNotExists' => ['databaseName', 'collectionName'],
'assertIndexExists' => ['databaseName', 'collectionName', 'indexName'],
'assertIndexNotExists' => ['databaseName', 'collectionName', 'indexName'],
'assertSameLsidOnLastTwoCommands' => ['client'],
'assertDifferentLsidOnLastTwoCommands' => ['client'],
'assertNumberConnectionsCheckedOut' => ['connections'],
'assertSessionDirty' => ['session'],
'assertSessionNotDirty' => ['session'],
'assertSessionPinned' => ['session'],
'assertSessionTransactionState' => ['session', 'state'],
'assertSessionUnpinned' => ['session'],
'failPoint' => ['client', 'failPoint', ''],
'targetedFailPoint' => ['session', 'failPoint' ],
// 'loop' => [],
],
Client::class => [],
Database::class => [],
Collection::class => [],
ChangeStream::class => [],
Cursor::class => [],
Session::class => [],
Bucket::class => [],
];
}
8 changes: 8 additions & 0 deletions tests/UnifiedSpecTests/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ private function execute()
private function executeForChangeStream(ChangeStream $changeStream)
{
$args = $this->prepareArguments();
Util::assertParamsBySchema(get_class($changeStream), $this->name, $args);

switch ($this->name) {
case 'iterateUntilDocumentOrError':
Expand Down Expand Up @@ -243,6 +244,7 @@ private function executeForChangeStream(ChangeStream $changeStream)
private function executeForClient(Client $client)
{
$args = $this->prepareArguments();
Util::assertParamsBySchema(get_class($client), $this->name, $args);

switch ($this->name) {
case 'createChangeStream':
Expand All @@ -268,6 +270,7 @@ private function executeForClient(Client $client)
private function executeForCollection(Collection $collection)
{
$args = $this->prepareArguments();
Util::assertParamsBySchema(get_class($collection), $this->name, $args);

switch ($this->name) {
case 'aggregate':
Expand Down Expand Up @@ -479,6 +482,7 @@ private function executeForCollection(Collection $collection)
private function executeForCursor(Cursor $cursor)
{
$args = $this->prepareArguments();
Util::assertParamsBySchema(get_class($cursor), $this->name, $args);

switch ($this->name) {
case 'close':
Expand Down Expand Up @@ -526,6 +530,7 @@ private function executeForCursor(Cursor $cursor)
private function executeForDatabase(Database $database)
{
$args = $this->prepareArguments();
Util::assertParamsBySchema(get_class($database), $this->name, $args);

switch ($this->name) {
case 'aggregate':
Expand Down Expand Up @@ -587,6 +592,7 @@ private function executeForDatabase(Database $database)
private function executeForSession(Session $session)
{
$args = $this->prepareArguments();
Util::assertParamsBySchema(get_class($session), $this->name, $args);

switch ($this->name) {
case 'abortTransaction':
Expand Down Expand Up @@ -627,6 +633,7 @@ private function executeForSession(Session $session)
private function executeForBucket(Bucket $bucket)
{
$args = $this->prepareArguments();
Util::assertParamsBySchema(get_class($bucket), $this->name, $args);

switch ($this->name) {
case 'delete':
Expand Down Expand Up @@ -672,6 +679,7 @@ private function executeForBucket(Bucket $bucket)
private function executeForTestRunner()
{
$args = $this->prepareArguments();
Util::assertParamsBySchema(self::OBJECT_TEST_RUNNER, $this->name, $args);

switch ($this->name) {
case 'assertCollectionExists':
Expand Down
14 changes: 14 additions & 0 deletions tests/UnifiedSpecTests/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Tests\MethodsParams;
use stdClass;

use function array_diff_key;
Expand Down Expand Up @@ -33,6 +34,19 @@ public static function assertHasOnlyKeys($arrayOrObject, array $keys): void
assertEmpty($diff, 'Unsupported keys: ' . implode(',', array_keys($diff)));
}

/**
* @param string $executingObjectName
* @param string $operation
* @param array $args
* @return void
*/
public static function assertParamsBySchema(string $executingObjectName, string $operation, array $args): void
{
if (isset(MethodsParams::$args[$executingObjectName][$operation])) {
self::assertHasOnlyKeys($args, MethodsParams::$args[$executingObjectName][$operation]);
}
}

public static function createReadConcern(stdClass $o): ReadConcern
{
self::assertHasOnlyKeys($o, ['level']);
Expand Down