diff --git a/documentation.md b/documentation.md index e513520..2c3d879 100644 --- a/documentation.md +++ b/documentation.md @@ -70,7 +70,7 @@ Returning a static value ```php use Exan\Moock\Mock; -Mock::method($this->mock->userExists(...))->forceReturn(true); +Mock::method($this->mock->userExists(...))->returns(true); $this->assertTrue($this->mock->userExists('some-email@domain.com')); ``` @@ -79,7 +79,7 @@ Returning a sequence of values ```php use Exan\Moock\Mock; -Mock::method($this->mock->userExists(...))->forceReturnSequence([true, true, false]); +Mock::method($this->mock->userExists(...))->returnsSequence([true, true, false]); $this->assertTrue($this->mock->userExists('some-email@domain.com')); $this->assertTrue($this->mock->userExists('some-email@domain.com')); @@ -93,7 +93,7 @@ Throwing an exception use Exan\Moock\Mock; use RuntimeException; -Mock::method($this->mock->createUser(...))->throwsException(RuntimeException::class); +Mock::method($this->mock->createUser(...))->throws(RuntimeException::class); $this->expectException(RuntimeException::class); @@ -114,117 +114,117 @@ These assertions work out of the box with both [PHPUnit](https://packagist.org/p Asserting the method not called at all. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->not() - ->toHaveBeenCalled(); + ->called(); ``` Asserting the method was called at all. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() - ->toHaveBeenCalled(); + ->assert() + ->called(); ``` Asserting the method was called exactly once. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() - ->toHaveBeenCalledOnce(); + ->assert() + ->calledOnce(); ``` Asserting the method was not called exactly once. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->not() - ->toHaveBeenCalledOnce(); + ->calledOnce(); ``` Asserting the method was called _n_ times. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() - ->toHaveBeenCalledTimes(2); + ->assert() + ->calledTimes(2); ``` Asserting the method was not called _n_ times. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->not() - ->toHaveBeenCalledTimes(2); + ->calledTimes(2); ``` ### Asserting method was called with specific input You can assert a method was called with specific input by passing the expected arguments into `with()`. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->with('my-email@domain.com') - ->toHaveBeenCalled(); + ->called(); ``` This can of course also be reversed. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() - ->not() + ->assert() ->with('other-email@domain.com') - ->toHaveBeenCalled(); + ->not() + ->called(); ``` Rather than being tied to static values, you can pass a closure as well. ```php Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->with(fn (string $email) => str_ends_with($email, '@domain.com')) - ->toHaveBeenCalled(); + ->called(); ``` If you only care about a specific argument, you can use named arguments. @@ -232,9 +232,9 @@ If you only care about a specific argument, you can use named arguments. $this->mock->createUser('my-email@domain.com', 'my-username', 'password'); Mock::method($this->mock->createUser(...)) - ->expect() + ->assert() ->with(email: 'my-email@domain.com', password: 'password') - ->toHaveBeenCalled(); + ->called(); ``` Of course, closures can be used here too. @@ -242,11 +242,11 @@ Of course, closures can be used here too. $this->mock->createUser('my-email@domain.com', 'my-username', 'password'); Mock::method($this->mock->createUser(...)) - ->expect() + ->assert() ->with( email: fn (string $email) => str_ends_with($email, '@domain.com'), password: 'password', - )->toHaveBeenCalled(); + )->called(); ``` ### Built-in helpers @@ -255,14 +255,14 @@ Mock::method($this->mock->createUser(...)) use Exan\Moock\Args\Str; Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('test@mail.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->with(Str::contains('@mail.com')) - ->toHaveBeenCalled(); + ->called(); ``` `string` must have specific length @@ -270,14 +270,14 @@ Mock::method($this->mock->userExists(...)) use Exan\Moock\Args\Str; Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('test@mail.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->with(Str::length(13)) - ->toHaveBeenCalled(); + ->called(); ``` `DateTimeInterface` must be before given time @@ -286,14 +286,14 @@ use Exan\Moock\Args\Date; use DateTime; Mock::method($this->mock->getUsersCreatedBefore(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersCreatedBefore(new DateTime('2024-12-31')); Mock::method($this->mock->getUsersCreatedBefore(...)) - ->expect() + ->assert() ->with(Date::before(new DateTime('2025-01-01 12:00:00'))) - ->toHaveBeenCalled(); + ->called(); ``` `DateTimeInterface` must be after given time @@ -302,14 +302,14 @@ use Exan\Moock\Args\Date; use DateTime; Mock::method($this->mock->getUsersCreatedBefore(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersCreatedBefore(new DateTime('2025-01-02')); Mock::method($this->mock->getUsersCreatedBefore(...)) - ->expect() + ->assert() ->with(Date::after(new DateTime('2025-01-01 12:00:00'))) - ->toHaveBeenCalled(); + ->called(); ``` `int|float` must be less than given number @@ -317,14 +317,14 @@ Mock::method($this->mock->getUsersCreatedBefore(...)) use Exan\Moock\Args\Number; Mock::method($this->mock->getUsersByAge(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersByAge(50); Mock::method($this->mock->getUsersByAge(...)) - ->expect() + ->assert() ->with(Number::lt(100)) - ->toHaveBeenCalled(); + ->called(); ``` `int|float` must be greater than given number @@ -332,14 +332,14 @@ Mock::method($this->mock->getUsersByAge(...)) use Exan\Moock\Args\Number; Mock::method($this->mock->getUsersByAge(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersByAge(75); Mock::method($this->mock->getUsersByAge(...)) - ->expect() + ->assert() ->with(Number::gt(50)) - ->toHaveBeenCalled(); + ->called(); ``` `int|float` must be within range @@ -347,14 +347,14 @@ Mock::method($this->mock->getUsersByAge(...)) use Exan\Moock\Args\Number; Mock::method($this->mock->getUsersByAge(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersByAge(15); Mock::method($this->mock->getUsersByAge(...)) - ->expect() + ->assert() ->with(Number::range(10, 20)) - ->toHaveBeenCalled(); + ->called(); ``` `array` must have given number of items @@ -364,9 +364,9 @@ use Exan\Moock\Args\Arr; $this->mock->deleteUsersByEmail(['a','b','c']); Mock::method($this->mock->deleteUsersByEmail(...)) - ->expect() + ->assert() ->with(Arr::count(3)) - ->toHaveBeenCalled(); + ->called(); ``` `array` must be a partial match @@ -380,12 +380,12 @@ $this->mock->deleteUsersByEmail([ ]); Mock::method($this->mock->deleteUsersByEmail(...)) - ->expect() + ->assert() ->with(Arr::partial([ 0 => 'some-email@example.com', 2 => fn ($email) => str_ends_with($email, '@example.com'), ])) - ->toHaveBeenCalled(); + ->called(); ``` --- @@ -397,30 +397,30 @@ If you're expecting many calls to be made after each other in specific order, yo ### Verifying order of calls To verify the order in which methods were called on a mock, call `$expect` in the desired order with the given method. ```php -Mock::method($this->mock->isValidEmail(...))->forceReturn(true); -Mock::method($this->mock->userExists(...))->forceReturn(false); +Mock::method($this->mock->isValidEmail(...))->returns(true); +Mock::method($this->mock->userExists(...))->returns(false); $this->mock->isValidEmail('mail@domain.com'); $this->mock->userExists('mail@domain.com'); $this->mock->createUser('mail@domain.com', 'username', 'password'); // Asserting the methods are called in the right order only -Mock::expect(function ($expect): void { - $expect($this->mock->isValidEmail(...)); - $expect($this->mock->userExists(...)); - $expect($this->mock->createUser(...)); +Mock::verify(function ($assert): void { + $assert($this->mock->isValidEmail(...)); + $assert($this->mock->userExists(...)); + $assert($this->mock->createUser(...)); }); ``` ### Verifying order and arguments Optionally attach expectations of the given arguments. ```php -Mock::expect(function (Closure $expect): void { - $expect($this->mock->isValidEmail(...))->with('mail@domain.com'); - $expect($this->mock->userExists(...)); // Argument isn't verified, just order +Mock::verify(function (Closure $assert): void { + $assert($this->mock->isValidEmail(...))->with('mail@domain.com'); + $assert($this->mock->userExists(...)); // Argument isn't verified, just order // Only mail and password are validated - $expect($this->mock->createUser(...))->with('mail@domain.com', password: 'password'); + $assert($this->mock->createUser(...))->with('mail@domain.com', password: 'password'); }); ``` @@ -435,10 +435,10 @@ $productsService->productExists(123); $userService->isValidEmail('mail@domain.com'); $productsService->purchase(123, 'mail@domain.com'); -Mock::expect(function (Closure $expect) use ($userService, $productsService): void { - $expect($productsService->productExists(...))->with(123); - $expect($userService->isValidEmail(...))->with('mail@domain.com'); - $expect($productsService->purchase(...))->with(123, 'mail@domain.com'); +Mock::verify(function (Closure $assert) use ($userService, $productsService): void { + $assert($productsService->productExists(...))->with(123); + $assert($userService->isValidEmail(...))->with('mail@domain.com'); + $assert($productsService->purchase(...))->with(123, 'mail@domain.com'); }); ``` @@ -449,11 +449,11 @@ Mock::expect(function (Closure $expect) use ($userService, $productsService): vo Filters restrict which arguments are allowed at runtime. Calls with disallowed input will immediately throw a `RuntimeException`. ### Filtering an argument -To filter arguments that are allowed into a method, you can use the `filter()` method. +To filter arguments that are allowed into a method, you can use the following. ```php Mock::method($this->mock->userExists(...)) - ->filter('my-email@domain.com') - ->forceReturn(true); + ->allow('my-email@domain.com') + ->returns(true); $this->assertTrue($this->mock->userExists('my-email@domain.com')); @@ -465,7 +465,7 @@ $this->mock->userExists('other-email@domain.com'); To filter specific args of a method, use named properties. ```php Mock::method($this->mock->createUser(...)) - ->filter(username: 'my-username'); + ->allow(username: 'my-username'); $this->mock->createUser('my-email@domain.com', 'my-username', 'password'); @@ -478,8 +478,8 @@ $this->mock->createUser('my-email@domain.com', 'other-username', 'password'); You can also pass a closure instead of a straight value, or use some of the helper functions documented in the expectations section instead. ```php Mock::method($this->mock->userExists(...)) - ->filter(fn (string $email) => in_array($email, ['first@mail.com', 'second@mail.com'])) - ->forceReturn(true); + ->allow(fn (string $email) => in_array($email, ['first@mail.com', 'second@mail.com'])) + ->returns(true); $this->mock->userExists('first@mail.com'); $this->mock->userExists('second@mail.com'); @@ -557,8 +557,8 @@ MoockAssert::useAssert(function (bool $actual, bool $expected, string $message) $mock = Mock::interface(UserServiceInterface::class); Mock::method($mock->createUser(...)) - ->expect() - ->toHaveBeenCalled(); // This now uses our custom assert, which doesn't throw exceptions on failure + ->assert() + ->called(); // This now uses our custom assert, which doesn't throw exceptions on failure $this->assertTrue($usedAssert); ``` diff --git a/src/Expectation.php b/src/Expectation.php index 741de76..a3fbfeb 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -35,7 +35,7 @@ private function callsAmount(): int return count($this->calls); } - public function toHaveBeenCalledTimes(int $expectedCalls): void + public function calledTimes(int $expectedCalls): void { $callsCount = $this->callsAmount(); @@ -46,7 +46,7 @@ public function toHaveBeenCalledTimes(int $expectedCalls): void $this->assert($callsCount === $expectedCalls, $message); } - public function toHaveBeenCalled(): void + public function called(): void { $callsCount = $this->callsAmount(); @@ -57,9 +57,9 @@ public function toHaveBeenCalled(): void $this->assert($callsCount > 0, $message); } - public function toHaveBeenCalledOnce(): void + public function calledOnce(): void { - $this->toHaveBeenCalledTimes(1); + $this->calledTimes(1); } public function dd(): never diff --git a/src/Methods/Mocker.php b/src/Methods/Mocker.php index e74e083..7911473 100644 --- a/src/Methods/Mocker.php +++ b/src/Methods/Mocker.php @@ -41,7 +41,7 @@ public function __construct( if (in_array($methodName, ['__construct', '__get']) || in_array($methodName, $methodNames) || str_starts_with($methodName, '__moock') - || $method->isStatic() // TODO: Why did I exclude these from being mocked in the first place? + || $method->isStatic() || $method->isFinal() ) { continue; diff --git a/src/Mock.php b/src/Mock.php index d5b61ad..73ab0dc 100644 --- a/src/Mock.php +++ b/src/Mock.php @@ -104,7 +104,7 @@ private static function codeToMock(string $code): MockedClassInterface /** * @param Closure(Closure(Closure $expectedMethod): Expectation $expect): void $expectation */ - public static function expect(Closure $expectation): void + public static function verify(Closure $expectation): void { $mockExpector = new MockExpector(); $mockExpector->validate($expectation); diff --git a/src/MockMethod.php b/src/MockMethod.php index c529ded..2b2f68e 100644 --- a/src/MockMethod.php +++ b/src/MockMethod.php @@ -26,7 +26,7 @@ public function __construct( $this->methodName = $this->ref->getName(); } - public function filter(mixed ...$filters): static + public function allow(mixed ...$filters): static { $this->classMock->__filter($this->methodName, ...$filters); @@ -47,14 +47,14 @@ public function void(): static return $this; } - public function forceReturn(mixed $returnValue): static + public function returns(mixed $returnValue): static { $this->classMock->__replace($this->methodName, fn () => $returnValue); return $this; } - public function forceReturnSequence(array $values): static + public function returnsSequence(array $values): static { $this->classMock->__replace($this->methodName, function () use (&$values): mixed { return array_shift($values); @@ -66,7 +66,7 @@ public function forceReturnSequence(array $values): static /** * @param class-string $exception */ - public function throwsException(string $exception): static + public function throws(string $exception): static { $this->classMock->__replace($this->methodName, function () use ($exception): never { throw new $exception(); @@ -75,7 +75,7 @@ public function throwsException(string $exception): static return $this; } - public function expect(): Expectation + public function assert(): Expectation { return new Expectation( $this->methodName, diff --git a/tests/Documentation/AssertingExpectationsTest.php b/tests/Documentation/AssertingExpectationsTest.php index e2a2e4b..17cb286 100644 --- a/tests/Documentation/AssertingExpectationsTest.php +++ b/tests/Documentation/AssertingExpectationsTest.php @@ -35,12 +35,12 @@ protected function setUp(): void public function it_asserts_method_not_called_at_all(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->not() - ->toHaveBeenCalled(); + ->called(); } #[Example(null, 'Asserting the method was called at all.')] @@ -48,13 +48,13 @@ public function it_asserts_method_not_called_at_all(): void public function it_asserts_method_was_called(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() - ->toHaveBeenCalled(); + ->assert() + ->called(); } #[Example(null, 'Asserting the method was called exactly once.')] @@ -62,13 +62,13 @@ public function it_asserts_method_was_called(): void public function it_asserts_method_called_once(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() - ->toHaveBeenCalledOnce(); + ->assert() + ->calledOnce(); } #[Example(null, 'Asserting the method was not called exactly once.')] @@ -76,15 +76,15 @@ public function it_asserts_method_called_once(): void public function it_asserts_method_not_called_once(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->not() - ->toHaveBeenCalledOnce(); + ->calledOnce(); } #[Example(null, 'Asserting the method was called _n_ times.')] @@ -92,14 +92,14 @@ public function it_asserts_method_not_called_once(): void public function it_asserts_method_called_n_times(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() - ->toHaveBeenCalledTimes(2); + ->assert() + ->calledTimes(2); } #[Example(null, 'Asserting the method was not called _n_ times.')] @@ -107,14 +107,14 @@ public function it_asserts_method_called_n_times(): void public function it_asserts_method_not_called_n_times(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->not() - ->toHaveBeenCalledTimes(2); + ->calledTimes(2); } #[Example('Asserting method was called with specific input', 'You can assert a method was called with specific input by passing the expected arguments into `with()`.')] @@ -122,14 +122,14 @@ public function it_asserts_method_not_called_n_times(): void public function it_asserts_method_was_called_with_specific_input(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->with('my-email@domain.com') - ->toHaveBeenCalled(); + ->called(); } #[Example(null, 'This can of course also be reversed.')] @@ -137,15 +137,15 @@ public function it_asserts_method_was_called_with_specific_input(): void public function it_asserts_method_was_called_not_with_specific_input(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() - ->not() + ->assert() ->with('other-email@domain.com') - ->toHaveBeenCalled(); + ->not() + ->called(); } #[Example(null, 'Rather than being tied to static values, you can pass a closure as well.')] @@ -153,14 +153,14 @@ public function it_asserts_method_was_called_not_with_specific_input(): void public function it_asserts_method_was_called_with_value_validated_by_closure(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('my-email@domain.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->with(fn (string $email) => str_ends_with($email, '@domain.com')) - ->toHaveBeenCalled(); + ->called(); } #[Example(null, 'If you only care about a specific argument, you can use named arguments.')] @@ -170,9 +170,9 @@ public function it_asserts_method_was_called_with_specific_named_arguments(): vo $this->mock->createUser('my-email@domain.com', 'my-username', 'password'); Mock::method($this->mock->createUser(...)) - ->expect() + ->assert() ->with(email: 'my-email@domain.com', password: 'password') - ->toHaveBeenCalled(); + ->called(); } #[Example(null, 'Of course, closures can be used here too.')] @@ -182,11 +182,11 @@ public function it_asserts_method_was_called_with_specific_named_arguments_and_c $this->mock->createUser('my-email@domain.com', 'my-username', 'password'); Mock::method($this->mock->createUser(...)) - ->expect() + ->assert() ->with( email: fn (string $email) => str_ends_with($email, '@domain.com'), password: 'password', - )->toHaveBeenCalled(); + )->called(); } #[ShowUse(Str::class)] @@ -195,14 +195,14 @@ public function it_asserts_method_was_called_with_specific_named_arguments_and_c public function it_asserts_string_contains_helper(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('test@mail.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->with(Str::contains('@mail.com')) - ->toHaveBeenCalled(); + ->called(); } #[ShowUse(Str::class)] @@ -211,14 +211,14 @@ public function it_asserts_string_contains_helper(): void public function it_asserts_string_length_helper(): void { Mock::method($this->mock->userExists(...)) - ->forceReturn(true); + ->returns(true); $this->mock->userExists('test@mail.com'); Mock::method($this->mock->userExists(...)) - ->expect() + ->assert() ->with(Str::length(13)) - ->toHaveBeenCalled(); + ->called(); } #[ShowUse(Date::class)] @@ -228,14 +228,14 @@ public function it_asserts_string_length_helper(): void public function it_asserts_date_before_helper(): void { Mock::method($this->mock->getUsersCreatedBefore(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersCreatedBefore(new DateTime('2024-12-31')); Mock::method($this->mock->getUsersCreatedBefore(...)) - ->expect() + ->assert() ->with(Date::before(new DateTime('2025-01-01 12:00:00'))) - ->toHaveBeenCalled(); + ->called(); } #[ShowUse(Date::class)] @@ -245,14 +245,14 @@ public function it_asserts_date_before_helper(): void public function it_asserts_date_after_helper(): void { Mock::method($this->mock->getUsersCreatedBefore(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersCreatedBefore(new DateTime('2025-01-02')); Mock::method($this->mock->getUsersCreatedBefore(...)) - ->expect() + ->assert() ->with(Date::after(new DateTime('2025-01-01 12:00:00'))) - ->toHaveBeenCalled(); + ->called(); } #[ShowUse(Number::class)] @@ -261,14 +261,14 @@ public function it_asserts_date_after_helper(): void public function it_asserts_number_lt_helper(): void { Mock::method($this->mock->getUsersByAge(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersByAge(50); Mock::method($this->mock->getUsersByAge(...)) - ->expect() + ->assert() ->with(Number::lt(100)) - ->toHaveBeenCalled(); + ->called(); } #[ShowUse(Number::class)] @@ -277,14 +277,14 @@ public function it_asserts_number_lt_helper(): void public function it_asserts_number_gt_helper(): void { Mock::method($this->mock->getUsersByAge(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersByAge(75); Mock::method($this->mock->getUsersByAge(...)) - ->expect() + ->assert() ->with(Number::gt(50)) - ->toHaveBeenCalled(); + ->called(); } #[ShowUse(Number::class)] @@ -293,14 +293,14 @@ public function it_asserts_number_gt_helper(): void public function it_asserts_number_range_helper(): void { Mock::method($this->mock->getUsersByAge(...)) - ->forceReturn([]); + ->returns([]); $this->mock->getUsersByAge(15); Mock::method($this->mock->getUsersByAge(...)) - ->expect() + ->assert() ->with(Number::range(10, 20)) - ->toHaveBeenCalled(); + ->called(); } #[ShowUse(Arr::class)] @@ -311,9 +311,9 @@ public function it_asserts_array_count_helper(): void $this->mock->deleteUsersByEmail(['a','b','c']); Mock::method($this->mock->deleteUsersByEmail(...)) - ->expect() + ->assert() ->with(Arr::count(3)) - ->toHaveBeenCalled(); + ->called(); } #[ShowUse(Arr::class)] @@ -328,11 +328,11 @@ public function it_asserts_array_partial_helper(): void ]); Mock::method($this->mock->deleteUsersByEmail(...)) - ->expect() + ->assert() ->with(Arr::partial([ 0 => 'some-email@example.com', 2 => fn ($email) => str_ends_with($email, '@example.com'), ])) - ->toHaveBeenCalled(); + ->called(); } } diff --git a/tests/Documentation/CompatibilityTest.php b/tests/Documentation/CompatibilityTest.php index fa110ec..1c71692 100644 --- a/tests/Documentation/CompatibilityTest.php +++ b/tests/Documentation/CompatibilityTest.php @@ -32,8 +32,8 @@ public function it_can_register_arbitrary_assertion_methods(): void $mock = Mock::interface(UserServiceInterface::class); Mock::method($mock->createUser(...)) - ->expect() - ->toHaveBeenCalled(); // This now uses our custom assert, which doesn't throw exceptions on failure + ->assert() + ->called(); // This now uses our custom assert, which doesn't throw exceptions on failure $this->assertTrue($usedAssert); } diff --git a/tests/Documentation/FilteringMethodsTest.php b/tests/Documentation/FilteringMethodsTest.php index d06128a..66058f7 100644 --- a/tests/Documentation/FilteringMethodsTest.php +++ b/tests/Documentation/FilteringMethodsTest.php @@ -27,14 +27,14 @@ protected function setUp(): void #[Example( 'Filtering an argument', - 'To filter arguments that are allowed into a method, you can use the `filter()` method.', + 'To filter arguments that are allowed into a method, you can use the following.', )] #[Test] public function it_can_filter_method_args(): void { Mock::method($this->mock->userExists(...)) - ->filter('my-email@domain.com') - ->forceReturn(true); + ->allow('my-email@domain.com') + ->returns(true); $this->assertTrue($this->mock->userExists('my-email@domain.com')); @@ -51,7 +51,7 @@ public function it_can_filter_method_args(): void public function it_can_filter_method_args_using_named_props(): void { Mock::method($this->mock->createUser(...)) - ->filter(username: 'my-username'); + ->allow(username: 'my-username'); $this->mock->createUser('my-email@domain.com', 'my-username', 'password'); @@ -68,8 +68,8 @@ public function it_can_filter_method_args_using_named_props(): void public function it_can_filter_method_args_with_closures(): void { Mock::method($this->mock->userExists(...)) - ->filter(fn (string $email) => in_array($email, ['first@mail.com', 'second@mail.com'])) - ->forceReturn(true); + ->allow(fn (string $email) => in_array($email, ['first@mail.com', 'second@mail.com'])) + ->returns(true); $this->mock->userExists('first@mail.com'); $this->mock->userExists('second@mail.com'); diff --git a/tests/Documentation/OrderExpectationTest.php b/tests/Documentation/OrderExpectationTest.php index fd6bca0..b12f3b5 100644 --- a/tests/Documentation/OrderExpectationTest.php +++ b/tests/Documentation/OrderExpectationTest.php @@ -33,18 +33,18 @@ protected function setUp(): void #[Test] public function it_can_verify_order_of_mocked_methods(): void { - Mock::method($this->mock->isValidEmail(...))->forceReturn(true); - Mock::method($this->mock->userExists(...))->forceReturn(false); + Mock::method($this->mock->isValidEmail(...))->returns(true); + Mock::method($this->mock->userExists(...))->returns(false); $this->mock->isValidEmail('mail@domain.com'); $this->mock->userExists('mail@domain.com'); $this->mock->createUser('mail@domain.com', 'username', 'password'); // Asserting the methods are called in the right order only - Mock::expect(function ($expect): void { - $expect($this->mock->isValidEmail(...)); - $expect($this->mock->userExists(...)); - $expect($this->mock->createUser(...)); + Mock::verify(function ($assert): void { + $assert($this->mock->isValidEmail(...)); + $assert($this->mock->userExists(...)); + $assert($this->mock->createUser(...)); }); } @@ -52,19 +52,19 @@ public function it_can_verify_order_of_mocked_methods(): void #[Test] public function it_can_verify_order_of_mocked_methods_and_args(): void { - Mock::method($this->mock->isValidEmail(...))->forceReturn(true); // @hide - Mock::method($this->mock->userExists(...))->forceReturn(false); // @hide + Mock::method($this->mock->isValidEmail(...))->returns(true); // @hide + Mock::method($this->mock->userExists(...))->returns(false); // @hide $this->mock->isValidEmail('mail@domain.com'); // @hide $this->mock->userExists('mail@domain.com'); // @hide $this->mock->createUser('mail@domain.com', 'username', 'password'); // @hide - Mock::expect(function (Closure $expect): void { - $expect($this->mock->isValidEmail(...))->with('mail@domain.com'); - $expect($this->mock->userExists(...)); // Argument isn't verified, just order + Mock::verify(function (Closure $assert): void { + $assert($this->mock->isValidEmail(...))->with('mail@domain.com'); + $assert($this->mock->userExists(...)); // Argument isn't verified, just order // Only mail and password are validated - $expect($this->mock->createUser(...))->with('mail@domain.com', password: 'password'); + $assert($this->mock->createUser(...))->with('mail@domain.com', password: 'password'); }); } @@ -75,18 +75,18 @@ public function it_can_verify_order_between_different_mocks(): void $userService = Mock::interface(UserServiceInterface::class); $productsService = Mock::interface(ProductServiceInterface::class); - Mock::method($userService->isValidEmail(...))->forceReturn(true); // @hide - Mock::method($productsService->productExists(...))->forceReturn(true); // @hide - Mock::method($productsService->purchase(...))->forceReturn(true); // @hide + Mock::method($userService->isValidEmail(...))->returns(true); // @hide + Mock::method($productsService->productExists(...))->returns(true); // @hide + Mock::method($productsService->purchase(...))->returns(true); // @hide $productsService->productExists(123); $userService->isValidEmail('mail@domain.com'); $productsService->purchase(123, 'mail@domain.com'); - Mock::expect(function (Closure $expect) use ($userService, $productsService): void { - $expect($productsService->productExists(...))->with(123); - $expect($userService->isValidEmail(...))->with('mail@domain.com'); - $expect($productsService->purchase(...))->with(123, 'mail@domain.com'); + Mock::verify(function (Closure $assert) use ($userService, $productsService): void { + $assert($productsService->productExists(...))->with(123); + $assert($userService->isValidEmail(...))->with('mail@domain.com'); + $assert($productsService->purchase(...))->with(123, 'mail@domain.com'); }); } } diff --git a/tests/Documentation/ReplacingMethodTest.php b/tests/Documentation/ReplacingMethodTest.php index bbf6ea9..63382f4 100644 --- a/tests/Documentation/ReplacingMethodTest.php +++ b/tests/Documentation/ReplacingMethodTest.php @@ -45,7 +45,7 @@ public function it_can_replace_methods(): void #[Test] public function it_can_force_return_a_value(): void { - Mock::method($this->mock->userExists(...))->forceReturn(true); + Mock::method($this->mock->userExists(...))->returns(true); $this->assertTrue($this->mock->userExists('some-email@domain.com')); } @@ -55,7 +55,7 @@ public function it_can_force_return_a_value(): void #[Test] public function it_can_force_return_a_sequence_of_values(): void { - Mock::method($this->mock->userExists(...))->forceReturnSequence([true, true, false]); + Mock::method($this->mock->userExists(...))->returnsSequence([true, true, false]); $this->assertTrue($this->mock->userExists('some-email@domain.com')); $this->assertTrue($this->mock->userExists('some-email@domain.com')); @@ -70,7 +70,7 @@ public function it_can_force_return_a_sequence_of_values(): void #[Test] public function it_can_force_an_exception(): void { - Mock::method($this->mock->createUser(...))->throwsException(RuntimeException::class); + Mock::method($this->mock->createUser(...))->throws(RuntimeException::class); $this->expectException(RuntimeException::class); diff --git a/tests/MockClassTest.php b/tests/MockClassTest.php index 2e67f3a..b0d899f 100644 --- a/tests/MockClassTest.php +++ b/tests/MockClassTest.php @@ -37,8 +37,8 @@ public function test_it_does_not_require_replacing_methods_for_expectations(): v $mock->testWithTrueDefault(); Mock::method($mock->testWithTrueDefault(...)) - ->expect() - ->toHaveBeenCalled(); + ->assert() + ->called(); } public function test_it_keeps_track_of_amount_of_calls(): void @@ -54,7 +54,7 @@ public function test_it_keeps_track_of_amount_of_calls(): void $mock->myMethod(); Mock::method($mock->myMethod(...)) - ->expect()->toHaveBeenCalledTimes(4); + ->assert()->calledTimes(4); } public function test_method_input_is_passed_to_replacement(): void @@ -75,7 +75,7 @@ public function test_method_input_is_passed_to_replacement(): void ); Mock::method($mock->myOtherMethod(...)) - ->expect()->toHaveBeenCalledOnce(); + ->assert()->calledOnce(); } public function test_it_can_partially_mock(): void @@ -102,7 +102,7 @@ public function non_replaced_methods_on_partial_mocks_are_fowarded(): void static::assertEquals('::original value::', $mock->myMethod()); static::assertTrue($mock->myMethodWasCalled); - Mock::method($mock->myMethod(...))->expect()->toHaveBeenCalledOnce(); + Mock::method($mock->myMethod(...))->assert()->calledOnce(); } public function test_it_can_set_arg_expectations(): void @@ -118,38 +118,38 @@ public function test_it_can_set_arg_expectations(): void $mock->myOtherMethod('::3::', '::3::'); Mock::method($mock->myOtherMethod(...)) - ->expect() + ->assert() ->with('::1::') - ->toHaveBeenCalled(); + ->called(); Mock::method($mock->myOtherMethod(...)) - ->expect() + ->assert() ->with('::1::') - ->toHaveBeenCalledTimes(3); + ->calledTimes(3); Mock::method($mock->myOtherMethod(...)) - ->expect() + ->assert() ->with('::1::', '::2::') - ->toHaveBeenCalledTimes(2); + ->calledTimes(2); Mock::method($mock->myOtherMethod(...)) - ->expect() + ->assert() ->with(fn (string $inputA) => $inputA === '::3::') - ->toHaveBeenCalledTimes(2); + ->calledTimes(2); Mock::method($mock->myOtherMethod(...)) - ->expect() + ->assert() ->with(inputB: fn (string $inputB) => $inputB === '::3::') - ->toHaveBeenCalledTimes(3); + ->calledTimes(3); } public function test_it_can_filter_allowed_inputs(): void { $mock = Mock::class(TestClass::class); - Mock::method($mock->myOtherMethod(...))->forceReturn([]); + Mock::method($mock->myOtherMethod(...))->returns([]); - Mock::method($mock->myOtherMethod(...))->filter('::A::', fn () => true); + Mock::method($mock->myOtherMethod(...))->allow('::A::', fn () => true); $mock->myOtherMethod('::A::', 'asdf'); $mock->myOtherMethod('::A::', 'ghjk'); diff --git a/tests/MockExpectTest.php b/tests/MockExpectTest.php index e485739..420e884 100644 --- a/tests/MockExpectTest.php +++ b/tests/MockExpectTest.php @@ -26,7 +26,7 @@ public function it_can_expect_order_of_methods_called_on_same_mock(): void $mock = Mock::interface(UserServiceInterface::class); - Mock::method($mock->getUsersByAge(...))->forceReturn([]); + Mock::method($mock->getUsersByAge(...))->returns([]); $mock->createUser('a', 'b', 'c'); $mock->getUsersByAge(123); @@ -34,7 +34,7 @@ public function it_can_expect_order_of_methods_called_on_same_mock(): void $mock->createUser('a', 'b', 'c'); $mock->getUsersByAge(123); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->createUser(...)); $expect($mock->getUsersByAge(...)); $expect($mock->createUser(...)); @@ -46,7 +46,7 @@ public function it_can_expect_order_of_methods_called_on_same_mock(): void $mock = Mock::interface(UserServiceInterface::class); - Mock::method($mock->getUsersByAge(...))->forceReturn([]); + Mock::method($mock->getUsersByAge(...))->returns([]); $mock->createUser('a', 'b', 'c'); $mock->getUsersByAge(123); @@ -54,7 +54,7 @@ public function it_can_expect_order_of_methods_called_on_same_mock(): void $mock->createUser('a', 'b', 'c'); $mock->getUsersByAge(123); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->getUsersByAge(...)); $expect($mock->createUser(...)); $expect($mock->createUser(...)); @@ -86,7 +86,7 @@ public function it_can_expect_order_of_methods_called_on_several_mocks(): void $mock2->createUser('a', 'b', 'c'); $mock1->createUser('a', 'b', 'c'); - Mock::expect(function (Closure $expect) use ($mock1, $mock2, $mock3): void { + Mock::verify(function (Closure $expect) use ($mock1, $mock2, $mock3): void { $expect($mock1->createUser(...)); $expect($mock2->createUser(...)); $expect($mock3->createUser(...)); @@ -109,7 +109,7 @@ public function it_can_expect_order_of_methods_called_on_several_mocks(): void $mock2->createUser('a', 'b', 'c'); $mock1->createUser('a', 'b', 'c'); - Mock::expect(function (Closure $expect) use ($mock1, $mock2, $mock3): void { + Mock::verify(function (Closure $expect) use ($mock1, $mock2, $mock3): void { $expect($mock1->createUser(...)); $expect($mock2->createUser(...)); $expect($mock3->createUser(...)); @@ -136,7 +136,7 @@ public function it_can_expect_methods_with_args_in_specific_order(): void $mock->createUser('d', 'e', 'f'); $mock->createUser('g', 'h', 'i'); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->createUser(...))->with('a', 'b', 'c'); $expect($mock->createUser(...))->with('d', 'e', 'f'); $expect($mock->createUser(...))->with('g', 'h', 'i'); @@ -150,7 +150,7 @@ public function it_can_expect_methods_with_args_in_specific_order(): void $mock->createUser('g', 'h', 'i'); $mock->createUser('d', 'e', 'f'); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->createUser(...))->with('a', 'b', 'c'); $expect($mock->createUser(...))->with('d', 'e', 'f'); $expect($mock->createUser(...))->with('g', 'h', 'i'); @@ -170,7 +170,7 @@ public function it_ignores_random_calls_in_between(): void $mock = Mock::interface(UserServiceInterface::class); - Mock::method($mock->getUsersByAge(...))->forceReturn([]); + Mock::method($mock->getUsersByAge(...))->returns([]); $mock->getUsersByAge(123); $mock->createUser('a', 'b', 'c'); @@ -183,7 +183,7 @@ public function it_ignores_random_calls_in_between(): void $mock->getUsersByAge(123); $mock->createUser('g', 'h', 'i'); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->createUser(...))->with('a', 'b', 'c'); $expect($mock->createUser(...))->with('d', 'e', 'f'); $expect($mock->createUser(...))->with('g', 'h', 'i'); @@ -207,7 +207,7 @@ public function it_can_expect_arg_and_non_arg_specific(): void $mock->createUser('d', 'e', 'f'); $mock->createUser('g', 'h', 'i'); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->createUser(...))->with('a', 'b', 'c'); $expect($mock->createUser(...)); $expect($mock->createUser(...))->with('g', 'h', 'i'); @@ -221,7 +221,7 @@ public function it_can_expect_arg_and_non_arg_specific(): void $mock->createUser('g', 'h', 'i'); $mock->createUser('d', 'e', 'f'); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->createUser(...))->with('a', 'b', 'c'); $expect($mock->createUser(...))->with('d', 'e', 'f'); $expect($mock->createUser(...)); @@ -246,7 +246,7 @@ public function it_can_expect_arg_with_fancy_syntax(): void $mock->createUser('d', 'e', 'f'); $mock->createUser('g', 'h', 'i'); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->createUser(...))->with(email: 'a', password: 'c'); $expect($mock->createUser(...))->with(email: 'd', password: 'f'); }); @@ -259,7 +259,7 @@ public function it_can_expect_arg_with_fancy_syntax(): void $mock->createUser('a', 'c', 'b'); $mock->createUser('d', 'f', 'e'); - Mock::expect(function (Closure $expect) use ($mock): void { + Mock::verify(function (Closure $expect) use ($mock): void { $expect($mock->createUser(...))->with(email: 'a', password: 'c'); $expect($mock->createUser(...))->with(email: 'd', password: 'f'); }); diff --git a/tests/MockInterfaceTest.php b/tests/MockInterfaceTest.php index 796a584..9d2bd53 100644 --- a/tests/MockInterfaceTest.php +++ b/tests/MockInterfaceTest.php @@ -41,7 +41,7 @@ public function test_it_keeps_track_of_amount_of_calls(): void $mock->myMethod(); Mock::method($mock->myMethod(...)) - ->expect()->toHaveBeenCalledTimes(4); + ->assert()->calledTimes(4); } public function test_method_input_is_passed_to_replacement(): void @@ -57,7 +57,7 @@ public function test_method_input_is_passed_to_replacement(): void $mock->myOtherMethod('::input a::', '::input b::'); Mock::method($mock->myOtherMethod(...)) - ->expect()->toHaveBeenCalledOnce(); + ->assert()->calledOnce(); } public function test_it_can_mock_several_interfaces(): void