diff --git a/src/Exceptions/MissingDependency.php b/src/Exceptions/MissingDependency.php new file mode 100644 index 00000000..e7d7940d --- /dev/null +++ b/src/Exceptions/MissingDependency.php @@ -0,0 +1,24 @@ +afterAll->set($closure); } + +if (!function_exists('mock')) { + /** + * Creates a new mock with the given class or object. + * + * @param string|object $object + */ + function mock($object): Mock + { + return new Mock($object); + } +} diff --git a/src/Mock.php b/src/Mock.php new file mode 100644 index 00000000..31d35fdb --- /dev/null +++ b/src/Mock.php @@ -0,0 +1,69 @@ +mock = Mockery::mock($object); + } + + /** + * Define mock expectations. + * + * @param mixed ...$methods + * + * @return \Mockery\MockInterface|\Mockery\LegacyMockInterface + */ + public function expect(...$methods) + { + foreach ($methods as $method => $result) { + /* @phpstan-ignore-next-line */ + $this->mock + ->shouldReceive((string) $method) + ->andReturn($result); + } + + return $this->mock; + } + + /** + * Proxies calls to the original mock object. + * + * @param array $arguments + * + * @return mixed + */ + public function __call(string $method, array $arguments) + { + /* @phpstan-ignore-next-line */ + return $this->mock->{$method}($arguments); + } +} diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 625557a8..8fa58f1a 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -63,6 +63,7 @@ ✓ it allows to call underlying protected/private methods ✓ it throws error if method do not exist ✓ it can forward unexpected calls to any global function + ✓ it can use helpers from helpers file PASS Tests\Features\HigherOrderTests ✓ it proxies calls to object @@ -77,7 +78,8 @@ ✓ it will throw exception from call if no macro exists PASS Tests\Features\Mocks - ✓ it has bar + ✓ it can mock methods + ✓ access to the mock object PASS Tests\Features\PendingHigherOrderTests ✓ get 'foo' → get 'bar' → expect true → toBeTrue @@ -221,5 +223,9 @@ ✓ it is a test ✓ it uses correct parent class +<<<<<<< HEAD Tests: 7 skipped, 119 passed +======= + Tests: 7 skipped, 117 passed +>>>>>>> feat(mock): adds work in progress \ No newline at end of file diff --git a/tests/Features/Helpers.php b/tests/Features/Helpers.php index 3203da9b..0f399fa4 100644 --- a/tests/Features/Helpers.php +++ b/tests/Features/Helpers.php @@ -42,3 +42,5 @@ it('throws error if method do not exist', function () { })->throws(\ReflectionException::class, 'Call to undefined method PHPUnit\Framework\TestCase::name()'); it('can forward unexpected calls to any global function')->_assertThat(); + +it('can use helpers from helpers file')->myAssertTrue(true); diff --git a/tests/Features/Mocks.php b/tests/Features/Mocks.php index 41268524..51b896d5 100644 --- a/tests/Features/Mocks.php +++ b/tests/Features/Mocks.php @@ -1,17 +1,24 @@ shouldReceive('bar') - ->times(1) - ->andReturn(2); +it('can mock methods', function () { + $mock = mock(Http::class)->expect( + get: 'foo', + ); - $mock->bar(); -}); + expect($mock->get())->toBe('foo'); +})->skip(((float) phpversion()) < 8.0); + +test('access to the mock object', function () { + $mock = mock(Http::class); + expect($mock->expect())->toBeInstanceOf(MockInterface::class); + + expect($mock->shouldReceive())->toBeInstanceOf(CompositeExpectation::class); +})->skip(((float) phpversion()) < 8.0); diff --git a/tests/Helpers.php b/tests/Helpers.php index ee869257..57d38e06 100644 --- a/tests/Helpers.php +++ b/tests/Helpers.php @@ -1,11 +1,6 @@ assertTrue($value); }