Skip to content

Commit 693484e

Browse files
authored
PHPLIB-1648: initialData.createOptions and $$lte operator (#1646)
* PHPLIB-1648: initialData.createOptions and $$lte operator Mark test incomplete at runtime when encountering unsupported syntax from schema 1.9. * PHPLIB-1650: Fix Int64 detection in Matches::isNumeric() This was discovered by adding flexible numeric comparisons to the $$lte tests.
1 parent 74f7a02 commit 693484e

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

tests/UnifiedSpecTests/CollectionData.php

+18-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
use function PHPUnit\Framework\assertContainsOnly;
1616
use function PHPUnit\Framework\assertIsArray;
17+
use function PHPUnit\Framework\assertIsObject;
1718
use function PHPUnit\Framework\assertIsString;
1819
use function PHPUnit\Framework\assertNotNull;
20+
use function PHPUnit\Framework\assertObjectNotHasProperty;
1921
use function PHPUnit\Framework\assertThat;
2022
use function sprintf;
2123

@@ -27,6 +29,8 @@ class CollectionData
2729

2830
private array $documents;
2931

32+
private array $createOptions = [];
33+
3034
public function __construct(stdClass $o)
3135
{
3236
assertIsString($o->collectionName);
@@ -38,6 +42,15 @@ public function __construct(stdClass $o)
3842
assertIsArray($o->documents);
3943
assertContainsOnly('object', $o->documents);
4044
$this->documents = $o->documents;
45+
46+
if (isset($o->createOptions)) {
47+
assertIsObject($o->createOptions);
48+
/* The writeConcern option is prohibited here, as prepareInitialData() applies w:majority. Since a session
49+
* option would be ignored by prepareInitialData() we can assert that it is also omitted. */
50+
assertObjectNotHasProperty('writeConcern', $o->createOptions);
51+
assertObjectNotHasProperty('session', $o->createOptions);
52+
$this->createOptions = (array) $o->createOptions;
53+
}
4154
}
4255

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

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

52-
if (empty($this->documents)) {
53-
$database->createCollection($this->collectionName, ['session' => $session]);
54-
55-
return;
65+
if (empty($this->documents) || ! empty($this->createOptions)) {
66+
$database->createCollection($this->collectionName, ['session' => $session] + $this->createOptions);
5667
}
5768

58-
$database->selectCollection($this->collectionName)->insertMany($this->documents, ['session' => $session]);
69+
if (! empty($this->documents)) {
70+
$database->selectCollection($this->collectionName)->insertMany($this->documents, ['session' => $session]);
71+
}
5972
}
6073

6174
public function assertOutcome(Client $client): void

tests/UnifiedSpecTests/Constraint/Matches.php

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use LogicException;
66
use MongoDB\BSON\Document;
7+
use MongoDB\BSON\Int64;
78
use MongoDB\BSON\Serializable;
89
use MongoDB\BSON\Type;
910
use MongoDB\Model\BSONArray;
@@ -30,10 +31,12 @@
3031
use function PHPUnit\Framework\assertIsBool;
3132
use function PHPUnit\Framework\assertIsString;
3233
use function PHPUnit\Framework\assertJson;
34+
use function PHPUnit\Framework\assertLessThanOrEqual;
3335
use function PHPUnit\Framework\assertMatchesRegularExpression;
3436
use function PHPUnit\Framework\assertNotNull;
3537
use function PHPUnit\Framework\assertStringStartsWith;
3638
use function PHPUnit\Framework\assertThat;
39+
use function PHPUnit\Framework\assertTrue;
3740
use function PHPUnit\Framework\containsOnly;
3841
use function PHPUnit\Framework\isInstanceOf;
3942
use function PHPUnit\Framework\isType;
@@ -352,6 +355,14 @@ private function assertMatchesOperator(BSONDocument $operator, $actual, string $
352355
return;
353356
}
354357

358+
if ($name === '$$lte') {
359+
assertTrue(self::isNumeric($operator['$$lte']), '$$lte requires number');
360+
assertTrue(self::isNumeric($actual), '$actual operand for $$lte should be a number');
361+
assertLessThanOrEqual($operator['$$lte'], $actual);
362+
363+
return;
364+
}
365+
355366
throw new LogicException('unsupported operator: ' . $name);
356367
}
357368

tests/UnifiedSpecTests/Context.php

+4
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ private static function getEnv(string $name): string
492492

493493
private static function prepareCollectionOrDatabaseOptions(array $options): array
494494
{
495+
if (array_key_exists('timeoutMS', $options)) {
496+
Assert::markTestIncomplete('CSOT is not yet implemented (PHPC-1760)');
497+
}
498+
495499
Util::assertHasOnlyKeys($options, ['readConcern', 'readPreference', 'writeConcern']);
496500

497501
return Util::prepareCommonOptions($options);

tests/UnifiedSpecTests/ExpectedError.php

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function __construct(?stdClass $o, EntityMap $entityMap)
8181
$this->isClientError = $o->isClientError;
8282
}
8383

84+
if (property_exists($o, 'isTimeoutError')) {
85+
Assert::markTestIncomplete('CSOT is not yet implemented (PHPC-1760)');
86+
}
87+
8488
if (isset($o->errorContains)) {
8589
assertIsString($o->errorContains);
8690
$this->messageContains = $o->errorContains;

tests/UnifiedSpecTests/UnifiedSpecTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ class UnifiedSpecTest extends FunctionalTestCase
5353
'valid-pass/entity-client-cmap-events: events are captured during an operation' => 'PHPC does not implement CMAP',
5454
'valid-pass/expectedEventsForClient-eventType: eventType can be set to command and cmap' => 'PHPC does not implement CMAP',
5555
'valid-pass/expectedEventsForClient-eventType: eventType defaults to command if unset' => 'PHPC does not implement CMAP',
56-
// CSOT is not yet implemented (PHPC-1760)
57-
'valid-pass/collectionData-createOptions: collection is created with the correct options' => 'CSOT is not yet implemented (PHPC-1760)',
58-
'valid-pass/operator-lte: special lte matching operator' => 'CSOT is not yet implemented (PHPC-1760)',
5956
// libmongoc always adds readConcern to aggregate command
6057
'index-management/search index operations ignore read and write concern: listSearchIndexes ignores read and write concern' => 'libmongoc appends readConcern to aggregate command',
6158
// Uses an invalid object name

tests/UnifiedSpecTests/UnifiedTestRunner.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final class UnifiedTestRunner
5858
/**
5959
* Support for the following schema versions is incomplete:
6060
*
61-
* - 1.9: Only createEntities operation is implemented
61+
* - 1.9: collectionOrDatabaseOptions.timeoutMS and expectedError.isTimeoutError are not implemented
6262
* - 1.10: Not implemented
6363
* - 1.11: Not implemented, but CMAP is not applicable
6464
* - 1.13: Only $$matchAsDocument and $$matchAsRoot is implemented

0 commit comments

Comments
 (0)