Skip to content

Commit 7822cc3

Browse files
committed
Use assertions
1 parent 7853991 commit 7822cc3

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

tests/Math/Adder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace MintyPHP\Mocking\Tests\Math;
44

5+
use Exception;
6+
57
class Adder
68
{
79
public static function add($a, $b):int
810
{
11+
throw new Exception("This method should be mocked!");
912
return $a+$b;
1013
}
1114
}

tests/StaticMethodMockTest.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use MintyPHP\Mocking\StaticMethodMock;
66
use MintyPHP\Mocking\Tests\Math\Adder;
7+
use PHPUnit\Framework\AssertionFailedError;
78
use PHPUnit\Framework\TestCase;
89

910
class StaticMethodMockTest extends TestCase
1011
{
11-
public function testUsingMockedAdder(): void
12+
public function testAdderAdd(): void
1213
{
1314
// Create a static method mock for the Adder class
1415
$mock = new StaticMethodMock(Adder::class, $this);
@@ -22,7 +23,7 @@ public function testUsingMockedAdder(): void
2223
$mock->assertExpectationsMet();
2324
}
2425

25-
public function testFailingAdder(): void
26+
public function testExtraExpectations(): void
2627
{
2728
// Create a static method mock for the Adder class
2829
$mock = new StaticMethodMock(Adder::class, $this);
@@ -33,21 +34,29 @@ public function testFailingAdder(): void
3334
$result = Adder::add(1, 2);
3435
// Verify the result
3536
$this->assertEquals(3, $result);
36-
// Assert that all expectations were met
37-
$mock->assertExpectationsMet();
37+
// Assert that all expectations were met (expected to fail)
38+
try {
39+
$mock->assertExpectationsMet();
40+
$this->fail('Expected AssertionFailedError was not thrown.');
41+
} catch (AssertionFailedError $e) {
42+
$this->assertEquals('StaticMethodMock not all expectations met for MintyPHP\Mocking\Tests\Math\Adder, 1 remaining', $e->getMessage());
43+
}
3844
}
3945

40-
public function testUsingMockedAdderAgain(): void
46+
public function testNotEnoughExpectations(): void
4147
{
4248
// Create a static method mock for the Adder class
4349
$mock = new StaticMethodMock(Adder::class, $this);
4450
// Set expectation for the add method
4551
$mock->expect('add', [1, 2], 3);
4652
// Call the public static add method
47-
$result = Adder::add(1, 2);
48-
// Verify the result
49-
$this->assertEquals(3, $result);
50-
// Assert that all expectations were met
51-
$mock->assertExpectationsMet();
53+
Adder::add(1, 2);
54+
try {
55+
// Call the public static add method again without expectation
56+
Adder::add(1, 2);
57+
$this->fail('Expected AssertionFailedError was not thrown.');
58+
} catch (AssertionFailedError $e) {
59+
$this->assertEquals('StaticMethodMock no expectations left for MintyPHP\Mocking\Tests\Math\Adder::add', $e->getMessage());
60+
}
5261
}
5362
}

0 commit comments

Comments
 (0)