77use Closure ;
88use PHPUnit \Framework \MockObject \Rule \InvokedAtLeastOnce ;
99use PHPUnit \Framework \MockObject \Rule \InvokedCount ;
10+ use PHPUnit \Framework \MockObject \Stub \ReturnCallback ;
11+ use PHPUnit \Framework \MockObject \Stub \Stub ;
1012
1113class Expected
1214{
@@ -33,7 +35,7 @@ public static function never($params = null): StubMarshaler
3335 {
3436 return new StubMarshaler (
3537 new InvokedCount (0 ),
36- self ::closureIfNull ($ params )
38+ self ::stubIfClosure ($ params )
3739 );
3840 }
3941
@@ -65,13 +67,20 @@ public static function never($params = null): StubMarshaler
6567 * Expected::once(function() { return Faker::name(); });
6668 * ```
6769 *
70+ * PHPUnit Stub can also be passed as parameter:
71+ *
72+ * ```php
73+ * <?php
74+ * Expected::exactly(3, new ReturnSelf());
75+ * ```
76+ *
6877 * @param mixed $params
6978 */
7079 public static function once ($ params = null ): StubMarshaler
7180 {
7281 return new StubMarshaler (
7382 new InvokedCount (1 ),
74- self ::closureIfNull ($ params )
83+ self ::stubIfClosure ($ params )
7584 );
7685 }
7786
@@ -103,14 +112,28 @@ public static function once($params = null): StubMarshaler
103112 * <?php
104113 * Expected::atLeastOnce(function() { return Faker::name(); });
105114 * ```
115+ *
116+ * ConsecutiveMap can also be passed as parameter:
117+ *
118+ * ```php
119+ * <?php
120+ * Expected::exactly(3, Stub::consecutive(1,2,3));
121+ * ```
122+ *
123+ * PHPUnit Stub can also be passed as parameter:
124+ *
125+ * ```php
126+ * <?php
127+ * Expected::exactly(3, new ReturnSelf());
128+ * ```
106129 *
107130 * @param mixed $params
108131 */
109132 public static function atLeastOnce ($ params = null ): StubMarshaler
110133 {
111134 return new StubMarshaler (
112135 new InvokedAtLeastOnce (),
113- self ::closureIfNull ($ params )
136+ self ::stubIfClosure ($ params )
114137 );
115138 }
116139
@@ -145,17 +168,40 @@ public static function atLeastOnce($params = null): StubMarshaler
145168 * <?php
146169 * Expected::exactly(function() { return Faker::name() });
147170 * ```
171+ *
172+ * ConsecutiveMap can also be passed as parameter:
173+ *
174+ * ```php
175+ * <?php
176+ * Expected::exactly(3, Stub::consecutive(1,2,3));
177+ * ```
178+ *
179+ * PHPUnit Stub can also be passed as parameter:
180+ *
181+ * ```php
182+ * <?php
183+ * Expected::exactly(3, new ReturnSelf());
184+ * ```
148185 *
149186 * @param mixed $params
150187 */
151188 public static function exactly (int $ count , $ params = null ): StubMarshaler
152189 {
153190 return new StubMarshaler (
154191 new InvokedCount ($ count ),
155- self ::closureIfNull ($ params )
192+ self ::stubIfClosure ($ params )
156193 );
157194 }
158195
196+ private static function stubIfClosure ($ params ) : Stub
197+ {
198+ if ($ params instanceof Stub) {
199+ return $ params ;
200+ }
201+
202+ return new ReturnCallback (self ::closureIfNull ($ params ));
203+ }
204+
159205 private static function closureIfNull ($ params ): Closure
160206 {
161207 if ($ params instanceof Closure) {
0 commit comments