22
33namespace MintyPHP \Mocking ;
44
5+ use Exception ;
6+ use PHPUnit \Framework \ExpectationFailedException ;
57use PHPUnit \Framework \TestCase ;
8+ use Throwable ;
69
710class StaticMethodMock
811{
@@ -14,7 +17,7 @@ class StaticMethodMock
1417 private string $ className ;
1518 /** @var TestCase */
1619 private TestCase $ testCase ;
17- /** @var array<int,array{method:string,arguments:array<int,mixed>,returns:mixed,exception:?\ Throwable}> $expectations*/
20+ /** @var array<int,array{method:string,arguments:array<int,mixed>,returns:mixed,exception:?Throwable}> $expectations*/
1821 private array $ expectations = [];
1922
2023 // Register a static mock for the given class name.
@@ -39,10 +42,10 @@ public function __construct(string $className, TestCase $testCase)
3942 * @param string $method The static method name
4043 * @param array<int,mixed> $arguments The arguments to expect
4144 * @param mixed $returns The return value if not void
42- * @param ?\ Throwable $exception An optional exception to throw
45+ * @param ?Throwable $exception An optional exception to throw
4346 */
4447
45- public function expect (string $ method , array $ arguments , mixed $ returns = null , ?\ Throwable $ exception = null ): void
48+ public function expect (string $ method , array $ arguments , mixed $ returns = null , ?Throwable $ exception = null ): void
4649 {
4750 $ this ->expectations [] = [
4851 'method ' => strtoupper ($ method ),
@@ -58,23 +61,24 @@ public function expect(string $method, array $arguments, mixed $returns = null,
5861 * @param string $method The method name
5962 * @param array<int,mixed> $arguments The method arguments
6063 * @return mixed The return value
61- * @throws \Exception If no mock is registered or expectation fails
64+ * @throws Exception If no mock is registered or expectation fails
65+ * @throws ExpectationFailedException If expectation fails
6266 */
6367 public static function handleStaticCall (string $ className , string $ method , array $ arguments ): mixed
6468 {
6569 if (!isset (self ::$ mocks [$ className ])) {
66- throw new \ Exception (sprintf ('StaticMethodMock no mock registered for class: %s ' , $ className ));
70+ throw new Exception (sprintf ('StaticMethodMock no mock registered for class: %s ' , $ className ));
6771 }
6872 $ mock = self ::$ mocks [$ className ];
6973 if (empty ($ mock ->expectations )) {
70- $ mock ->testCase ->fail (sprintf ('StaticMethodMock unexpected call: %s::%s ' , $ className , $ method ));
74+ $ mock ->testCase ->fail (sprintf ('StaticMethodMock no expectations left for %s::%s ' , $ className , $ method ));
7175 }
7276 $ expected = array_shift ($ mock ->expectations );
7377 if ($ expected ['method ' ] != strtoupper ($ method )) {
74- $ mock ->testCase ->fail ( sprintf ('StaticMethodMock method mismatch: expected %s got %s for %s::%s ' , $ expected [ ' method ' ], strtoupper ( $ method ) , $ className , $ method ));
78+ $ mock ->testCase ->assertEquals ( $ expected [ ' method ' ], strtoupper ( $ method ), sprintf ('StaticMethodMock method mismatch for %s::%s ' , $ className , $ method ));
7579 }
7680 if ($ expected ['arguments ' ] != $ 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 ) ));
81+ $ mock ->testCase ->assertEquals ( $ expected [ ' arguments ' ], $ arguments , sprintf ('StaticMethodMock arguments mismatch for %s::%s ' , $ className , $ method ));
7882 }
7983 if ($ expected ['exception ' ] !== null ) {
8084 throw $ expected ['exception ' ];
0 commit comments