99use InvalidArgumentException ;
1010use RuntimeException ;
1111use stdClass ;
12+ use Symfony \Bridge \PhpUnit \ConstraintTrait ;
1213
1314/**
1415 * Constraint that checks if one document matches another.
1718 */
1819class DocumentsMatchConstraint extends Constraint
1920{
21+ use ConstraintTrait;
22+
2023 private $ ignoreExtraKeysInRoot = false ;
2124 private $ ignoreExtraKeysInEmbedded = false ;
2225 private $ placeholders = [];
@@ -38,50 +41,12 @@ class DocumentsMatchConstraint extends Constraint
3841 */
3942 public function __construct ($ value , $ ignoreExtraKeysInRoot = false , $ ignoreExtraKeysInEmbedded = false , array $ placeholders = [])
4043 {
41- parent ::__construct ();
4244 $ this ->value = $ this ->prepareBSON ($ value , true , $ this ->sortKeys );
4345 $ this ->ignoreExtraKeysInRoot = $ ignoreExtraKeysInRoot ;
4446 $ this ->ignoreExtraKeysInEmbedded = $ ignoreExtraKeysInEmbedded ;
4547 $ this ->placeholders = $ placeholders ;
4648 }
4749
48- /**
49- * Returns a string representation of the constraint.
50- *
51- * @return string
52- */
53- public function toString ()
54- {
55- return 'matches ' . json_encode ($ this ->value );
56- }
57-
58- /**
59- * Evaluates the constraint for parameter $other. Returns true if the
60- * constraint is met, false otherwise.
61- *
62- * @param mixed $other
63- * @return boolean
64- */
65- protected function matches ($ other )
66- {
67- /* TODO: If ignoreExtraKeys and sortKeys are both false, then we may be
68- * able to skip preparation, convert both documents to extended JSON,
69- * and compare strings.
70- *
71- * If ignoreExtraKeys is false and sortKeys is true, we still be able to
72- * compare JSON strings but will still require preparation to sort keys
73- * in all documents and sub-documents. */
74- $ other = $ this ->prepareBSON ($ other , true , $ this ->sortKeys );
75-
76- try {
77- $ this ->assertEquals ($ this ->value , $ other , $ this ->ignoreExtraKeysInRoot );
78- } catch (RuntimeException $ e ) {
79- return false ;
80- }
81-
82- return true ;
83- }
84-
8550 /**
8651 * Compares two documents recursively.
8752 *
@@ -131,6 +96,31 @@ private function assertEquals(ArrayObject $expected, ArrayObject $actual, $ignor
13196 }
13297 }
13398
99+ private function doMatches ($ other )
100+ {
101+ /* TODO: If ignoreExtraKeys and sortKeys are both false, then we may be
102+ * able to skip preparation, convert both documents to extended JSON,
103+ * and compare strings.
104+ *
105+ * If ignoreExtraKeys is false and sortKeys is true, we still be able to
106+ * compare JSON strings but will still require preparation to sort keys
107+ * in all documents and sub-documents. */
108+ $ other = $ this ->prepareBSON ($ other , true , $ this ->sortKeys );
109+
110+ try {
111+ $ this ->assertEquals ($ this ->value , $ other , $ this ->ignoreExtraKeysInRoot );
112+ } catch (RuntimeException $ e ) {
113+ return false ;
114+ }
115+
116+ return true ;
117+ }
118+
119+ private function doToString ()
120+ {
121+ return 'matches ' . json_encode ($ this ->value );
122+ }
123+
134124 /**
135125 * Prepare a BSON document or array for comparison.
136126 *
0 commit comments