Skip to content

Commit f919e90

Browse files
authored
Merge pull request #48 from WyriHaximus/switch-to-mockery-for-mocking
Switch to Mockery for mocking
2 parents 48a20d1 + c428953 commit f919e90

File tree

2 files changed

+57
-85
lines changed

2 files changed

+57
-85
lines changed

src/PSR16Adapter.php

+11-40
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
final class PSR16Adapter implements SimpleCacheInterface
1616
{
1717
public function __construct(
18-
private readonly CacheInterface $cache
18+
private readonly CacheInterface $cache,
1919
) {
2020
}
2121

@@ -25,9 +25,6 @@ public function __construct(
2525
*/
2626
public function get(string $key, mixed $default = null)
2727
{
28-
/**
29-
* @psalm-suppress TooManyTemplateParams
30-
*/
3128
return await($this->cache->get($key, $default));
3229
}
3330

@@ -37,48 +34,32 @@ public function get(string $key, mixed $default = null)
3734
*/
3835
public function set(string $key, mixed $value, DateInterval|int|null $ttl = null)
3936
{
40-
/**
41-
* @var bool
42-
* @psalm-suppress TooManyTemplateParams
43-
*/
4437
return await($this->cache->set($key, $value, $this->convertToSeconds($ttl)));
4538
}
4639

47-
/**
48-
* @inheritDoc
49-
*/
40+
/** @inheritDoc */
5041
public function delete(string $key)
5142
{
52-
/**
53-
* @var bool
54-
* @psalm-suppress TooManyTemplateParams
55-
*/
5643
return await($this->cache->delete($key));
5744
}
5845

59-
/**
60-
* @inheritDoc
61-
*/
46+
/** @inheritDoc */
6247
public function clear()
6348
{
64-
/**
65-
* @var bool
66-
* @psalm-suppress TooManyTemplateParams
67-
*/
6849
return await($this->cache->clear());
6950
}
7051

7152
/**
7253
* @inheritDoc
54+
* @psalm-suppress InvalidReturnType
7355
* @phpstan-ignore-next-line
7456
*/
7557
public function getMultiple(iterable $keys, mixed $default = null): iterable
7658
{
7759
/**
78-
* @var iterable<string, mixed>
79-
* @phpstan-ignore-next-line
80-
* @psalm-suppress TooManyTemplateParams
60+
* @psalm-suppress InvalidReturnStatement
8161
* @psalm-suppress MixedArgumentTypeCoercion
62+
* @phpstan-ignore-next-line
8263
*/
8364
return await($this->cache->getMultiple(iterable_to_array($keys), $default));
8465
}
@@ -90,39 +71,29 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
9071
public function setMultiple(iterable $values, DateInterval|int|null $ttl = null)
9172
{
9273
/**
93-
* @var bool
94-
* @psalm-suppress TooManyTemplateParams
9574
* @psalm-suppress MixedArgumentTypeCoercion
75+
* @phpstan-ignore-next-line
9676
*/
9777
return await($this->cache->setMultiple(iterable_to_array($values), $this->convertToSeconds($ttl)));
9878
}
9979

100-
/**
101-
* @inheritDoc
102-
*/
80+
/** @inheritDoc */
10381
public function deleteMultiple(iterable $keys)
10482
{
10583
/**
106-
* @var bool
107-
* @phpstan-ignore-next-line
108-
* @psalm-suppress TooManyTemplateParams
10984
* @psalm-suppress MixedArgumentTypeCoercion
85+
* @phpstan-ignore-next-line
11086
*/
11187
return await($this->cache->deleteMultiple(iterable_to_array($keys)));
11288
}
11389

114-
/**
115-
* @inheritDoc
116-
*/
90+
/** @inheritDoc */
11791
public function has(string $key)
11892
{
119-
/**
120-
* @var bool
121-
* @psalm-suppress TooManyTemplateParams
122-
*/
12393
return await($this->cache->has($key));
12494
}
12595

96+
/** @phpstan-ignore-next-line */
12697
private function convertToSeconds(DateInterval|int|null $ttl): int|null
12798
{
12899
if ($ttl instanceof DateInterval) {

tests/PSR16AdapterTest.php

+46-45
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use DateInterval;
88
use Exception;
9+
use Mockery;
910
use React\Cache\CacheInterface;
1011
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
1112
use WyriHaximus\React\Cache\PSR16Adapter;
@@ -17,49 +18,49 @@ final class PSR16AdapterTest extends AsyncTestCase
1718
{
1819
public function testGet(): void
1920
{
20-
$client = $this->prophesize(CacheInterface::class);
21+
$client = Mockery::mock(CacheInterface::class);
2122
$key = 'key';
2223
$value = 'value';
23-
$client->get($key, null)->shouldBeCalled()->willReturn(resolve($value));
24-
self::assertSame($value, (new PSR16Adapter($client->reveal()))->get($key));
24+
$client->shouldReceive('get')->with($key, null)->andReturn(resolve($value));
25+
self::assertSame($value, (new PSR16Adapter($client))->get($key));
2526
}
2627

2728
public function testGetNonExistant(): void
2829
{
29-
$client = $this->prophesize(CacheInterface::class);
30+
$client = Mockery::mock(CacheInterface::class);
3031
$key = 'key';
31-
$client->get($key, null)->shouldBeCalled()->willReturn(resolve(null));
32-
self::assertNull((new PSR16Adapter($client->reveal()))->get($key));
32+
$client->shouldReceive('get')->with($key, null)->andReturn(resolve(null));
33+
self::assertNull((new PSR16Adapter($client))->get($key));
3334
}
3435

3536
public function testSet(): void
3637
{
37-
$client = $this->prophesize(CacheInterface::class);
38+
$client = Mockery::mock(CacheInterface::class);
3839
$key = 'key';
3940
$value = 'value';
40-
$client->set($key, $value, null)->shouldBeCalled()->willReturn(resolve(true));
41-
(new PSR16Adapter($client->reveal()))->set($key, $value);
41+
$client->shouldReceive('set')->with($key, $value, null)->andReturn(resolve(true));
42+
self::assertTrue((new PSR16Adapter($client))->set($key, $value));
4243
}
4344

4445
public function testSetTtl(): void
4546
{
46-
$client = $this->prophesize(CacheInterface::class);
47+
$client = Mockery::mock(CacheInterface::class);
4748
$key = 'key';
4849
$value = 'value';
4950
$ttl = 123;
50-
$client->set($key, $value, $ttl)->shouldBeCalled()->willReturn(resolve(true));
51-
self::assertTrue((new PSR16Adapter($client->reveal()))->set($key, $value, $ttl));
51+
$client->shouldReceive('set')->with($key, $value, $ttl)->andReturn(resolve(true));
52+
self::assertTrue((new PSR16Adapter($client))->set($key, $value, $ttl));
5253
}
5354

5455
public function testSetDateIntervalTtl(): void
5556
{
56-
$client = $this->prophesize(CacheInterface::class);
57+
$client = Mockery::mock(CacheInterface::class);
5758
$key = 'key';
5859
$value = 'value';
5960
$dateIntervalTtl = new DateInterval('PT123S');
6061
$ttl = 123;
61-
$client->set($key, $value, $ttl)->shouldBeCalled()->willReturn(resolve(true));
62-
self::assertTrue((new PSR16Adapter($client->reveal()))->set($key, $value, $dateIntervalTtl));
62+
$client->shouldReceive('set')->with($key, $value, $ttl)->andReturn(resolve(true));
63+
self::assertTrue((new PSR16Adapter($client))->set($key, $value, $dateIntervalTtl));
6364
}
6465

6566
public function testSetTtlException(): void
@@ -68,12 +69,12 @@ public function testSetTtlException(): void
6869
self::expectException($exception::class);
6970
self::expectExceptionMessage($exception->getMessage());
7071

71-
$client = $this->prophesize(CacheInterface::class);
72+
$client = Mockery::mock(CacheInterface::class);
7273
$key = 'key';
7374
$value = 'value';
7475
$ttl = 123;
75-
$client->set($key, $value, $ttl)->shouldBeCalled()->willReturn(reject($exception));
76-
self::assertFalse((new PSR16Adapter($client->reveal()))->set($key, $value, $ttl));
76+
$client->shouldReceive('set')->with($key, $value, $ttl)->andReturn(reject($exception));
77+
self::assertFalse((new PSR16Adapter($client))->set($key, $value, $ttl));
7778
}
7879

7980
public function testSetException(): void
@@ -82,19 +83,19 @@ public function testSetException(): void
8283
self::expectException($exception::class);
8384
self::expectExceptionMessage($exception->getMessage());
8485

85-
$client = $this->prophesize(CacheInterface::class);
86+
$client = Mockery::mock(CacheInterface::class);
8687
$key = 'key';
8788
$value = 'value';
88-
$client->set($key, $value, null)->shouldBeCalled()->willReturn(reject($exception));
89-
self::assertFalse((new PSR16Adapter($client->reveal()))->set($key, $value));
89+
$client->shouldReceive('set')->with($key, $value, null)->andReturn(reject($exception));
90+
self::assertFalse((new PSR16Adapter($client))->set($key, $value));
9091
}
9192

9293
public function testDelete(): void
9394
{
94-
$client = $this->prophesize(CacheInterface::class);
95+
$client = Mockery::mock(CacheInterface::class);
9596
$key = 'key';
96-
$client->delete($key)->shouldBeCalled()->willReturn(resolve(true));
97-
(new PSR16Adapter($client->reveal()))->delete($key);
97+
$client->shouldReceive('delete')->with($key)->andReturn(resolve(true));
98+
self::assertTrue((new PSR16Adapter($client))->delete($key));
9899
}
99100

100101
public function testDeleteException(): void
@@ -103,26 +104,26 @@ public function testDeleteException(): void
103104
self::expectException($exception::class);
104105
self::expectExceptionMessage($exception->getMessage());
105106

106-
$client = $this->prophesize(CacheInterface::class);
107+
$client = Mockery::mock(CacheInterface::class);
107108
$key = 'key';
108-
$client->delete($key)->shouldBeCalled()->willReturn(reject($exception));
109-
(new PSR16Adapter($client->reveal()))->delete($key);
109+
$client->shouldReceive('delete')->with($key)->andReturn(reject($exception));
110+
(new PSR16Adapter($client))->delete($key);
110111
}
111112

112113
public function testHas(): void
113114
{
114-
$client = $this->prophesize(CacheInterface::class);
115+
$client = Mockery::mock(CacheInterface::class);
115116
$key = 'key';
116-
$client->has($key)->shouldBeCalled()->willReturn(resolve(true));
117-
(new PSR16Adapter($client->reveal()))->has($key);
117+
$client->shouldReceive('has')->with($key)->andReturn(resolve(true));
118+
self::assertTrue((new PSR16Adapter($client))->has($key));
118119
}
119120

120121
public function testDeleteMultiple(): void
121122
{
122-
$client = $this->prophesize(CacheInterface::class);
123+
$client = Mockery::mock(CacheInterface::class);
123124
$key = 'key';
124-
$client->deleteMultiple([$key])->shouldBeCalled()->willReturn(resolve(true));
125-
(new PSR16Adapter($client->reveal()))->deleteMultiple([$key]);
125+
$client->shouldReceive('deleteMultiple')->with([$key])->andReturn(resolve(true));
126+
self::assertTrue((new PSR16Adapter($client))->deleteMultiple([$key]));
126127
}
127128

128129
public function testDeleteMultipleException(): void
@@ -131,35 +132,35 @@ public function testDeleteMultipleException(): void
131132
self::expectException($exception::class);
132133
self::expectExceptionMessage($exception->getMessage());
133134

134-
$client = $this->prophesize(CacheInterface::class);
135+
$client = Mockery::mock(CacheInterface::class);
135136
$key = 'key';
136-
$client->deleteMultiple([$key])->shouldBeCalled()->willReturn(reject($exception));
137-
(new PSR16Adapter($client->reveal()))->deleteMultiple([$key]);
137+
$client->shouldReceive('deleteMultiple')->with([$key])->andReturn(reject($exception));
138+
(new PSR16Adapter($client))->deleteMultiple([$key]);
138139
}
139140

140141
public function testCLear(): void
141142
{
142-
$client = $this->prophesize(CacheInterface::class);
143-
$client->clear()->shouldBeCalled()->willReturn(resolve(true));
144-
(new PSR16Adapter($client->reveal()))->clear();
143+
$client = Mockery::mock(CacheInterface::class);
144+
$client->shouldReceive('clear')->andReturn(resolve(true));
145+
self::assertTrue((new PSR16Adapter($client))->clear());
145146
}
146147

147148
public function testSetMultiple(): void
148149
{
149-
$client = $this->prophesize(CacheInterface::class);
150+
$client = Mockery::mock(CacheInterface::class);
150151
$key = 'key';
151152
$value = 'value';
152153
$ttl = 123;
153-
$client->setMultiple([$key => $value], $ttl)->shouldBeCalled()->willReturn(resolve(true));
154-
self::assertTrue((new PSR16Adapter($client->reveal()))->setMultiple([$key => $value], $ttl));
154+
$client->shouldReceive('setMultiple')->with([$key => $value], $ttl)->andReturn(resolve(true));
155+
self::assertTrue((new PSR16Adapter($client))->setMultiple([$key => $value], $ttl));
155156
}
156157

157158
public function testGetMultiple(): void
158159
{
159-
$client = $this->prophesize(CacheInterface::class);
160+
$client = Mockery::mock(CacheInterface::class);
160161
$key = 'key';
161162
$value = 'value';
162-
$client->getMultiple([$key], null)->shouldBeCalled()->willReturn(resolve([$key => $value]));
163-
self::assertSame([$key => $value], (new PSR16Adapter($client->reveal()))->getMultiple([$key]));
163+
$client->shouldReceive('getMultiple')->with([$key], null)->andReturn(resolve([$key => $value]));
164+
self::assertSame([$key => $value], (new PSR16Adapter($client))->getMultiple([$key]));
164165
}
165166
}

0 commit comments

Comments
 (0)