9
9
use InvalidArgumentException ;
10
10
use RuntimeException ;
11
11
use stdClass ;
12
+ use Symfony \Bridge \PhpUnit \ConstraintTrait ;
12
13
13
14
/**
14
15
* Constraint that checks if one document matches another.
17
18
*/
18
19
class DocumentsMatchConstraint extends Constraint
19
20
{
21
+ use ConstraintTrait;
22
+
20
23
private $ ignoreExtraKeysInRoot = false ;
21
24
private $ ignoreExtraKeysInEmbedded = false ;
22
25
private $ placeholders = [];
@@ -38,50 +41,12 @@ class DocumentsMatchConstraint extends Constraint
38
41
*/
39
42
public function __construct ($ value , $ ignoreExtraKeysInRoot = false , $ ignoreExtraKeysInEmbedded = false , array $ placeholders = [])
40
43
{
41
- parent ::__construct ();
42
44
$ this ->value = $ this ->prepareBSON ($ value , true , $ this ->sortKeys );
43
45
$ this ->ignoreExtraKeysInRoot = $ ignoreExtraKeysInRoot ;
44
46
$ this ->ignoreExtraKeysInEmbedded = $ ignoreExtraKeysInEmbedded ;
45
47
$ this ->placeholders = $ placeholders ;
46
48
}
47
49
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
-
85
50
/**
86
51
* Compares two documents recursively.
87
52
*
@@ -131,6 +96,31 @@ private function assertEquals(ArrayObject $expected, ArrayObject $actual, $ignor
131
96
}
132
97
}
133
98
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
+
134
124
/**
135
125
* Prepare a BSON document or array for comparison.
136
126
*
0 commit comments