StoreFactoryTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
eloc 21
c 2
b 0
f 1
dl 0
loc 58
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A can_create_for_connection() 0 3 1
A unsupported_string_connection() 0 5 1
A connectionProvider() 0 16 2
A unsupported_object_connection() 0 5 1
A connection_must_be_string_or_object() 0 5 1
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']);
0 ignored issues
show
Bug introduced by
array('array') of type array<integer,string> is incompatible with the type object|string expected by parameter $connection of Zenstruck\Governator\StoreFactory::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        StoreFactory::create(/** @scrutinizer ignore-type */ ['array']);
Loading history...
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