Skip to content

Commit ffe96df

Browse files
committed
Improve error reporting
1 parent d234983 commit ffe96df

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/StaticMethodMock.php

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

33
namespace MintyPHP\Mocking;
44

5+
use PHPUnit\Framework\TestCase;
6+
57
class 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'];

tests/StaticMethodMockTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MintyPHP\Mocking\Tests;
44

55
use MintyPHP\Mocking\StaticMethodMock;
6+
use PHPUnit\Framework\TestCase;
67

78
class Adder
89
{
@@ -12,12 +13,12 @@ public static function add($a, $b):int
1213
}
1314
}
1415

15-
class StaticMethodMockTest extends \PHPUnit\Framework\TestCase
16+
class StaticMethodMockTest extends TestCase
1617
{
1718
public function testStaticMethodMock(): void
1819
{
1920
// Create a static method mock for the Adder class
20-
$mock = new StaticMethodMock(Adder::class);
21+
$mock = new StaticMethodMock(Adder::class, $this);
2122
// Set expectation for the add method
2223
$mock->expect('add', [1, 2], 3);
2324
// Call the public static add method

0 commit comments

Comments
 (0)