Skip to content

Commit 9b97ee4

Browse files
committed
Fix MatchesTest and make EntityMap optional
1 parent 91e39bf commit 9b97ee4

File tree

2 files changed

+23
-36
lines changed

2 files changed

+23
-36
lines changed

tests/UnifiedSpecTests/Constraint/Matches.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Matches extends Constraint
6666
public function __construct($value, EntityMap $entityMap = null, $allowExtraRootKeys = true, $allowOperators = true)
6767
{
6868
$this->value = self::prepare($value);
69-
$this->entityMap = $entityMap ?? new EntityMap();
69+
$this->entityMap = $entityMap;
7070
$this->allowExtraRootKeys = $allowExtraRootKeys;
7171
$this->allowOperators = $allowOperators;
7272
$this->comparatorFactory = Factory::getInstance();
@@ -253,6 +253,7 @@ private function assertMatchesOperator(BSONDocument $operator, $actual, string $
253253
}
254254

255255
if ($name === '$$matchesEntity') {
256+
assertNotNull($this->entityMap, '$$matchesEntity requires EntityMap');
256257
assertInternalType('string', $operator['$$matchesEntity'], '$$matchesEntity requires string');
257258

258259
/* TODO: Consider including the entity ID in any error message to
@@ -297,6 +298,7 @@ private function assertMatchesOperator(BSONDocument $operator, $actual, string $
297298
}
298299

299300
if ($name === '$$sessionLsid') {
301+
assertNotNull($this->entityMap, '$$sessionLsid requires EntityMap');
300302
assertInternalType('string', $operator['$$sessionLsid'], '$$sessionLsid requires string');
301303
$lsid = $this->entityMap->getLogicalSessionId($operator['$$sessionLsid']);
302304

tests/UnifiedSpecTests/Constraint/MatchesTest.php

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,13 @@ public function testOperatorMatchesEntity()
9797

9898
public function testOperatorMatchesHexBytes()
9999
{
100-
$stream1 = fopen('php://temp', 'w+b');
101-
fwrite($stream1, hex2bin('DEADBEEF'));
102-
rewind($stream1);
103-
104-
$stream2 = fopen('php://temp', 'w+b');
105-
fwrite($stream2, hex2bin('90ABCDEF'));
106-
rewind($stream2);
107-
108100
$c = new Matches(['$$matchesHexBytes' => 'DEADBEEF']);
109-
$this->assertResult(true, $c, $stream1, 'value matches hex bytes (root-level)');
110-
$this->assertResult(false, $c, $stream2, 'value does not match hex bytes (root-level)');
101+
$this->assertResult(true, $c, hex2bin('DEADBEEF'), 'value matches hex bytes (root-level)');
102+
$this->assertResult(false, $c, hex2bin('90ABCDEF'), 'value does not match hex bytes (root-level)');
111103

112104
$c = new Matches(['x' => ['$$matchesHexBytes' => '90ABCDEF']]);
113-
$this->assertResult(true, $c, ['x' => $stream2], 'value matches hex bytes (embedded)');
114-
$this->assertResult(false, $c, ['x' => $stream1], 'value does not match hex bytes (embedded)');
105+
$this->assertResult(true, $c, ['x' => hex2bin('90ABCDEF')], 'value matches hex bytes (embedded)');
106+
$this->assertResult(false, $c, ['x' => hex2bin('DEADBEEF')], 'value does not match hex bytes (embedded)');
115107
}
116108

117109
public function testOperatorUnsetOrMatches()
@@ -174,14 +166,14 @@ public function errorMessageProvider()
174166
{
175167
return [
176168
'assertEquals: type check (root-level)' => [
177-
'string is not expected type "integer"',
178-
new Matches(1),
179-
'1',
169+
'boolean is not expected type "string"',
170+
new Matches('foo'),
171+
true,
180172
],
181173
'assertEquals: type check (embedded)' => [
182-
'Field path "x": string is not expected type "integer"',
183-
new Matches(['x' => 1]),
184-
['x' => '1'],
174+
'Field path "x": boolean is not expected type "string"',
175+
new Matches(['x' => 'foo']),
176+
['x' => true],
185177
],
186178
'assertEquals: comparison failure (root-level)' => [
187179
'Failed asserting that two strings are equal.',
@@ -254,9 +246,6 @@ public function testOperatorSyntaxValidation($expectedMessage, Matches $constrai
254246

255247
public function operatorErrorMessageProvider()
256248
{
257-
$entityMap = new EntityMap();
258-
$entityMap->set('notSession', 1);
259-
260249
return [
261250
'$$exists type' => [
262251
'$$exists requires bool',
@@ -270,13 +259,13 @@ public function operatorErrorMessageProvider()
270259
'$$type requires string or string[]',
271260
new Matches(['x' => ['$$type' => [1]]]),
272261
],
262+
'$$matchesEntity requires EntityMap' => [
263+
'$$matchesEntity requires EntityMap',
264+
new Matches(['x' => ['$$matchesEntity' => 'foo']]),
265+
],
273266
'$$matchesEntity type' => [
274267
'$$matchesEntity requires string',
275-
new Matches(['x' => ['$$matchesEntity' => 1]]),
276-
],
277-
'$$matchesEntity undefined entity' => [
278-
'No entity is defined for "undefined"',
279-
new Matches(['$$matchesEntity' => 'undefined']),
268+
new Matches(['x' => ['$$matchesEntity' => 1]], new EntityMap()),
280269
],
281270
'$$matchesHexBytes type' => [
282271
'$$matchesHexBytes requires string',
@@ -286,17 +275,13 @@ public function operatorErrorMessageProvider()
286275
'$$matchesHexBytes requires pairs of hex chars',
287276
new Matches(['$$matchesHexBytes' => 'f00']),
288277
],
278+
'$$sessionLsid requires EntityMap' => [
279+
'$$sessionLsid requires EntityMap',
280+
new Matches(['x' => ['$$sessionLsid' => 'foo']]),
281+
],
289282
'$$sessionLsid type' => [
290283
'$$sessionLsid requires string',
291-
new Matches(['x' => ['$$sessionLsid' => 1]]),
292-
],
293-
'$$sessionLsid undefined entity' => [
294-
'No entity is defined for "undefined"',
295-
new Matches(['$$sessionLsid' => 'undefined']),
296-
],
297-
'$$sessionLsid invalid entity' => [
298-
'$$sessionLsid requires session entity',
299-
new Matches(['x' => ['$$sessionLsid' => 'notSession']], $entityMap),
284+
new Matches(['x' => ['$$sessionLsid' => 1]], new EntityMap()),
300285
],
301286
];
302287
}

0 commit comments

Comments
 (0)