Skip to content

Commit 6f76ff3

Browse files
committed
PHPLIB-1001: Support "number" alias in IsBsonType constraint
Also revises instantiation of deprecated BSON types and fixes an assertion message.
1 parent 356b89c commit 6f76ff3

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

tests/UnifiedSpecTests/Constraint/IsBsonType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ final class IsBsonType extends Constraint
6767
'decimal',
6868
'minKey',
6969
'maxKey',
70+
'number',
7071
];
7172

7273
/** @var string */
@@ -167,6 +168,9 @@ private function doMatches($other): bool
167168
case 'maxKey':
168169
return $other instanceof MaxKeyInterface;
169170

171+
case 'number':
172+
return is_int($other) || (PHP_INT_SIZE == 4 && $other instanceof Int64) || is_float($other) || $other instanceof Decimal128Interface;
173+
170174
default:
171175
// This should already have been caught in the constructor
172176
throw new LogicException('Unsupported type: ' . $this->type);

tests/UnifiedSpecTests/Constraint/IsBsonTypeTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ public function testConstraint($type, $value): void
3838

3939
public function provideTypes()
4040
{
41-
$undefined = toPHP(fromJSON('{ "undefined": {"$undefined": true} }'));
42-
$symbol = toPHP(fromJSON('{ "symbol": {"$symbol": "test"} }'));
43-
$dbPointer = toPHP(fromJSON('{ "dbPointer": {"$dbPointer": {"$ref": "phongo.test", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }'));
41+
$undefined = toPHP(fromJSON('{ "x": {"$undefined": true} }'))->x;
42+
$symbol = toPHP(fromJSON('{ "x": {"$symbol": "test"} }'))->x;
43+
$dbPointer = toPHP(fromJSON('{ "x": {"$dbPointer": {"$ref": "db.coll", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }'))->x;
44+
$long = PHP_INT_SIZE == 4 ? unserialize('C:18:"MongoDB\BSON\Int64":38:{a:1:{s:7:"integer";s:10:"4294967296";}}') : 4294967296;
4445

4546
return [
4647
'double' => ['double', 1.4],
@@ -52,22 +53,26 @@ public function provideTypes()
5253
'array(indexed array)' => ['array', ['foo']],
5354
'array(BSONArray)' => ['array', new BSONArray()],
5455
'binData' => ['binData', new Binary('', 0)],
55-
'undefined' => ['undefined', $undefined->undefined],
56+
'undefined' => ['undefined', $undefined],
5657
'objectId' => ['objectId', new ObjectId()],
5758
'bool' => ['bool', true],
5859
'date' => ['date', new UTCDateTime()],
5960
'null' => ['null', null],
6061
'regex' => ['regex', new Regex('.*')],
61-
'dbPointer' => ['dbPointer', $dbPointer->dbPointer],
62+
'dbPointer' => ['dbPointer', $dbPointer],
6263
'javascript' => ['javascript', new Javascript('foo = 1;')],
63-
'symbol' => ['symbol', $symbol->symbol],
64+
'symbol' => ['symbol', $symbol],
6465
'javascriptWithScope' => ['javascriptWithScope', new Javascript('foo = 1;', ['x' => 1])],
6566
'int' => ['int', 1],
6667
'timestamp' => ['timestamp', new Timestamp(0, 0)],
67-
'long' => ['long', PHP_INT_SIZE == 4 ? unserialize('C:18:"MongoDB\BSON\Int64":38:{a:1:{s:7:"integer";s:10:"4294967296";}}') : 4294967296],
68+
'long' => ['long', $long],
6869
'decimal' => ['decimal', new Decimal128('18446744073709551616')],
6970
'minKey' => ['minKey', new MinKey()],
7071
'maxKey' => ['maxKey', new MaxKey()],
72+
'number(double)' => ['number', 1.4],
73+
'number(decimal)' => ['number', new Decimal128('18446744073709551616')],
74+
'number(int)' => ['number', 1],
75+
'number(long)' => ['number', $long],
7176
];
7277
}
7378

@@ -89,10 +94,20 @@ public function testAnyOf(): void
8994
$c = IsBsonType::anyOf('double', 'int');
9095

9196
$this->assertResult(true, $c, 1, 'int is double or int');
92-
$this->assertResult(true, $c, 1.4, 'int is double or int');
97+
$this->assertResult(true, $c, 1.4, 'double is double or int');
9398
$this->assertResult(false, $c, 'foo', 'string is not double or int');
9499
}
95100

101+
public function testAnyOfWithNumberAlias(): void
102+
{
103+
$c = IsBsonType::anyOf('number', 'string');
104+
105+
$this->assertResult(true, $c, 1, 'int is number or string');
106+
$this->assertResult(true, $c, 1.4, 'double is number or string');
107+
$this->assertResult(true, $c, 'foo', 'string is number or string');
108+
$this->assertResult(false, $c, true, 'bool is not number or string');
109+
}
110+
96111
public function testErrorMessage(): void
97112
{
98113
$c = new IsBsonType('string');

0 commit comments

Comments
 (0)