Skip to content

Commit 6cb886e

Browse files
committed
PHPLIB-1010: Relax acceptance of Int64 for $$type operators
1 parent 691f74a commit 6cb886e

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

tests/SpecTests/DocumentsMatchConstraint.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,10 @@ private function assertBSONType(string $expectedType, $actualValue): void
246246
return;
247247

248248
case 'long':
249-
if (PHP_INT_SIZE == 4) {
250-
(new IsInstanceOf(Int64::class))->evaluate($actualValue);
251-
} else {
252-
(new IsType('int'))->evaluate($actualValue);
253-
}
249+
LogicalOr::fromConstraints(
250+
new IsType('int'),
251+
new IsInstanceOf(Int64::class)
252+
)->evaluate($actualValue);
254253

255254
return;
256255

tests/SpecTests/DocumentsMatchConstraintTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,31 @@ public function testBSONTypeAssertions($type, $value): void
8484

8585
public function provideBSONTypes()
8686
{
87-
$undefined = toPHP(fromJSON('{ "undefined": {"$undefined": true} }'));
88-
$symbol = toPHP(fromJSON('{ "symbol": {"$symbol": "test"} }'));
89-
$dbPointer = toPHP(fromJSON('{ "dbPointer": {"$dbPointer": {"$ref": "phongo.test", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }'));
87+
$undefined = toPHP(fromJSON('{ "x": {"$undefined": true} }'))->x;
88+
$symbol = toPHP(fromJSON('{ "x": {"$symbol": "test"} }'))->x;
89+
$dbPointer = toPHP(fromJSON('{ "x": {"$dbPointer": {"$ref": "db.coll", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }'))->x;
90+
$int64 = unserialize('C:18:"MongoDB\BSON\Int64":28:{a:1:{s:7:"integer";s:1:"1";}}');
91+
$long = PHP_INT_SIZE == 4 ? unserialize('C:18:"MongoDB\BSON\Int64":38:{a:1:{s:7:"integer";s:10:"4294967296";}}') : 4294967296;
9092

9193
return [
9294
'double' => ['double', 1.4],
9395
'string' => ['string', 'foo'],
9496
'object' => ['object', new BSONDocument()],
9597
'array' => ['array', ['foo']],
9698
'binData' => ['binData', new Binary('', 0)],
97-
'undefined' => ['undefined', $undefined->undefined],
99+
'undefined' => ['undefined', $undefined],
98100
'objectId' => ['objectId', new ObjectId()],
99101
'boolean' => ['boolean', true],
100102
'date' => ['date', new UTCDateTime()],
101103
'null' => ['null', null],
102104
'regex' => ['regex', new Regex('.*')],
103-
'dbPointer' => ['dbPointer', $dbPointer->dbPointer],
105+
'dbPointer' => ['dbPointer', $dbPointer],
104106
'javascript' => ['javascript', new Javascript('foo = 1;')],
105-
'symbol' => ['symbol', $symbol->symbol],
107+
'symbol' => ['symbol', $symbol],
106108
'int' => ['int', 1],
107109
'timestamp' => ['timestamp', new Timestamp(0, 0)],
108-
'long' => ['long', PHP_INT_SIZE == 4 ? unserialize('C:18:"MongoDB\BSON\Int64":38:{a:1:{s:7:"integer";s:10:"4294967296";}}') : 4294967296],
110+
'long(int64)' => ['long', $int64],
111+
'long(long)' => ['long', $long],
109112
'decimal' => ['decimal', new Decimal128('18446744073709551616')],
110113
'minKey' => ['minKey', new MinKey()],
111114
'maxKey' => ['maxKey', new MaxKey()],

tests/UnifiedSpecTests/Constraint/IsBsonType.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
use function range;
3939
use function sprintf;
4040

41-
use const PHP_INT_SIZE;
42-
4341
final class IsBsonType extends Constraint
4442
{
4543
use ConstraintTrait;
@@ -153,11 +151,7 @@ private function doMatches($other): bool
153151
return $other instanceof TimestampInterface;
154152

155153
case 'long':
156-
if (PHP_INT_SIZE == 4) {
157-
return $other instanceof Int64;
158-
}
159-
160-
return is_int($other);
154+
return is_int($other) || $other instanceof Int64;
161155

162156
case 'decimal':
163157
return $other instanceof Decimal128Interface;
@@ -169,7 +163,7 @@ private function doMatches($other): bool
169163
return $other instanceof MaxKeyInterface;
170164

171165
case 'number':
172-
return is_int($other) || (PHP_INT_SIZE == 4 && $other instanceof Int64) || is_float($other) || $other instanceof Decimal128Interface;
166+
return is_int($other) || $other instanceof Int64 || is_float($other) || $other instanceof Decimal128Interface;
173167

174168
default:
175169
// This should already have been caught in the constructor

tests/UnifiedSpecTests/Constraint/IsBsonTypeTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function provideTypes()
4141
$undefined = toPHP(fromJSON('{ "x": {"$undefined": true} }'))->x;
4242
$symbol = toPHP(fromJSON('{ "x": {"$symbol": "test"} }'))->x;
4343
$dbPointer = toPHP(fromJSON('{ "x": {"$dbPointer": {"$ref": "db.coll", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }'))->x;
44+
$int64 = unserialize('C:18:"MongoDB\BSON\Int64":28:{a:1:{s:7:"integer";s:1:"1";}}');
4445
$long = PHP_INT_SIZE == 4 ? unserialize('C:18:"MongoDB\BSON\Int64":38:{a:1:{s:7:"integer";s:10:"4294967296";}}') : 4294967296;
4546

4647
return [
@@ -65,13 +66,15 @@ public function provideTypes()
6566
'javascriptWithScope' => ['javascriptWithScope', new Javascript('foo = 1;', ['x' => 1])],
6667
'int' => ['int', 1],
6768
'timestamp' => ['timestamp', new Timestamp(0, 0)],
68-
'long' => ['long', $long],
69+
'long(int64)' => ['long', $int64],
70+
'long(long)' => ['long', $long],
6971
'decimal' => ['decimal', new Decimal128('18446744073709551616')],
7072
'minKey' => ['minKey', new MinKey()],
7173
'maxKey' => ['maxKey', new MaxKey()],
7274
'number(double)' => ['number', 1.4],
7375
'number(decimal)' => ['number', new Decimal128('18446744073709551616')],
7476
'number(int)' => ['number', 1],
77+
'number(int64)' => ['number', $int64],
7578
'number(long)' => ['number', $long],
7679
];
7780
}

0 commit comments

Comments
 (0)