22
33namespace MintyPHP \Mocking ;
44
5+ use PHPUnit \Framework \TestCase ;
6+
57class StaticMethodMock
68{
79 /** @var ?callable */
@@ -10,13 +12,16 @@ class StaticMethodMock
1012 public static array $ mocks = [];
1113 /** @var string */
1214 private string $ className ;
15+ /** @var TestCase */
16+ private TestCase $ testCase ;
1317 /** @var array<int,array{method:string,arguments:array<int,mixed>,returns:mixed,exception:?\Throwable}> $expectations*/
1418 private array $ expectations = [];
1519
1620 // Register a static mock for the given class name.
17- public function __construct (string $ className )
21+ public function __construct (string $ className, TestCase $ testCase )
1822 {
1923 $ this ->className = $ className ;
24+ $ this ->testCase = $ testCase ;
2025 self ::$ mocks [$ className ] = $ this ;
2126 if (self ::$ autoloader === null ) {
2227 self ::$ autoloader = function (string $ class ): void {
@@ -53,7 +58,7 @@ public function expect(string $method, array $arguments, mixed $returns = null,
5358 * @param string $method The method name
5459 * @param array<int,mixed> $arguments The method arguments
5560 * @return mixed The return value
56- * @throws \Exception If no mock is registered or expectations do not match
61+ * @throws \Exception If no mock is registered or expectation fails
5762 */
5863 public static function handleStaticCall (string $ className , string $ method , array $ arguments ): mixed
5964 {
@@ -62,15 +67,14 @@ public static function handleStaticCall(string $className, string $method, array
6267 }
6368 $ mock = self ::$ mocks [$ className ];
6469 if (empty ($ mock ->expectations )) {
65- throw new \ Exception (sprintf ('StaticMethodMock unexpected call: %s::%s ' , $ className , $ method ));
70+ $ mock -> testCase -> fail (sprintf ('StaticMethodMock unexpected call: %s::%s ' , $ className , $ method ));
6671 }
6772 $ expected = array_shift ($ mock ->expectations );
68- // Basic matching
6973 if ($ expected ['method ' ] != strtoupper ($ method )) {
70- throw new \ Exception (sprintf ('StaticMethodMock method mismatch: expected %s got %s for %s::%s ' , $ expected ['method ' ], strtoupper ($ method ), $ className , $ method ));
74+ $ mock -> testCase -> fail (sprintf ('StaticMethodMock method mismatch: expected %s got %s for %s::%s ' , $ expected ['method ' ], strtoupper ($ method ), $ className , $ method ));
7175 }
7276 if ($ expected ['arguments ' ] != $ arguments ) {
73- throw new \ Exception (sprintf ('StaticMethodMock arguments mismatch for %s::%s: expected %s got %s ' , $ className , $ method , json_encode ($ expected ['arguments ' ]), json_encode ($ arguments )));
77+ $ mock -> testCase -> fail (sprintf ('StaticMethodMock arguments mismatch for %s::%s: expected %s got %s ' , $ className , $ method , json_encode ($ expected ['arguments ' ]), json_encode ($ arguments )));
7478 }
7579 if ($ expected ['exception ' ] !== null ) {
7680 throw $ expected ['exception ' ];
0 commit comments