Skip to content

Commit

Permalink
Add neverExpect method
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed May 17, 2024
1 parent 3e9baa7 commit 37dd895
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand Down
12 changes: 11 additions & 1 deletion src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ trait Extension
/**
* @param array<array<int,mixed>|\PHPUnit\Framework\Constraint\Callback<mixed>> $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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Inspirum\PHPUnit\Tests\Assertion;
namespace Inspirum\PHPUnit\Tests;

use LengthException;
use PHPUnit\Framework\Constraint\Callback;
Expand Down
2 changes: 1 addition & 1 deletion tests/Assertion/Mock.php → tests/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Inspirum\PHPUnit\Tests\Assertion;
namespace Inspirum\PHPUnit\Tests;

interface Mock
{
Expand Down
36 changes: 36 additions & 0 deletions tests/NeverExpectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Inspirum\PHPUnit\Tests;

use Inspirum\PHPUnit\Extension;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class NeverExpectTest extends TestCase
{
use Extension;

private Mock&MockObject $mock1;
private Mock&MockObject $mock2;
private Mock&MockObject $mock3;

protected function setUp(): void
{
parent::setUp();

$this->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,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 37dd895

Please sign in to comment.