11<?php
22
3- declare (strict_types=1 );
4-
53namespace MintyPHP \Mocking ;
64
75class StaticMethodMock
@@ -12,40 +10,52 @@ class StaticMethodMock
1210 public static array $ mocks = [];
1311 /** @var string */
1412 private string $ className ;
15- /** @var array<int,array{method:string,arguments:array,returnsVoid:bool ,returns:mixed,exception:?Throwable}> */
13+ /** @var array<int,array{method:string,arguments:array<int,mixed> ,returns:mixed,exception:?\ Throwable}> $expectations */
1614 private array $ expectations = [];
1715
1816 // Register a static mock for the given class name.
19- public function __construct (string $ className ) {
17+ public function __construct (string $ className )
18+ {
2019 $ this ->className = $ className ;
2120 self ::$ mocks [$ className ] = $ this ;
2221 if (self ::$ autoloader === null ) {
23- self ::$ autoloader = function (string $ class ) {
22+ self ::$ autoloader = function (string $ class ): void {
2423 if ($ class === $ this ->className ) {
25- $ namespace = substr ($ this ->className , 0 , strrpos ($ this ->className , '\\' ));
24+ $ namespace = substr ($ this ->className , 0 , strrpos ($ this ->className , '\\' ) + 0 );
2625 $ shortClassName = substr ($ this ->className , strrpos ($ this ->className , '\\' ) + 1 );
2726 eval ('namespace ' . $ namespace . ' { class ' . $ shortClassName . ' { public static function __callStatic($name, $arguments) { return \MintyPHP\Tests\StaticMethodMock::handleStaticCall( \'' . $ this ->className . '\', $name, $arguments); } } } ' );
28- return true ;
2927 }
30- return false ;
3128 };
3229 spl_autoload_register (self ::$ autoloader , true , true );
3330 }
3431 }
3532
36- /** Expect a with specific body (exact match). */
37- public function expect (string $ method , array $ arguments , bool $ returnsVoid = true , mixed $ returns = null , ?\Throwable $ exception = null )
33+ /** Expect a with specific body (exact match).
34+ * @param string $method The static method name
35+ * @param array<int,mixed> $arguments The arguments to expect
36+ * @param mixed $returns The return value if not void
37+ * @param ?\Throwable $exception An optional exception to throw
38+ */
39+
40+ public function expect (string $ method , array $ arguments , mixed $ returns = null , ?\Throwable $ exception = null ): void
3841 {
3942 $ this ->expectations [] = [
4043 'method ' => strtoupper ($ method ),
4144 'arguments ' => $ arguments ,
42- 'returnsVoid ' => $ returnsVoid ,
4345 'returns ' => $ returns ,
4446 'exception ' => $ exception ,
4547 ];
4648 }
4749
48- public static function handleStaticCall (string $ className , string $ method , array $ arguments )
50+ /**
51+ * Handle a static call to a mocked class.
52+ * @param string $className The class name
53+ * @param string $method The method name
54+ * @param array<int,mixed> $arguments The method arguments
55+ * @return mixed The return value
56+ * @throws \Exception If no mock is registered or expectations do not match
57+ */
58+ public static function handleStaticCall (string $ className , string $ method , array $ arguments ): mixed
4959 {
5060 if (!isset (self ::$ mocks [$ className ])) {
5161 throw new \Exception (sprintf ('StaticMethodMock no mock registered for class: %s ' , $ className ));
@@ -65,9 +75,6 @@ public static function handleStaticCall(string $className, string $method, array
6575 if ($ expected ['exception ' ] !== null ) {
6676 throw $ expected ['exception ' ];
6777 }
68- if ($ expected ['returnsVoid ' ]) {
69- return ;
70- }
7178 return $ expected ['returns ' ];
7279 }
7380}
0 commit comments