Skip to content

Commit e4ef88b

Browse files
committed
Changes to get CRUD POC tests running
1 parent 8860afb commit e4ef88b

File tree

9 files changed

+354
-749
lines changed

9 files changed

+354
-749
lines changed

tests/UnifiedSpecTests/Constraint/Matches.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ private static function prepare($bson)
416416
}
417417

418418
if (! $bson instanceof BSONArray && ! $bson instanceof BSONDocument) {
419-
$bson = new BSONDocument($bson);
419+
/* If $bson is an object, any numeric keys may become inaccessible.
420+
* We can work around this by casting back to an array. */
421+
$bson = new BSONDocument((array) $bson);
420422
}
421423

422424
foreach ($bson as $key => $value) {

tests/UnifiedSpecTests/Context.php

Lines changed: 59 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
use MongoDB\Client;
77
use MongoDB\Collection;
88
use MongoDB\Database;
9+
use MongoDB\Driver\Manager;
910
use MongoDB\Driver\ReadConcern;
1011
use MongoDB\Driver\ReadPreference;
11-
use MongoDB\Driver\Session;
12+
use MongoDB\Driver\Server;
1213
use MongoDB\Driver\WriteConcern;
1314
use stdClass;
1415
use function array_diff_key;
@@ -101,41 +102,76 @@ public function createEntities(array $entities)
101102
}
102103
}
103104

104-
public function getEntityMap() : EntityMap
105+
public static function createReadConcern(stdClass $o) : ReadConcern
105106
{
106-
return $this->entityMap;
107-
}
107+
self::assertHasOnlyKeys($o, ['level']);
108108

109-
public function getInternalClient() : Client
110-
{
111-
return $this->internalClient;
109+
$level = $o->level ?? null;
110+
assertInternalType('string', $level);
111+
112+
return new ReadConcern($level);
112113
}
113114

114-
public function prepareOperationArguments(array $args) : array
115+
public static function createReadPreference(stdClass $o) : ReadPreference
115116
{
116-
if (array_key_exists('readConcern', $args)) {
117-
assertInternalType('object', $args['readConcern']);
118-
$args['readConcern'] = self::prepareReadConcern($args['readConcern']);
117+
self::assertHasOnlyKeys($o, ['mode', 'tagSets', 'maxStalenessSeconds', 'hedge']);
118+
119+
$mode = $o->mode ?? null;
120+
$tagSets = $o->tagSets ?? null;
121+
$maxStalenessSeconds = $o->maxStalenessSeconds ?? null;
122+
$hedge = $o->hedge ?? null;
123+
124+
assertInternalType('string', $mode);
125+
126+
if (isset($tagSets)) {
127+
assertInternalType('array', $tagSets);
128+
assertContains('object', $tagSets);
119129
}
120130

121-
if (array_key_exists('readPreference', $args)) {
122-
assertInternalType('object', $args['readPreference']);
123-
$args['readPreference'] = self::prepareReadPreference($args['readPreference']);
131+
$options = [];
132+
133+
if (isset($maxStalenessSeconds)) {
134+
assertInternalType('int', $maxStalenessSeconds);
135+
$options['maxStalenessSeconds'] = $maxStalenessSeconds;
124136
}
125137

126-
if (array_key_exists('session', $args)) {
127-
assertInternalType('string', $args['session']);
128-
$session = $this->entityMap[$args['session']];
129-
assertInstanceOf(Session::class, $session);
130-
$args['session'] = $session;
138+
if (isset($hedge)) {
139+
assertInternalType('object', $hedge);
140+
$options['hedge'] = $hedge;
131141
}
132142

133-
if (array_key_exists('writeConcern', $args)) {
134-
assertInternalType('object', $args['writeConcern']);
135-
$args['writeConcern'] = self::prepareWriteConcern($args['writeConcern']);
143+
return new ReadPreference($mode, $tagSets, $options);
144+
}
145+
146+
public static function createWriteConcern(stdClass $o) : WriteConcern
147+
{
148+
self::assertHasOnlyKeys($o, ['w', 'wtimeoutMS', 'journal']);
149+
150+
$w = $o->w ?? -2; /* MONGOC_WRITE_CONCERN_W_DEFAULT */
151+
$wtimeoutMS = $o->wtimeoutMS ?? 0;
152+
$journal = $o->journal ?? null;
153+
154+
assertThat($w, logicalOr(isType('int'), isType('string')));
155+
assertInternalType('int', $wtimeoutMS);
156+
157+
$args = [$w, $wtimeoutMS];
158+
159+
if (isset($journal)) {
160+
assertInternalType('bool', $journal);
161+
$args[] = $journal;
136162
}
137163

138-
return $args;
164+
return new WriteConcern(...$args);
165+
}
166+
167+
public function getEntityMap() : EntityMap
168+
{
169+
return $this->entityMap;
170+
}
171+
172+
public function getInternalClient() : Client
173+
{
174+
return $this->internalClient;
139175
}
140176

141177
public function startEventObservers()
@@ -267,68 +303,6 @@ private static function prepareCollectionOrDatabaseOptions(array $options) : arr
267303
return $options;
268304
}
269305

270-
private static function createReadConcern(stdClass $o) : ReadConcern
271-
{
272-
self::assertHasOnlyKeys($o, ['level']);
273-
274-
$level = $o->level ?? null;
275-
assertInternalType('string', $level);
276-
277-
return new ReadConcern($level);
278-
}
279-
280-
private static function createReadPreference(stdClass $o) : ReadPreference
281-
{
282-
self::assertHasOnlyKeys($o, ['mode', 'tagSets', 'maxStalenessSeconds', 'hedge']);
283-
284-
$mode = $o->mode ?? null;
285-
$tagSets = $o->tagSets ?? null;
286-
$maxStalenessSeconds = $o->maxStalenessSeconds ?? null;
287-
$hedge = $o->hedge ?? null;
288-
289-
assertInternalType('string', $mode);
290-
291-
if (isset($tagSets)) {
292-
assertInternalType('array', $tagSets);
293-
assertContains('object', $tagSets);
294-
}
295-
296-
$options = [];
297-
298-
if (isset($maxStalenessSeconds)) {
299-
assertInternalType('int', $maxStalenessSeconds);
300-
$options['maxStalenessSeconds'] = $maxStalenessSeconds;
301-
}
302-
303-
if (isset($hedge)) {
304-
assertInternalType('object', $hedge);
305-
$options['hedge'] = $hedge;
306-
}
307-
308-
return new ReadPreference($mode, $tagSets, $options);
309-
}
310-
311-
private static function createWriteConcern(stdClass $o) : WriteConcern
312-
{
313-
self::assertHasOnlyKeys($o, ['w', 'wtimeoutMS', 'journal']);
314-
315-
$w = $o->w ?? -2; /* MONGOC_WRITE_CONCERN_W_DEFAULT */
316-
$wtimeoutMS = $o->wtimeoutMS ?? 0;
317-
$journal = $o->journal ?? null;
318-
319-
assertThat($w, logicalOr(isType('int'), isType('string')));
320-
assertInternalType('int', $wtimeoutMS);
321-
322-
$args = [$w, $wtimeoutMS];
323-
324-
if (isset($journal)) {
325-
assertInternalType('bool', $journal);
326-
$args[] = $journal;
327-
}
328-
329-
return new WriteConcern(...$args);
330-
}
331-
332306
/**
333307
* Removes mongos hosts beyond the first if the URI refers to a sharded
334308
* cluster. Otherwise, the URI is returned as-is.

tests/UnifiedSpecTests/EntityMap.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@
33
namespace MongoDB\Tests\UnifiedSpecTests;
44

55
use ArrayAccess;
6+
use MongoDB\ChangeStream;
7+
use MongoDB\Client;
8+
use MongoDB\Collection;
9+
use MongoDB\Database;
610
use MongoDB\Driver\Session;
11+
use MongoDB\GridFS\Bucket;
12+
use MongoDB\Tests\UnifiedSpecTests\Constraint\IsBsonType;
13+
use MongoDB\Tests\UnifiedSpecTests\Constraint\IsStream;
714
use PHPUnit\Framework\Assert;
15+
use PHPUnit\Framework\Constraint\Constraint;
816
use function array_key_exists;
917
use function assertArrayHasKey;
1018
use function assertArrayNotHasKey;
1119
use function assertInternalType;
20+
use function assertThat;
21+
use function isInstanceOf;
22+
use function logicalOr;
1223
use function sprintf;
1324

1425
class EntityMap implements ArrayAccess
1526
{
1627
/** @var array */
1728
private $map = [];
1829

30+
/** @var Constraint */
31+
private static $isSupportedType;
32+
1933
public function __destruct()
2034
{
2135
/* TODO: Determine if this is actually necessary. References to session
@@ -55,6 +69,7 @@ public function offsetSet($key, $value)
5569
{
5670
assertInternalType('string', $key);
5771
assertArrayNotHasKey($key, $this->map, sprintf('Entity already exists for key "%s" and cannot be replaced', $key));
72+
assertThat($value, self::isSupportedType());
5873

5974
$this->map[$key] = $value;
6075
}
@@ -66,4 +81,22 @@ public function offsetUnset($key)
6681
{
6782
Assert::fail('Entities cannot be removed from the map');
6883
}
84+
85+
private static function isSupportedType() : Constraint
86+
{
87+
if (self::$isSupportedType === null) {
88+
self::$isSupportedType = logicalOr(
89+
isInstanceOf(Client::class),
90+
isInstanceOf(Database::class),
91+
isInstanceOf(Collection::class),
92+
isInstanceOf(Session::class),
93+
isInstanceOf(Bucket::class),
94+
isInstanceOf(ChangeStream::class),
95+
IsBsonType::any(),
96+
new IsStream()
97+
);
98+
}
99+
100+
return self::$isSupportedType;
101+
}
69102
}

tests/UnifiedSpecTests/ExpectedError.php

Lines changed: 14 additions & 22 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 PHPUnit\Framework\Assert;
1011
use stdClass;
1112
use Throwable;
1213
use function assertArrayHasKey;
@@ -20,6 +21,7 @@
2021
use function assertSame;
2122
use function assertTrue;
2223
use function get_class;
24+
use function property_exists;
2325
use function sprintf;
2426

2527
final class ExpectedError
@@ -37,7 +39,7 @@ final class ExpectedError
3739
];
3840

3941
/** @var bool */
40-
private $isError = true;
42+
private $isError = false;
4143

4244
/** @var bool */
4345
private $isClientError;
@@ -60,8 +62,14 @@ final class ExpectedError
6062
/** @var ExpectedResult */
6163
private $expectedResult;
6264

63-
private function __construct(stdClass $o = null)
65+
public function __construct(Context $context, stdClass $o = null)
6466
{
67+
if ($o === null) {
68+
return;
69+
}
70+
71+
$this->isError = true;
72+
6573
if (isset($o->isError)) {
6674
assertTrue($o->isError);
6775
}
@@ -89,33 +97,17 @@ private function __construct(stdClass $o = null)
8997
if (isset($o->errorLabelsContain)) {
9098
assertInternalType('array', $o->errorLabelsContain);
9199
assertContainsOnly('string', $o->errorLabelsContain);
92-
$o->includedLabels = $o->errorLabelsContain;
100+
$this->includedLabels = $o->errorLabelsContain;
93101
}
94102

95103
if (isset($o->errorLabelsOmit)) {
96104
assertInternalType('array', $o->errorLabelsOmit);
97105
assertContainsOnly('string', $o->errorLabelsOmit);
98-
$o->excludedLabels = $o->errorLabelsOmit;
99-
}
100-
101-
if (isset($o->expectedResult)) {
102-
$o->expectedResult = new ExpectedResult($o->expectResult);
106+
$this->excludedLabels = $o->errorLabelsOmit;
103107
}
104-
}
105-
106-
public static function fromOperation(stdClass $o) : self
107-
{
108-
if (! isset($o->expectError)) {
109-
$expectedError = new self();
110-
$expectedError->isError = false;
111-
112-
return $expectedError;
113-
}
114-
115-
$expectedError = new self($o->expectError);
116108

117-
if (isset($o->expectError->expectResult)) {
118-
$o->expectResult = ExpectedResult::fromOperation($o);
109+
if (property_exists($o, 'expectResult')) {
110+
$this->expectedResult = new ExpectedResult($context, $o);
119111
}
120112
}
121113

0 commit comments

Comments
 (0)