BlackholeCacheItemPoolDecoratorTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCachePool() 0 11 2
A testIsHit() 0 9 1
A testGetItem() 0 7 1
A testGetItems() 0 9 2
A testGetDecoratedItem() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MovingImage\Bundle\VMProApiBundle\Tests\Decorator;
6
7
use MovingImage\Bundle\VMProApiBundle\Decorator\BlackholeCacheItemDecorator;
8
use MovingImage\Bundle\VMProApiBundle\Decorator\BlackholeCacheItemPoolDecorator;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Cache\CacheItemPoolInterface;
11
use Symfony\Component\Cache\Adapter\ArrayAdapter;
12
13
class BlackholeCacheItemPoolDecoratorTest extends TestCase
14
{
15
    /**
16
     * Asserts that blackhole pool will always report a cache miss.
17
     */
18
    public function testIsHit()
19
    {
20
        $cachePool = $this->getCachePool(['test' => 'test']);
21
22
        $blackholePool = new BlackholeCacheItemPoolDecorator($cachePool);
23
24
        $this->assertTrue($cachePool->getItem('test')->isHit());
25
        $this->assertFalse($blackholePool->getItem('test')->isHit());
26
    }
27
28
    /**
29
     * Asserts that item returned by the blackhole pool is the correct type.
30
     */
31
    public function testGetItem()
32
    {
33
        $cachePool = $this->getCachePool(['test' => 'test']);
34
        $blackholePool = new BlackholeCacheItemPoolDecorator($cachePool);
35
36
        $this->assertInstanceOf(BlackholeCacheItemDecorator::class, $blackholePool->getItem('test'));
37
    }
38
39
    /**
40
     * Asserts that items returned by the blackhole pool are the correct type.
41
     */
42
    public function testGetItems()
43
    {
44
        $cachePool = $this->getCachePool(['test1' => 'test1', 'test2' => 'test2']);
45
        $blackholePool = new BlackholeCacheItemPoolDecorator($cachePool);
46
47
        foreach ($blackholePool->getItems(['test1', 'test2']) as $item) {
48
            $this->assertInstanceOf(BlackholeCacheItemDecorator::class, $item);
49
        }
50
    }
51
52
    /**
53
     * Asserts that decorated item returned by the blackhole pool is of the correct type.
54
     */
55
    public function testGetDecoratedItem()
56
    {
57
        $cachePool = $this->getCachePool(['test' => 'test']);
58
        $cacheItem = $cachePool->getItem('test');
59
        $blackholePool = new BlackholeCacheItemPoolDecorator($cachePool);
60
        $blackholeItem = $blackholePool->getItem('test');
61
62
        $this->assertInstanceOf(get_class($cacheItem), $blackholeItem->getDecoratedItem());
63
    }
64
65
    /**
66
     * Returns an implementation of the CacheItemPoolInterface
67
     * initialized with the values provided in the array.
68
     *
69
     * @throws \Psr\Cache\InvalidArgumentException
70
     */
71
    private function getCachePool(array $values): CacheItemPoolInterface
72
    {
73
        $cachePool = new ArrayAdapter();
74
        foreach ($values as $key => $value) {
75
            $cacheItem = $cachePool->getItem($key);
76
            $cacheItem->set($value);
77
            $cachePool->save($cacheItem);
78
        }
79
80
        return $cachePool;
81
    }
82
}
83