Skip to content

Commit 40ea773

Browse files
committed
improve code
1 parent 2b4767d commit 40ea773

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/StaticMethodMock.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace MintyPHP\Mocking;
64

75
class 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
}

tests/StaticMethodMockTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace MintyPHP\Mocking\Tests;
64

75
use MintyPHP\Mocking\StaticMethodMock;
@@ -21,7 +19,7 @@ public function testStaticMethodMock(): void
2119
// Create a static method mock for the Adder class
2220
$mock = new StaticMethodMock(Adder::class);
2321
// Set expectation for the add method
24-
$mock->expect('add', [1, 2], false, 3);
22+
$mock->expect('add', [1, 2], 3);
2523
// Call the public static add method
2624
$result = Adder::add(1, 2);
2725
// Verify the result

0 commit comments

Comments
 (0)