1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Governator\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Predis\ClientInterface; |
7
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
8
|
|
|
use Psr\SimpleCache\CacheInterface; |
9
|
|
|
use Symfony\Component\Cache\Traits\RedisClusterProxy; |
10
|
|
|
use Symfony\Component\Cache\Traits\RedisProxy; |
11
|
|
|
use Zenstruck\Governator\Store\MemoryStore; |
12
|
|
|
use Zenstruck\Governator\Store\Psr16CacheStore; |
13
|
|
|
use Zenstruck\Governator\Store\Psr6CacheStore; |
14
|
|
|
use Zenstruck\Governator\Store\RedisStore; |
15
|
|
|
use Zenstruck\Governator\Store\UnlimitedStore; |
16
|
|
|
use Zenstruck\Governator\StoreFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Kevin Bond <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class StoreFactoryTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
* @dataProvider connectionProvider |
26
|
|
|
*/ |
27
|
|
|
public function can_create_for_connection($connection, $expectedClass): void |
28
|
|
|
{ |
29
|
|
|
$this->assertInstanceOf($expectedClass, StoreFactory::create($connection)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function connectionProvider(): iterable |
33
|
|
|
{ |
34
|
|
|
yield ['memory', MemoryStore::class]; |
35
|
|
|
yield ['unlimited', UnlimitedStore::class]; |
36
|
|
|
yield [new MemoryStore(), MemoryStore::class]; |
37
|
|
|
yield [$this->createMock(CacheItemPoolInterface::class), Psr6CacheStore::class]; |
38
|
|
|
yield [$this->createMock(CacheInterface::class), Psr16CacheStore::class]; |
39
|
|
|
yield [$this->createMock(\Redis::class), RedisStore::class]; |
40
|
|
|
yield [$this->createMock(\RedisArray::class), RedisStore::class]; |
41
|
|
|
yield [$this->createMock(\RedisCluster::class), RedisStore::class]; |
42
|
|
|
yield [$this->createMock(ClientInterface::class), RedisStore::class]; |
43
|
|
|
yield [$this->createMock(RedisProxy::class), RedisStore::class]; |
44
|
|
|
yield ['redis://localhost', RedisStore::class]; |
45
|
|
|
|
46
|
|
|
if (\class_exists(RedisClusterProxy::class)) { |
47
|
|
|
yield [$this->createMock(RedisClusterProxy::class), RedisStore::class]; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function connection_must_be_string_or_object(): void |
55
|
|
|
{ |
56
|
|
|
$this->expectException(\TypeError::class); |
57
|
|
|
|
58
|
|
|
StoreFactory::create(['array']); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
*/ |
64
|
|
|
public function unsupported_string_connection(): void |
65
|
|
|
{ |
66
|
|
|
$this->expectException(\InvalidArgumentException::class); |
67
|
|
|
|
68
|
|
|
StoreFactory::create('invalid'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function unsupported_object_connection(): void |
75
|
|
|
{ |
76
|
|
|
$this->expectException(\InvalidArgumentException::class); |
77
|
|
|
|
78
|
|
|
StoreFactory::create(new \stdClass()); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|