Issues (62)

Tests/Cache/AuthorizationCacheWarmerTest.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Cache;
12
13
use PHPUnit\Framework\TestCase;
14
use Psr\Cache\CacheItemPoolInterface;
15
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16
use Symfony\Component\Routing\RouterInterface;
17
use Symfony\Component\Routing\RouteCollection;
18
use Yarhon\RouteGuardBundle\Controller\ControllerMetadata;
19
use Yarhon\RouteGuardBundle\Routing\RouteMetadata;
20
use Yarhon\RouteGuardBundle\Cache\DataCollector\RouteCollectionDataCollector;
21
use Yarhon\RouteGuardBundle\Cache\AuthorizationCacheWarmer;
22
23
/**
24
 * @author Yaroslav Honcharuk <[email protected]>
25
 */
26
class AuthorizationCacheWarmerTest extends TestCase
27
{
28
    private $dataCollector;
29
30
    private $testsCache;
31
32
    private $controllerMetadataCache;
33
34
    private $routeMetadataCache;
35
36
    private $cacheWarmer;
37
38
    public function setUp()
39
    {
40
        $this->dataCollector = $this->createMock(RouteCollectionDataCollector::class);
41
42
        $router = $this->createMock(RouterInterface::class);
43
44
        $router->method('getRouteCollection')
0 ignored issues
show
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

44
        $router->/** @scrutinizer ignore-call */ 
45
                 method('getRouteCollection')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            ->willReturn(new RouteCollection());
46
47
        $this->testsCache = new ArrayAdapter(0, false);
48
        $this->controllerMetadataCache = new ArrayAdapter(0, false);
49
        $this->routeMetadataCache = new ArrayAdapter(0, false);
50
51
        $this->cacheWarmer = new AuthorizationCacheWarmer($this->dataCollector, $router, $this->testsCache, $this->controllerMetadataCache, $this->routeMetadataCache);
52
    }
53
54
    public function testCachesAreFilled()
55
    {
56
        $routeOneData = $this->createRouteData();
57
        $routeTwoData = $this->createRouteData();
58
59
        $this->dataCollector->method('collect')
60
            ->willReturn([
61
                'route1' => $routeOneData,
62
                'route2' => $routeTwoData,
63
            ]);
64
65
        $this->cacheWarmer->warmUp('');
66
67
        $this->assertSame($routeOneData[0], $this->testsCache->getItem('route1')->get());
68
        $this->assertSame($routeOneData[1], $this->controllerMetadataCache->getItem('route1')->get());
69
        $this->assertSame($routeOneData[2], $this->routeMetadataCache->getItem('route1')->get());
70
71
        $this->assertSame($routeTwoData[0], $this->testsCache->getItem('route2')->get());
72
        $this->assertSame($routeTwoData[1], $this->controllerMetadataCache->getItem('route2')->get());
73
        $this->assertSame($routeTwoData[2], $this->routeMetadataCache->getItem('route2')->get());
74
    }
75
76
    public function testCachesAreCleared()
77
    {
78
        list($tests, $controllerMetadata, $routeMetadata) = $this->createRouteData();
79
80
        $this->addCacheItem($this->testsCache, 'index', $tests);
81
        $this->addCacheItem($this->controllerMetadataCache, 'index', $controllerMetadata);
82
        $this->addCacheItem($this->routeMetadataCache, 'index', $routeMetadata);
83
84
        $this->dataCollector->method('collect')
85
            ->willReturn([]);
86
87
        $this->cacheWarmer->warmUp('');
88
89
        $this->assertFalse($this->testsCache->hasItem('index'));
90
        $this->assertFalse($this->controllerMetadataCache->hasItem('index'));
91
        $this->assertFalse($this->routeMetadataCache->hasItem('index'));
92
    }
93
94
    public function testIsOptional()
95
    {
96
        $this->assertFalse($this->cacheWarmer->isOptional());
97
    }
98
99
    private function addCacheItem($cache, $name, $value)
100
    {
101
        $cacheItem = $cache->getItem($name);
102
        $cacheItem->set($value);
103
        $cache->save($cacheItem);
104
    }
105
106
    private function createRouteData()
107
    {
108
        return [[], $this->createMock(ControllerMetadata::class), $this->createMock(RouteMetadata::class)];
109
    }
110
}
111