From c4289533de7807dd6e2a8178d68cd8d9c4ca140f Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 2 Apr 2024 14:49:54 +0200 Subject: [PATCH] Switch to Mockery for mocking --- src/PSR16Adapter.php | 51 +++++---------------- tests/PSR16AdapterTest.php | 91 +++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 85 deletions(-) diff --git a/src/PSR16Adapter.php b/src/PSR16Adapter.php index a9914f9..9071a31 100644 --- a/src/PSR16Adapter.php +++ b/src/PSR16Adapter.php @@ -15,7 +15,7 @@ final class PSR16Adapter implements SimpleCacheInterface { public function __construct( - private readonly CacheInterface $cache + private readonly CacheInterface $cache, ) { } @@ -25,9 +25,6 @@ public function __construct( */ public function get(string $key, mixed $default = null) { - /** - * @psalm-suppress TooManyTemplateParams - */ return await($this->cache->get($key, $default)); } @@ -37,48 +34,32 @@ public function get(string $key, mixed $default = null) */ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null) { - /** - * @var bool - * @psalm-suppress TooManyTemplateParams - */ return await($this->cache->set($key, $value, $this->convertToSeconds($ttl))); } - /** - * @inheritDoc - */ + /** @inheritDoc */ public function delete(string $key) { - /** - * @var bool - * @psalm-suppress TooManyTemplateParams - */ return await($this->cache->delete($key)); } - /** - * @inheritDoc - */ + /** @inheritDoc */ public function clear() { - /** - * @var bool - * @psalm-suppress TooManyTemplateParams - */ return await($this->cache->clear()); } /** * @inheritDoc + * @psalm-suppress InvalidReturnType * @phpstan-ignore-next-line */ public function getMultiple(iterable $keys, mixed $default = null): iterable { /** - * @var iterable - * @phpstan-ignore-next-line - * @psalm-suppress TooManyTemplateParams + * @psalm-suppress InvalidReturnStatement * @psalm-suppress MixedArgumentTypeCoercion + * @phpstan-ignore-next-line */ return await($this->cache->getMultiple(iterable_to_array($keys), $default)); } @@ -90,39 +71,29 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable public function setMultiple(iterable $values, DateInterval|int|null $ttl = null) { /** - * @var bool - * @psalm-suppress TooManyTemplateParams * @psalm-suppress MixedArgumentTypeCoercion + * @phpstan-ignore-next-line */ return await($this->cache->setMultiple(iterable_to_array($values), $this->convertToSeconds($ttl))); } - /** - * @inheritDoc - */ + /** @inheritDoc */ public function deleteMultiple(iterable $keys) { /** - * @var bool - * @phpstan-ignore-next-line - * @psalm-suppress TooManyTemplateParams * @psalm-suppress MixedArgumentTypeCoercion + * @phpstan-ignore-next-line */ return await($this->cache->deleteMultiple(iterable_to_array($keys))); } - /** - * @inheritDoc - */ + /** @inheritDoc */ public function has(string $key) { - /** - * @var bool - * @psalm-suppress TooManyTemplateParams - */ return await($this->cache->has($key)); } + /** @phpstan-ignore-next-line */ private function convertToSeconds(DateInterval|int|null $ttl): int|null { if ($ttl instanceof DateInterval) { diff --git a/tests/PSR16AdapterTest.php b/tests/PSR16AdapterTest.php index 29c8e59..fa71d04 100644 --- a/tests/PSR16AdapterTest.php +++ b/tests/PSR16AdapterTest.php @@ -6,6 +6,7 @@ use DateInterval; use Exception; +use Mockery; use React\Cache\CacheInterface; use WyriHaximus\AsyncTestUtilities\AsyncTestCase; use WyriHaximus\React\Cache\PSR16Adapter; @@ -17,49 +18,49 @@ final class PSR16AdapterTest extends AsyncTestCase { public function testGet(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; $value = 'value'; - $client->get($key, null)->shouldBeCalled()->willReturn(resolve($value)); - self::assertSame($value, (new PSR16Adapter($client->reveal()))->get($key)); + $client->shouldReceive('get')->with($key, null)->andReturn(resolve($value)); + self::assertSame($value, (new PSR16Adapter($client))->get($key)); } public function testGetNonExistant(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; - $client->get($key, null)->shouldBeCalled()->willReturn(resolve(null)); - self::assertNull((new PSR16Adapter($client->reveal()))->get($key)); + $client->shouldReceive('get')->with($key, null)->andReturn(resolve(null)); + self::assertNull((new PSR16Adapter($client))->get($key)); } public function testSet(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; $value = 'value'; - $client->set($key, $value, null)->shouldBeCalled()->willReturn(resolve(true)); - (new PSR16Adapter($client->reveal()))->set($key, $value); + $client->shouldReceive('set')->with($key, $value, null)->andReturn(resolve(true)); + self::assertTrue((new PSR16Adapter($client))->set($key, $value)); } public function testSetTtl(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; $value = 'value'; $ttl = 123; - $client->set($key, $value, $ttl)->shouldBeCalled()->willReturn(resolve(true)); - self::assertTrue((new PSR16Adapter($client->reveal()))->set($key, $value, $ttl)); + $client->shouldReceive('set')->with($key, $value, $ttl)->andReturn(resolve(true)); + self::assertTrue((new PSR16Adapter($client))->set($key, $value, $ttl)); } public function testSetDateIntervalTtl(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; $value = 'value'; $dateIntervalTtl = new DateInterval('PT123S'); $ttl = 123; - $client->set($key, $value, $ttl)->shouldBeCalled()->willReturn(resolve(true)); - self::assertTrue((new PSR16Adapter($client->reveal()))->set($key, $value, $dateIntervalTtl)); + $client->shouldReceive('set')->with($key, $value, $ttl)->andReturn(resolve(true)); + self::assertTrue((new PSR16Adapter($client))->set($key, $value, $dateIntervalTtl)); } public function testSetTtlException(): void @@ -68,12 +69,12 @@ public function testSetTtlException(): void self::expectException($exception::class); self::expectExceptionMessage($exception->getMessage()); - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; $value = 'value'; $ttl = 123; - $client->set($key, $value, $ttl)->shouldBeCalled()->willReturn(reject($exception)); - self::assertFalse((new PSR16Adapter($client->reveal()))->set($key, $value, $ttl)); + $client->shouldReceive('set')->with($key, $value, $ttl)->andReturn(reject($exception)); + self::assertFalse((new PSR16Adapter($client))->set($key, $value, $ttl)); } public function testSetException(): void @@ -82,19 +83,19 @@ public function testSetException(): void self::expectException($exception::class); self::expectExceptionMessage($exception->getMessage()); - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; $value = 'value'; - $client->set($key, $value, null)->shouldBeCalled()->willReturn(reject($exception)); - self::assertFalse((new PSR16Adapter($client->reveal()))->set($key, $value)); + $client->shouldReceive('set')->with($key, $value, null)->andReturn(reject($exception)); + self::assertFalse((new PSR16Adapter($client))->set($key, $value)); } public function testDelete(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; - $client->delete($key)->shouldBeCalled()->willReturn(resolve(true)); - (new PSR16Adapter($client->reveal()))->delete($key); + $client->shouldReceive('delete')->with($key)->andReturn(resolve(true)); + self::assertTrue((new PSR16Adapter($client))->delete($key)); } public function testDeleteException(): void @@ -103,26 +104,26 @@ public function testDeleteException(): void self::expectException($exception::class); self::expectExceptionMessage($exception->getMessage()); - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; - $client->delete($key)->shouldBeCalled()->willReturn(reject($exception)); - (new PSR16Adapter($client->reveal()))->delete($key); + $client->shouldReceive('delete')->with($key)->andReturn(reject($exception)); + (new PSR16Adapter($client))->delete($key); } public function testHas(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; - $client->has($key)->shouldBeCalled()->willReturn(resolve(true)); - (new PSR16Adapter($client->reveal()))->has($key); + $client->shouldReceive('has')->with($key)->andReturn(resolve(true)); + self::assertTrue((new PSR16Adapter($client))->has($key)); } public function testDeleteMultiple(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; - $client->deleteMultiple([$key])->shouldBeCalled()->willReturn(resolve(true)); - (new PSR16Adapter($client->reveal()))->deleteMultiple([$key]); + $client->shouldReceive('deleteMultiple')->with([$key])->andReturn(resolve(true)); + self::assertTrue((new PSR16Adapter($client))->deleteMultiple([$key])); } public function testDeleteMultipleException(): void @@ -131,35 +132,35 @@ public function testDeleteMultipleException(): void self::expectException($exception::class); self::expectExceptionMessage($exception->getMessage()); - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; - $client->deleteMultiple([$key])->shouldBeCalled()->willReturn(reject($exception)); - (new PSR16Adapter($client->reveal()))->deleteMultiple([$key]); + $client->shouldReceive('deleteMultiple')->with([$key])->andReturn(reject($exception)); + (new PSR16Adapter($client))->deleteMultiple([$key]); } public function testCLear(): void { - $client = $this->prophesize(CacheInterface::class); - $client->clear()->shouldBeCalled()->willReturn(resolve(true)); - (new PSR16Adapter($client->reveal()))->clear(); + $client = Mockery::mock(CacheInterface::class); + $client->shouldReceive('clear')->andReturn(resolve(true)); + self::assertTrue((new PSR16Adapter($client))->clear()); } public function testSetMultiple(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; $value = 'value'; $ttl = 123; - $client->setMultiple([$key => $value], $ttl)->shouldBeCalled()->willReturn(resolve(true)); - self::assertTrue((new PSR16Adapter($client->reveal()))->setMultiple([$key => $value], $ttl)); + $client->shouldReceive('setMultiple')->with([$key => $value], $ttl)->andReturn(resolve(true)); + self::assertTrue((new PSR16Adapter($client))->setMultiple([$key => $value], $ttl)); } public function testGetMultiple(): void { - $client = $this->prophesize(CacheInterface::class); + $client = Mockery::mock(CacheInterface::class); $key = 'key'; $value = 'value'; - $client->getMultiple([$key], null)->shouldBeCalled()->willReturn(resolve([$key => $value])); - self::assertSame([$key => $value], (new PSR16Adapter($client->reveal()))->getMultiple([$key])); + $client->shouldReceive('getMultiple')->with([$key], null)->andReturn(resolve([$key => $value])); + self::assertSame([$key => $value], (new PSR16Adapter($client))->getMultiple([$key])); } }