-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1001, PHPLIB-1010, PHPLIB-1011: Improvements to $$type operator #991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
691f74a
PHPLIB-1001: Support "number" alias in IsBsonType constraint
jmikola 6cb886e
PHPLIB-1010: Relax acceptance of Int64 for $$type operators
jmikola 61c17de
Remove code path for unused PHPUnit versions
jmikola 7c4eac6
PHPLIB-1001: Support "number" alias for legacy $$type operator
jmikola 77a5c0f
PHPLIB-1011: Support asserting multiple types in legacy $$type operator
jmikola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,9 +38,11 @@ public function testConstraint($type, $value): void | |
|
||
public function provideTypes() | ||
{ | ||
$undefined = toPHP(fromJSON('{ "undefined": {"$undefined": true} }')); | ||
$symbol = toPHP(fromJSON('{ "symbol": {"$symbol": "test"} }')); | ||
$dbPointer = toPHP(fromJSON('{ "dbPointer": {"$dbPointer": {"$ref": "phongo.test", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }')); | ||
$undefined = toPHP(fromJSON('{ "x": {"$undefined": true} }'))->x; | ||
$symbol = toPHP(fromJSON('{ "x": {"$symbol": "test"} }'))->x; | ||
$dbPointer = toPHP(fromJSON('{ "x": {"$dbPointer": {"$ref": "db.coll", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }'))->x; | ||
$int64 = unserialize('C:18:"MongoDB\BSON\Int64":28:{a:1:{s:7:"integer";s:1:"1";}}'); | ||
$long = PHP_INT_SIZE == 4 ? unserialize('C:18:"MongoDB\BSON\Int64":38:{a:1:{s:7:"integer";s:10:"4294967296";}}') : 4294967296; | ||
|
||
return [ | ||
'double' => ['double', 1.4], | ||
|
@@ -52,22 +54,28 @@ public function provideTypes() | |
'array(indexed array)' => ['array', ['foo']], | ||
'array(BSONArray)' => ['array', new BSONArray()], | ||
'binData' => ['binData', new Binary('', 0)], | ||
'undefined' => ['undefined', $undefined->undefined], | ||
'undefined' => ['undefined', $undefined], | ||
'objectId' => ['objectId', new ObjectId()], | ||
'bool' => ['bool', true], | ||
'date' => ['date', new UTCDateTime()], | ||
'null' => ['null', null], | ||
'regex' => ['regex', new Regex('.*')], | ||
'dbPointer' => ['dbPointer', $dbPointer->dbPointer], | ||
'dbPointer' => ['dbPointer', $dbPointer], | ||
'javascript' => ['javascript', new Javascript('foo = 1;')], | ||
'symbol' => ['symbol', $symbol->symbol], | ||
'symbol' => ['symbol', $symbol], | ||
'javascriptWithScope' => ['javascriptWithScope', new Javascript('foo = 1;', ['x' => 1])], | ||
'int' => ['int', 1], | ||
'timestamp' => ['timestamp', new Timestamp(0, 0)], | ||
'long' => ['long', PHP_INT_SIZE == 4 ? unserialize('C:18:"MongoDB\BSON\Int64":38:{a:1:{s:7:"integer";s:10:"4294967296";}}') : 4294967296], | ||
'long(int64)' => ['long', $int64], | ||
'long(long)' => ['long', $long], | ||
'decimal' => ['decimal', new Decimal128('18446744073709551616')], | ||
'minKey' => ['minKey', new MinKey()], | ||
'maxKey' => ['maxKey', new MaxKey()], | ||
'number(double)' => ['number', 1.4], | ||
'number(decimal)' => ['number', new Decimal128('18446744073709551616')], | ||
'number(int)' => ['number', 1], | ||
'number(int64)' => ['number', $int64], | ||
'number(long)' => ['number', $long], | ||
]; | ||
} | ||
|
||
|
@@ -89,10 +97,20 @@ public function testAnyOf(): void | |
$c = IsBsonType::anyOf('double', 'int'); | ||
|
||
$this->assertResult(true, $c, 1, 'int is double or int'); | ||
$this->assertResult(true, $c, 1.4, 'int is double or int'); | ||
$this->assertResult(true, $c, 1.4, 'double is double or int'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||
$this->assertResult(false, $c, 'foo', 'string is not double or int'); | ||
} | ||
|
||
public function testAnyOfWithNumberAlias(): void | ||
{ | ||
$c = IsBsonType::anyOf('number', 'string'); | ||
|
||
$this->assertResult(true, $c, 1, 'int is number or string'); | ||
$this->assertResult(true, $c, 1.4, 'double is number or string'); | ||
$this->assertResult(true, $c, 'foo', 'string is number or string'); | ||
$this->assertResult(false, $c, true, 'bool is not number or string'); | ||
} | ||
|
||
public function testErrorMessage(): void | ||
{ | ||
$c = new IsBsonType('string'); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our tests make the best case for providing a constructor for
Int64
. Can't wait to eventually get rid of it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember that a constructor would provide users a foot-gun allowing them to force 32-bit values to serialize as 64-bit, only to have BSON decoding convert them back to basic integers. Previous discussions on this:
Unless we implement some BSON codec API that allows users to coerce integer serialization (e.g. always use 64-bit BSON types), I think the current behavior is less likely to confuse folks while noting we can't please everyone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't suggesting we add a constructor - apologies if this was misunderstood. I was alluding to getting rid of Int64 if and when we drop support for 32-bit platforms. It's wishful thinking at this point, but I hope that x86 goes out of style sometime this century ^^