Skip to content

Commit 37dd895

Browse files
committed
Add neverExpect method
1 parent 3e9baa7 commit 37dd895

File tree

8 files changed

+56
-9
lines changed

8 files changed

+56
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
## v1.0.0 (2024-05-17)
1313
### Added
1414
- Added `withConsecutive` assertion method
15+
- Added `neverExpect` helper method

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ or add requirement to your `composer.json`
8686
Validate arguments and responses:
8787

8888
```php
89-
$mock->expects(self::exactly(count($arguments)))->method('example')
89+
$mock->expects(self::exactly(2))->method('example')
9090
->will(self::withConsecutive(
9191
arguments: [
9292
[1, 2, 0.1],
@@ -105,7 +105,7 @@ self::assertFalse($mock->example(2, 3, 0.01));
105105
Optional responses:
106106

107107
```php
108-
$mock->expects(self::exactly(count($arguments)))->method('example')
108+
$mock->expects(self::exactly(2))->method('example')
109109
->will(self::withConsecutive(
110110
arguments: [
111111
[1, 2, 0.1],
@@ -120,7 +120,7 @@ $mock->example(2, 3, 0.01);
120120
Simplification for same response for each call
121121

122122
```php
123-
$mock->expects(self::exactly(count($arguments)))->method('example')
123+
$mock->expects(self::exactly(2))->method('example')
124124
->will(self::withConsecutive(
125125
arguments: [
126126
[1, 2, 0.1],
@@ -136,7 +136,7 @@ self::assertTrue($mock->example(2, 3, 0.01));
136136
Supports throwing exceptions:
137137

138138
```php
139-
$mock->expects(self::exactly(count($arguments)))->method('example')
139+
$mock->expects(self::exactly(2))->method('example')
140140
->will(self::withConsecutive(
141141
arguments: [
142142
[1, 2, 0.1],

src/Extension.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@ trait Extension
1111
/**
1212
* @param array<array<int,mixed>|\PHPUnit\Framework\Constraint\Callback<mixed>> $arguments
1313
*/
14-
final public static function withConsecutive(array $arguments, mixed $responses = null): Stub
14+
protected static function withConsecutive(array $arguments, mixed $responses = null): Stub
1515
{
1616
return Assertion::withConsecutive($arguments, $responses);
1717
}
18+
19+
/**
20+
* @param list<\PHPUnit\Framework\MockObject\MockObject> $mocks
21+
*/
22+
protected static function neverExpect(array $mocks): void
23+
{
24+
foreach ($mocks as $mock) {
25+
$mock->expects(self::never())->method(self::anything());
26+
}
27+
}
1828
}

tests/Assertion/BaseWithConsecutiveTestCase.php renamed to tests/BaseWithConsecutiveTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Inspirum\PHPUnit\Tests\Assertion;
5+
namespace Inspirum\PHPUnit\Tests;
66

77
use LengthException;
88
use PHPUnit\Framework\Constraint\Callback;

tests/Assertion/Mock.php renamed to tests/Mock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Inspirum\PHPUnit\Tests\Assertion;
5+
namespace Inspirum\PHPUnit\Tests;
66

77
interface Mock
88
{

tests/NeverExpectTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Inspirum\PHPUnit\Tests;
6+
7+
use Inspirum\PHPUnit\Extension;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class NeverExpectTest extends TestCase
12+
{
13+
use Extension;
14+
15+
private Mock&MockObject $mock1;
16+
private Mock&MockObject $mock2;
17+
private Mock&MockObject $mock3;
18+
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->mock1 = $this->createMock(Mock::class);
24+
$this->mock2 = $this->createMock(Mock::class);
25+
$this->mock3 = $this->createMock(Mock::class);
26+
}
27+
28+
public function testNeverExpect(): void
29+
{
30+
self::neverExpect([
31+
$this->mock1,
32+
$this->mock2,
33+
$this->mock3,
34+
]);
35+
}
36+
}

tests/Assertion/WithConsecutiveAssertionTest.php renamed to tests/WithConsecutiveAssertionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Inspirum\PHPUnit\Tests\Assertion;
5+
namespace Inspirum\PHPUnit\Tests;
66

77
use Inspirum\PHPUnit\Assertion;
88
use PHPUnit\Framework\MockObject\Stub\Stub;

tests/Assertion/WithConsecutiveExtensionTest.php renamed to tests/WithConsecutiveExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Inspirum\PHPUnit\Tests\Assertion;
5+
namespace Inspirum\PHPUnit\Tests;
66

77
use Inspirum\PHPUnit\Extension;
88
use PHPUnit\Framework\MockObject\Stub\Stub;

0 commit comments

Comments
 (0)