Skip to content

PHPLIB-459: Support better failure descriptions in DocumentsMatchConstraint #661

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 1 commit into from
Aug 20, 2019

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Aug 5, 2019

https://jira.mongodb.org/browse/PHPLIB-459

This improves the error messages for the above constraint by no longer just printing the expected and actual object, but also by giving a reason for the failure and showing a diff. Comparison follows.

Instead of simply comparing values, this uses sebastian/comparator internally to compare object values and uses its exceptions to generate failure messages.


Here's an actual comparison failure before:

Failed asserting that stdClass Object &000000001f899da5000000004f623c22 (
    'update' => 'test'
    'ordered' => true
    '$db' => 'crud-tests'
    'lsid' => stdClass Object &000000001f899dc2000000004f623c22 (
        'id' => MongoDB\BSON\Binary Object &000000001f899daa000000004f623c22 (
            'data' => '(?z??A??-y{?T?C'
            'type' => 4
        )
    )
    'updates' => Array &0 (
        0 => stdClass Object &000000001f899da1000000004f623c22 (
            'q' => stdClass Object &000000001f899da9000000004f623c22 ()
            'u' => stdClass Object &000000001f899d9b000000004f623c22 (
                '$set' => stdClass Object &000000001f899dad000000004f623c22 (
                    'y.$[i].b' => 2
                )
            )
            'upsert' => false
            'multi' => false
            'arrayFilters' => stdClass Object &000000001f899db0000000004f623c22 (
                0 => stdClass Object &000000001f899db2000000004f623c22 (
                    'i.b' => 3
                )
            )
        )
        1 => stdClass Object &000000001f899db3000000004f623c22 (
            'q' => stdClass Object &000000001f899da3000000004f623c22 ()
            'u' => stdClass Object &000000001f899d95000000004f623c22 (
                '$set' => stdClass Object &000000001f899db7000000004f623c22 (
                    'y.$[i].b' => 2
                )
            )
            'upsert' => false
            'multi' => true
            'arrayFilters' => stdClass Object &000000001f899da7000000004f623c22 (
                0 => stdClass Object &000000001f899d9d000000004f623c22 (
                    'i.b' => 1
                )
            )
        )
    )
) matches {"update":"test","updates":[{"q":{},"u":{"$set":{"y.$[i].b":2}},"arrayFilters":[{"i.b":3}]},{"q":{},"u":{"$set":{"y.$[i].b":2}},"multi":true,"arrayFilters":[{"i.b":1}]}],"ordered":true}.

and the same failure after the change:

Failed asserting that two BSON objects are equal.
Field path "updates.0.arrayFilters": MongoDB\Model\BSONDocument Object (...) is not instance of expected class "MongoDB\Model\BSONArray".
--- Expected
+++ Actual
@@ @@
-MongoDB\Model\BSONDocument Object &0000000077a2cf6c00000000788c65fd (
+MongoDB\Model\BSONDocument Object &0000000077a2cf5d00000000788c65fd (
     'update' => 'test'
-    'updates' => MongoDB\Model\BSONArray Object &0000000077a2cf6d00000000788c65fd (
-        0 => MongoDB\Model\BSONDocument Object &0000000077a2cf5c00000000788c65fd (
-            'q' => MongoDB\Model\BSONDocument Object &0000000077a2cf6200000000788c65fd ()
-            'u' => MongoDB\Model\BSONDocument Object &0000000077a2cf6300000000788c65fd (
-                '$set' => MongoDB\Model\BSONDocument Object &0000000077a2cf6000000000788c65fd (
+    'ordered' => true
+    '$db' => 'crud-tests'
+    'lsid' => MongoDB\Model\BSONDocument Object &0000000077a2cf5200000000788c65fd (
+        'id' => MongoDB\BSON\Binary Object &0000000077a2cf5000000000788c65fd (
+            'data' => Binary String: 0xab912072076f4d8abf8eaf4988f954ea
+            'type' => 4
+        )
+    )
+    'updates' => MongoDB\Model\BSONArray Object &0000000077a2cf6500000000788c65fd (
+        0 => MongoDB\Model\BSONDocument Object &0000000077a2cf7c00000000788c65fd (
+            'q' => MongoDB\Model\BSONDocument Object &0000000077a2cf7700000000788c65fd ()
+            'u' => MongoDB\Model\BSONDocument Object &0000000077a2cf7500000000788c65fd (
+                '$set' => MongoDB\Model\BSONDocument Object &0000000077a2cf7300000000788c65fd (
                     'y.$[i].b' => 2
                 )
             )
-            'arrayFilters' => MongoDB\Model\BSONArray Object &0000000077a2cf6400000000788c65fd (
-                0 => MongoDB\Model\BSONDocument Object &0000000077a2cf6100000000788c65fd (
+            'upsert' => false
+            'multi' => false
+            'arrayFilters' => MongoDB\Model\BSONDocument Object &0000000077a2cf7200000000788c65fd (
+                0 => MongoDB\Model\BSONDocument Object &0000000077a2cf7000000000788c65fd (
                     'i.b' => 3
                 )
             )
         )
-        1 => MongoDB\Model\BSONDocument Object &0000000077a2cf6800000000788c65fd (
-            'q' => MongoDB\Model\BSONDocument Object &0000000077a2cf6900000000788c65fd ()
-            'u' => MongoDB\Model\BSONDocument Object &0000000077a2cf7f00000000788c65fd (
-                '$set' => MongoDB\Model\BSONDocument Object &0000000077a2cf7a00000000788c65fd (
+        1 => MongoDB\Model\BSONDocument Object &0000000077a2cf7900000000788c65fd (
+            'q' => MongoDB\Model\BSONDocument Object &0000000077a2cf7400000000788c65fd ()
+            'u' => MongoDB\Model\BSONDocument Object &0000000077a2cf8e00000000788c65fd (
+                '$set' => MongoDB\Model\BSONDocument Object &0000000077a2cf8d00000000788c65fd (
                     'y.$[i].b' => 2
                 )
             )
+            'upsert' => false
             'multi' => true
-            'arrayFilters' => MongoDB\Model\BSONArray Object &0000000077a2cf7d00000000788c65fd (
-                0 => MongoDB\Model\BSONDocument Object &0000000077a2cf7b00000000788c65fd (
+            'arrayFilters' => MongoDB\Model\BSONDocument Object &0000000077a2cf8c00000000788c65fd (
+                0 => MongoDB\Model\BSONDocument Object &0000000077a2cf8a00000000788c65fd (
                     'i.b' => 1
                 )
             )
         )
     )
-    'ordered' => true

@alcaeus alcaeus self-assigned this Aug 5, 2019
@alcaeus alcaeus requested a review from kvwalker August 5, 2019 14:57
@alcaeus alcaeus force-pushed the phplib-459 branch 2 times, most recently from f0d3aa5 to 9f35439 Compare August 6, 2019 05:39
@alcaeus alcaeus requested review from jmikola and removed request for kvwalker August 9, 2019 05:23
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a great improvement!

@@ -16,7 +19,7 @@ public function testIgnoreExtraKeysInRoot()
$this->assertResult(false, $c, ['x' => 1, 'y' => ['a' => 1, 'b' => 2, 'c' => 3]], 'Extra keys in embedded are not permitted');
$this->assertResult(true, $c, ['y' => ['b' => 2, 'a' => 1], 'x' => 1], 'Root and embedded key order is not significant');

// Arrays are always intepretted as root documents
// Arrays are always interpreted as root documents
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel so ashamed. Thank you for catching this :)

@alcaeus alcaeus force-pushed the phplib-459 branch 2 times, most recently from da9fcd7 to 4550f89 Compare August 20, 2019 09:48
alcaeus added a commit that referenced this pull request Aug 20, 2019
@alcaeus alcaeus merged commit 016eb3e into mongodb:master Aug 20, 2019
@alcaeus alcaeus deleted the phplib-459 branch August 20, 2019 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants