Skip to content

Commit 334e22d

Browse files
committed
Use assertions
1 parent e56c46b commit 334e22d

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/StaticMethodMock.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace MintyPHP\Mocking;
44

5+
use Exception;
6+
use PHPUnit\Framework\ExpectationFailedException;
57
use PHPUnit\Framework\TestCase;
8+
use Throwable;
69

710
class 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'];

tests/StaticMethodMockTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testStaticMethodMock(): void
2020
// Create a static method mock for the Adder class
2121
$mock = new StaticMethodMock(Adder::class, $this);
2222
// Set expectation for the add method
23-
$mock->expect('add', [1, 2], 3);
23+
$mock->expect('add', [1, 2, 3], 3);
2424
// Call the public static add method
2525
$result = Adder::add(1, 2);
2626
// Verify the result

0 commit comments

Comments
 (0)