Skip to content

Commit

Permalink
Switch to Mockery for mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Apr 2, 2024
1 parent 48a20d1 commit c428953
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 85 deletions.
51 changes: 11 additions & 40 deletions src/PSR16Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
final class PSR16Adapter implements SimpleCacheInterface
{
public function __construct(
private readonly CacheInterface $cache
private readonly CacheInterface $cache,
) {
}

Expand All @@ -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));
}

Expand All @@ -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<string, mixed>
* @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));
}
Expand All @@ -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) {
Expand Down
91 changes: 46 additions & 45 deletions tests/PSR16AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateInterval;
use Exception;
use Mockery;
use React\Cache\CacheInterface;
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
use WyriHaximus\React\Cache\PSR16Adapter;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]));
}
}

0 comments on commit c428953

Please sign in to comment.