44
55use MintyPHP \Mocking \StaticMethodMock ;
66use MintyPHP \Mocking \Tests \Math \Adder ;
7+ use PHPUnit \Framework \AssertionFailedError ;
78use PHPUnit \Framework \TestCase ;
89
910class 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