Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed May 16, 2024
1 parent 8f1d949 commit aeb67ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
15 changes: 3 additions & 12 deletions src/CallableFake.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
<?php

declare(strict_types=1);

namespace TiMacDonald\CallableFake;

use Closure;
use PHPUnit\Framework\Assert;

use function array_filter;
use function array_intersect;
use function array_keys;
use function call_user_func_array;
use function count;
use function func_get_args;
use function implode;

class CallableFake
{
/**
Expand All @@ -29,7 +19,8 @@ class CallableFake

public function __construct(?callable $callback = null)
{
$this->returnResolver = $callback ?? static function (): void {
$this->returnResolver = $callback ?? function (): void {
//
};
}

Expand Down Expand Up @@ -136,7 +127,7 @@ public function wasNotInvoked(): bool
*/
public function called(callable $callback): array
{
return array_filter($this->invocations, static function (array $arguments) use ($callback): bool {
return array_filter($this->invocations, function (array $arguments) use ($callback): bool {
return (bool) $callback(...$arguments);

Check warning on line 131 in src/CallableFake.php

View workflow job for this annotation

GitHub Actions / Test

Escaped Mutant for Mutator "CastBool": --- Original +++ New @@ @@ public function called(callable $callback): array { return array_filter($this->invocations, function (array $arguments) use ($callback): bool { - return (bool) $callback(...$arguments); + return $callback(...$arguments); }); } }
});
}
Expand Down
50 changes: 23 additions & 27 deletions tests/CallableFakeTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
<?php

declare(strict_types=1);

namespace Tests;

use Closure;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use TiMacDonald\CallableFake\CallableFake;

use function in_array;

class CallableFakeTest extends TestCase
{
public function testAssertCalledWithFalseBeforeBeingCalled(): void
{
$fake = new CallableFake();

try {
$fake->assertCalled(static function (string $arg) {
$fake->assertCalled(function (string $arg) {
return $arg === 'x';
});
$this->fail();
Expand All @@ -32,7 +28,7 @@ public function testAssertCalledWithTrueBeforeBeingCalled(): void
$fake = new CallableFake();

try {
$fake->assertCalled(static function (string $arg) {
$fake->assertCalled(function (string $arg) {
return $arg === 'a';
});
$this->fail();
Expand All @@ -46,7 +42,7 @@ public function testAssertCalledWithTrueAfterBeingCalled(): void
$fake = new callablefake();
$fake('a');

$fake->assertCalled(static function (string $arg) {
$fake->assertCalled(function (string $arg) {
return $arg === 'a';
});
}
Expand All @@ -57,7 +53,7 @@ public function testAssertCalledWithFalseAfterBeingCalled(): void
$fake('a');

try {
$fake->assertCalled(static function (string $arg) {
$fake->assertCalled(function (string $arg) {
return $arg === 'x';
});
$this->fail();
Expand All @@ -70,7 +66,7 @@ public function testAssertNotCalledWithTrueBeforeBeingCalled(): void
{
$fake = new CallableFake();

$fake->assertNotCalled(static function (string $arg) {
$fake->assertNotCalled(function (string $arg) {
return $arg === 'a';
});
}
Expand All @@ -79,7 +75,7 @@ public function testAssertNotCalledWithFalseBeforeBeingCalled(): void
{
$fake = new CallableFake();

$fake->assertNotCalled(static function (string $arg) {
$fake->assertNotCalled(function (string $arg) {
return $arg === 'x';
});
}
Expand All @@ -90,7 +86,7 @@ public function testAssertNotCalledWithTrueAfterBeingCalled(): void
$fake('a');

try {
$fake->assertNotCalled(static function (string $arg) {
$fake->assertNotCalled(function (string $arg) {
return $arg === 'a';
});
$this->fail();
Expand All @@ -104,7 +100,7 @@ public function testAssertNotCalledWithFalseAfterBeingCalled(): void
$fake = new CallableFake();
$fake('a');

$fake->assertNotCalled(static function (string $arg) {
$fake->assertNotCalled(function (string $arg) {
return $arg === 'x';
});
}
Expand All @@ -114,7 +110,7 @@ public function testAssertCalledTimesWithTrueAndExpectedCount(): void
$fake = new CallableFake();
$fake('a');

$fake->assertCalledTimes(static function (string $arg) {
$fake->assertCalledTimes(function (string $arg) {
return $arg === 'a';
}, 1);
}
Expand All @@ -125,7 +121,7 @@ public function testAssertCalledTimesWithTrueAndUnexpectedCount(): void
$fake('a');

try {
$fake->assertCalledTimes(static function (string $arg) {
$fake->assertCalledTimes(function (string $arg) {
return $arg === 'a';
}, 2);
$this->fail();
Expand All @@ -139,7 +135,7 @@ public function testAssertCalledTimesWithFalseAndExpectedCount(): void
$fake = new CallableFake();
$fake('a');

$fake->assertCalledTimes(static function (string $arg) {
$fake->assertCalledTimes(function (string $arg) {
return $arg === 'x';
}, 0);
}
Expand All @@ -150,7 +146,7 @@ public function testAssertCalledTimesWithFalseAndUnexpectedCount(): void
$fake('a');

try {
$fake->assertCalledTimes(static function (string $arg) {
$fake->assertCalledTimes(function (string $arg) {
return $arg === 'x';
}, 2);
$this->fail();
Expand Down Expand Up @@ -225,11 +221,11 @@ public function testCanUseAsAClosure(): void
{
$fake = new CallableFake();

(static function (Closure $callback): void {
(function (Closure $callback): void {
$callback('a');
})($fake->asClosure());

$fake->assertCalled(static function (string $arg) {
$fake->assertCalled(function (string $arg) {
return $arg === 'a';
});
}
Expand All @@ -239,7 +235,7 @@ public function testReturnValuesCanBeTruthy(): void
$fake = new CallableFake();
$fake('a');

$fake->assertCalled(static function () {
$fake->assertCalled(function () {
return 1;
});
}
Expand All @@ -248,14 +244,14 @@ public function testReturnValuesCanBeFalsy(): void
{
$fake = new CallableFake();

$fake->assertNotCalled(static function () {
$fake->assertNotCalled(function () {
return 0;
});
}

public function testCanSpecifyInvocationReturnTypes(): void
{
$fake = CallableFake::withReturnResolver(static function (int $index): string {
$fake = CallableFake::withReturnResolver(function (int $index): string {
return [
0 => 'a',
1 => 'b',
Expand Down Expand Up @@ -293,7 +289,7 @@ public function testCanGetInvocationArguments(): void
$callable('c', 'd');
$callable('x', 'y');

$invocationArguments = $callable->called(static function (string $arg) {
$invocationArguments = $callable->called(function (string $arg) {
return in_array($arg, ['a', 'c'], true);
});

Expand All @@ -307,7 +303,7 @@ public function testAssertCalledIndexWithExpectedSingleIndex(): void
$callable('a');
$callable('b');

$callable->assertCalledIndex(static function (string $arg): bool {
$callable->assertCalledIndex(function (string $arg): bool {
return $arg === 'a';
}, 1);
}
Expand All @@ -319,7 +315,7 @@ public function testAssertCalledIndexWithExpectedMutlipleIndex(): void
$callable('a');
$callable('b');

$callable->assertCalledIndex(static function (string $arg): bool {
$callable->assertCalledIndex(function (string $arg): bool {
return $arg === 'b';
}, [0, 2]);
}
Expand All @@ -332,7 +328,7 @@ public function testAssertCalledIndexWithUnexpectedSingleIndex(): void
$callable('b');

try {
$callable->assertCalledIndex(static function (string $arg): bool {
$callable->assertCalledIndex(function (string $arg): bool {
return $arg === 'b';
}, 1);
$this->fail();
Expand All @@ -349,7 +345,7 @@ public function testAssertCalledIndexWithUnexpectedMultipleIndex(): void
$callable('b');

try {
$callable->assertCalledIndex(static function (string $arg): bool {
$callable->assertCalledIndex(function (string $arg): bool {
return $arg === 'b';
}, [1, 3]);
$this->fail();
Expand All @@ -363,7 +359,7 @@ public function testAssertCalledIndexWhenNeverCalledWithExpectedCallable(): void
$callable = new CallableFake();

try {
$callable->assertCalledIndex(static function (string $arg): bool {
$callable->assertCalledIndex(function (string $arg): bool {
return $arg === 'b';
}, 1);
$this->fail();
Expand Down

0 comments on commit aeb67ad

Please sign in to comment.