From 37dd8953c8132d16137cb89030f7bb16da7fed10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Novotn=C3=BD?= Date: Fri, 17 May 2024 14:23:45 +0200 Subject: [PATCH] Add neverExpect method --- CHANGELOG.md | 1 + README.md | 8 ++--- src/Extension.php | 12 ++++++- .../BaseWithConsecutiveTestCase.php | 2 +- tests/{Assertion => }/Mock.php | 2 +- tests/NeverExpectTest.php | 36 +++++++++++++++++++ .../WithConsecutiveAssertionTest.php | 2 +- .../WithConsecutiveExtensionTest.php | 2 +- 8 files changed, 56 insertions(+), 9 deletions(-) rename tests/{Assertion => }/BaseWithConsecutiveTestCase.php (99%) rename tests/{Assertion => }/Mock.php (92%) create mode 100644 tests/NeverExpectTest.php rename tests/{Assertion => }/WithConsecutiveAssertionTest.php (89%) rename tests/{Assertion => }/WithConsecutiveExtensionTest.php (90%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 447afc6..1f65e04 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## v1.0.0 (2024-05-17) ### Added - Added `withConsecutive` assertion method +- Added `neverExpect` helper method diff --git a/README.md b/README.md index 4f48e23..b98e4ad 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ or add requirement to your `composer.json` Validate arguments and responses: ```php -$mock->expects(self::exactly(count($arguments)))->method('example') +$mock->expects(self::exactly(2))->method('example') ->will(self::withConsecutive( arguments: [ [1, 2, 0.1], @@ -105,7 +105,7 @@ self::assertFalse($mock->example(2, 3, 0.01)); Optional responses: ```php -$mock->expects(self::exactly(count($arguments)))->method('example') +$mock->expects(self::exactly(2))->method('example') ->will(self::withConsecutive( arguments: [ [1, 2, 0.1], @@ -120,7 +120,7 @@ $mock->example(2, 3, 0.01); Simplification for same response for each call ```php -$mock->expects(self::exactly(count($arguments)))->method('example') +$mock->expects(self::exactly(2))->method('example') ->will(self::withConsecutive( arguments: [ [1, 2, 0.1], @@ -136,7 +136,7 @@ self::assertTrue($mock->example(2, 3, 0.01)); Supports throwing exceptions: ```php -$mock->expects(self::exactly(count($arguments)))->method('example') +$mock->expects(self::exactly(2))->method('example') ->will(self::withConsecutive( arguments: [ [1, 2, 0.1], diff --git a/src/Extension.php b/src/Extension.php index a7d59e8..dba8d73 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -11,8 +11,18 @@ trait Extension /** * @param array|\PHPUnit\Framework\Constraint\Callback> $arguments */ - final public static function withConsecutive(array $arguments, mixed $responses = null): Stub + protected static function withConsecutive(array $arguments, mixed $responses = null): Stub { return Assertion::withConsecutive($arguments, $responses); } + + /** + * @param list<\PHPUnit\Framework\MockObject\MockObject> $mocks + */ + protected static function neverExpect(array $mocks): void + { + foreach ($mocks as $mock) { + $mock->expects(self::never())->method(self::anything()); + } + } } diff --git a/tests/Assertion/BaseWithConsecutiveTestCase.php b/tests/BaseWithConsecutiveTestCase.php similarity index 99% rename from tests/Assertion/BaseWithConsecutiveTestCase.php rename to tests/BaseWithConsecutiveTestCase.php index d4552f1..fcab32c 100644 --- a/tests/Assertion/BaseWithConsecutiveTestCase.php +++ b/tests/BaseWithConsecutiveTestCase.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Inspirum\PHPUnit\Tests\Assertion; +namespace Inspirum\PHPUnit\Tests; use LengthException; use PHPUnit\Framework\Constraint\Callback; diff --git a/tests/Assertion/Mock.php b/tests/Mock.php similarity index 92% rename from tests/Assertion/Mock.php rename to tests/Mock.php index 03bb3e1..ef569d6 100644 --- a/tests/Assertion/Mock.php +++ b/tests/Mock.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Inspirum\PHPUnit\Tests\Assertion; +namespace Inspirum\PHPUnit\Tests; interface Mock { diff --git a/tests/NeverExpectTest.php b/tests/NeverExpectTest.php new file mode 100644 index 0000000..bd35e5b --- /dev/null +++ b/tests/NeverExpectTest.php @@ -0,0 +1,36 @@ +mock1 = $this->createMock(Mock::class); + $this->mock2 = $this->createMock(Mock::class); + $this->mock3 = $this->createMock(Mock::class); + } + + public function testNeverExpect(): void + { + self::neverExpect([ + $this->mock1, + $this->mock2, + $this->mock3, + ]); + } +} diff --git a/tests/Assertion/WithConsecutiveAssertionTest.php b/tests/WithConsecutiveAssertionTest.php similarity index 89% rename from tests/Assertion/WithConsecutiveAssertionTest.php rename to tests/WithConsecutiveAssertionTest.php index 1b54624..5041d43 100644 --- a/tests/Assertion/WithConsecutiveAssertionTest.php +++ b/tests/WithConsecutiveAssertionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Inspirum\PHPUnit\Tests\Assertion; +namespace Inspirum\PHPUnit\Tests; use Inspirum\PHPUnit\Assertion; use PHPUnit\Framework\MockObject\Stub\Stub; diff --git a/tests/Assertion/WithConsecutiveExtensionTest.php b/tests/WithConsecutiveExtensionTest.php similarity index 90% rename from tests/Assertion/WithConsecutiveExtensionTest.php rename to tests/WithConsecutiveExtensionTest.php index 8dd1eb3..345f82f 100644 --- a/tests/Assertion/WithConsecutiveExtensionTest.php +++ b/tests/WithConsecutiveExtensionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Inspirum\PHPUnit\Tests\Assertion; +namespace Inspirum\PHPUnit\Tests; use Inspirum\PHPUnit\Extension; use PHPUnit\Framework\MockObject\Stub\Stub;