@@ -56,6 +56,42 @@ public function setConstructorExpectations(array $constructorExpectations)
56
56
$ this ->constructorExpectations [] = $ this ->currentConstructorExpectations ;
57
57
}
58
58
59
+ public function getConstructorExpectations ()
60
+ {
61
+ return $ this ->constructorExpectations ;
62
+ }
63
+
64
+ /**
65
+ * Returns constructor expectations array that matches the given $unit.
66
+ * Empty array otherwise.
67
+ *
68
+ * @param $unit
69
+ * @return array|mixed|void
70
+ * @throws ReflectionException
71
+ */
72
+ public function getConstructorExpectationsForInstance ($ unit )
73
+ {
74
+ foreach ($ this ->constructorExpectations as $ index => $ args ) {
75
+ $ expected = new $ unit (...$ args );
76
+
77
+ $ ref = new ReflectionClass ($ unit );
78
+
79
+ // we start by assuming that the unit instance and the $expected one are equal
80
+ // until proven otherwise when we find differences between properties.
81
+ $ isEqual = true ;
82
+ foreach ($ ref ->getProperties () as $ property ) {
83
+ if ($ property ->getValue ($ unit ) !== $ property ->getValue ($ expected )) {
84
+ $ isEqual = false ;
85
+ break ;
86
+ }
87
+ }
88
+
89
+ if ($ isEqual ) {
90
+ return $ this ->constructorExpectations [$ index ];
91
+ }
92
+ }
93
+ }
94
+
59
95
/**
60
96
* @return array
61
97
* @throws ReflectionException
@@ -111,6 +147,32 @@ private function registerMock(): UnitMock
111
147
return $ this ;
112
148
}
113
149
150
+ /**
151
+ * Compare the mock to an actual instance.
152
+ *
153
+ * @param object $unit
154
+ * @return void
155
+ * @throws Mockery\Exception\NoMatchingExpectationException
156
+ */
157
+ public function compareTo (object $ unit )
158
+ {
159
+ $ expected = array_map (fn ($ args ) => new $ unit (...$ args ), $ this ->constructorExpectations );
160
+
161
+ $ ref = new ReflectionClass ($ unit );
162
+ foreach ($ ref ->getProperties () as $ property ) {
163
+
164
+ $ expectations = array_map (fn ($ instance ) => $ property ->getValue ($ instance ), $ expected );
165
+
166
+ if (!in_array ($ property ->getValue ($ unit ), $ expectations )) {
167
+ throw new Mockery \Exception \NoMatchingExpectationException (
168
+ "Mismatch in \${$ property ->getName ()} when running {$ this ->unit } \n\n--- Expected (one of) \n" .
169
+ print_r (join ("\n" , array_map (fn ($ instance ) => $ property ->getValue ($ instance ), $ expected )), true ).
170
+ "\n\n+++Actual: \n" .print_r ($ property ->getValue ($ unit ), true )."\n\n"
171
+ );
172
+ }
173
+ }
174
+ }
175
+
114
176
public function getMock (): MockInterface
115
177
{
116
178
$ this ->registerMock ();
0 commit comments