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-1648: initialData.createOptions and $$lte operator #1646

Merged
merged 2 commits into from
Mar 31, 2025
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
23 changes: 18 additions & 5 deletions tests/UnifiedSpecTests/CollectionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

use function PHPUnit\Framework\assertContainsOnly;
use function PHPUnit\Framework\assertIsArray;
use function PHPUnit\Framework\assertIsObject;
use function PHPUnit\Framework\assertIsString;
use function PHPUnit\Framework\assertNotNull;
use function PHPUnit\Framework\assertObjectNotHasProperty;
use function PHPUnit\Framework\assertThat;
use function sprintf;

Expand All @@ -27,6 +29,8 @@ class CollectionData

private array $documents;

private array $createOptions = [];

public function __construct(stdClass $o)
{
assertIsString($o->collectionName);
Expand All @@ -38,6 +42,15 @@ public function __construct(stdClass $o)
assertIsArray($o->documents);
assertContainsOnly('object', $o->documents);
$this->documents = $o->documents;

if (isset($o->createOptions)) {
assertIsObject($o->createOptions);
/* The writeConcern option is prohibited here, as prepareInitialData() applies w:majority. Since a session
* option would be ignored by prepareInitialData() we can assert that it is also omitted. */
assertObjectNotHasProperty('writeConcern', $o->createOptions);
assertObjectNotHasProperty('session', $o->createOptions);
$this->createOptions = (array) $o->createOptions;
}
}

public function prepareInitialData(Client $client, ?Session $session = null): void
Expand All @@ -49,13 +62,13 @@ public function prepareInitialData(Client $client, ?Session $session = null): vo

$database->dropCollection($this->collectionName, ['session' => $session]);

if (empty($this->documents)) {
$database->createCollection($this->collectionName, ['session' => $session]);

return;
if (empty($this->documents) || ! empty($this->createOptions)) {
$database->createCollection($this->collectionName, ['session' => $session] + $this->createOptions);
}

$database->selectCollection($this->collectionName)->insertMany($this->documents, ['session' => $session]);
if (! empty($this->documents)) {
$database->selectCollection($this->collectionName)->insertMany($this->documents, ['session' => $session]);
}
}

public function assertOutcome(Client $client): void
Expand Down
11 changes: 11 additions & 0 deletions tests/UnifiedSpecTests/Constraint/Matches.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LogicException;
use MongoDB\BSON\Document;
use MongoDB\BSON\Int64;
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Type;
use MongoDB\Model\BSONArray;
Expand All @@ -30,10 +31,12 @@
use function PHPUnit\Framework\assertIsBool;
use function PHPUnit\Framework\assertIsString;
use function PHPUnit\Framework\assertJson;
use function PHPUnit\Framework\assertLessThanOrEqual;
use function PHPUnit\Framework\assertMatchesRegularExpression;
use function PHPUnit\Framework\assertNotNull;
use function PHPUnit\Framework\assertStringStartsWith;
use function PHPUnit\Framework\assertThat;
use function PHPUnit\Framework\assertTrue;
use function PHPUnit\Framework\containsOnly;
use function PHPUnit\Framework\isInstanceOf;
use function PHPUnit\Framework\isType;
Expand Down Expand Up @@ -352,6 +355,14 @@ private function assertMatchesOperator(BSONDocument $operator, $actual, string $
return;
}

if ($name === '$$lte') {
assertTrue(self::isNumeric($operator['$$lte']), '$$lte requires number');
assertTrue(self::isNumeric($actual), '$actual operand for $$lte should be a number');
assertLessThanOrEqual($operator['$$lte'], $actual);

return;
}

throw new LogicException('unsupported operator: ' . $name);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/UnifiedSpecTests/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ private static function getEnv(string $name): string

private static function prepareCollectionOrDatabaseOptions(array $options): array
{
if (array_key_exists('timeoutMS', $options)) {
Assert::markTestIncomplete('CSOT is not yet implemented (PHPC-1760)');
}

Util::assertHasOnlyKeys($options, ['readConcern', 'readPreference', 'writeConcern']);

return Util::prepareCommonOptions($options);
Expand Down
4 changes: 4 additions & 0 deletions tests/UnifiedSpecTests/ExpectedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function __construct(?stdClass $o, EntityMap $entityMap)
$this->isClientError = $o->isClientError;
}

if (property_exists($o, 'isTimeoutError')) {
Assert::markTestIncomplete('CSOT is not yet implemented (PHPC-1760)');
}

if (isset($o->errorContains)) {
assertIsString($o->errorContains);
$this->messageContains = $o->errorContains;
Expand Down
3 changes: 0 additions & 3 deletions tests/UnifiedSpecTests/UnifiedSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ class UnifiedSpecTest extends FunctionalTestCase
'valid-pass/entity-client-cmap-events: events are captured during an operation' => 'PHPC does not implement CMAP',
'valid-pass/expectedEventsForClient-eventType: eventType can be set to command and cmap' => 'PHPC does not implement CMAP',
'valid-pass/expectedEventsForClient-eventType: eventType defaults to command if unset' => 'PHPC does not implement CMAP',
// CSOT is not yet implemented (PHPC-1760)
'valid-pass/collectionData-createOptions: collection is created with the correct options' => 'CSOT is not yet implemented (PHPC-1760)',
'valid-pass/operator-lte: special lte matching operator' => 'CSOT is not yet implemented (PHPC-1760)',
// libmongoc always adds readConcern to aggregate command
'index-management/search index operations ignore read and write concern: listSearchIndexes ignores read and write concern' => 'libmongoc appends readConcern to aggregate command',
// Uses an invalid object name
Expand Down
2 changes: 1 addition & 1 deletion tests/UnifiedSpecTests/UnifiedTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class UnifiedTestRunner
/**
* Support for the following schema versions is incomplete:
*
* - 1.9: Only createEntities operation is implemented
* - 1.9: collectionOrDatabaseOptions.timeoutMS and expectedError.isTimeoutError are not implemented
* - 1.10: Not implemented
* - 1.11: Not implemented, but CMAP is not applicable
* - 1.13: Only $$matchAsDocument and $$matchAsRoot is implemented
Expand Down
Loading