Completed
Push — master ( 9bb55b...536c73 )
by David de
11s
created

InvalidationListenerTest::testOnConsoleTerminate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCacheBundle\Tests\Unit\EventListener;
13
14
use FOS\HttpCacheBundle\Configuration\InvalidatePath;
15
use FOS\HttpCacheBundle\Configuration\InvalidateRoute;
16
use FOS\HttpCacheBundle\EventListener\InvalidationListener;
17
use FOS\HttpCacheBundle\Http\RuleMatcher;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\RequestMatcher;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
23
use Symfony\Component\Routing\Route;
24
use Symfony\Component\Routing\RouteCollection;
25
26
class InvalidationListenerTest extends \PHPUnit_Framework_TestCase
27
{
28
    protected $cacheManager;
29
    protected $invalidators;
30
31
    public function setUp()
32
    {
33
        $this->cacheManager = \Mockery::mock('\FOS\HttpCacheBundle\CacheManager');
34
    }
35
36
    public function testNoRoutesInvalidatedWhenResponseIsUnsuccessful()
37
    {
38
        $this->cacheManager
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
39
            ->shouldReceive('invalidateRoute')->never()
40
            ->shouldReceive('flush')->once();
41
42
        $this->invalidators = \Mockery::mock('\FOS\HttpCacheBundle\Invalidator\InvalidatorCollection')
43
            ->shouldReceive('hasInvalidatorRoute')
44
            ->with('my_route')
45
            ->andReturn(false)
46
            ->getMock();
47
48
        $request = new Request();
49
        $request->attributes->set('_route', 'my_route');
50
51
        $event = $this->getEvent($request, new Response('', 500));
52
        $this->getListener()->onKernelTerminate($event);
53
    }
54
55
    public function testOnKernelTerminate()
56
    {
57
        $cacheManager = \Mockery::mock('\FOS\HttpCacheBundle\CacheManager');
58
        $cacheManager->shouldReceive('invalidatePath')->with('/retrieve/something/123')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
59
            ->shouldReceive('invalidatePath')->with('/retrieve/something/123/bla')
60
            ->shouldReceive('flush')->once()
61
            ->getMock();
62
63
        $routes = new RouteCollection();
64
        $routes->add('route_invalidator', new Route('/edit/something/{id}/{special}'));
65
        $routes->add('route_invalidated', new Route('/retrieve/something/{id}'));
66
        $routes->add('route_invalidated_special', new Route('/retrieve/something/{id}/{special}'));
67
68
        $requestParams = array('id' => 123, 'special' => 'bla');
69
        $router = \Mockery::mock('\Symfony\Component\Routing\Router')
70
            ->shouldDeferMissing()
71
            ->shouldReceive('generate')
72
            ->with('route_invalidated', $requestParams)
73
            ->andReturn('/retrieve/something/123?special=bla')
74
75
            ->shouldReceive('generate')
76
            ->with('route_invalidated_special', $requestParams)
77
            ->andReturn('/retrieve/something/123/bla')
78
            ->getMock();
79
80
        $ruleMatcher = new RuleMatcher(new RequestMatcher(
81
            null,
82
            null,
83
            null,
84
            null,
85
            array('_route' => 'route_invalidator')
86
        ), array());
87
88
        $listener = new InvalidationListener($cacheManager, $router);
89
        $listener->addRule($ruleMatcher, array(
90
            'route_invalidated' => array('ignore_extra_params' => true),
91
            'route_invalidated_special' => array('ignore_extra_params' => true),
92
        ));
93
94
        $request = new Request();
95
        $request->attributes->set('_route', 'route_invalidator');
96
        $request->attributes->set('_route_params', $requestParams);
97
98
        $event = $this->getEvent($request);
99
        $listener->onKernelTerminate($event);
100
    }
101
102
    public function testOnKernelException()
103
    {
104
        $cacheManager = \Mockery::mock('\FOS\HttpCacheBundle\CacheManager');
105
        $cacheManager
106
            ->shouldReceive('flush')->once()
107
        ;
108
109
        $router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface');
110
111
        $listener = new InvalidationListener($cacheManager, $router);
112
113
        $request = new Request();
114
115
        $event = $this->getEvent($request);
116
        $listener->onKernelException($event);
0 ignored issues
show
Unused Code introduced by
The call to InvalidationListener::onKernelException() has too many arguments starting with $event.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
117
    }
118
119
    public function testInvalidatePath()
120
    {
121
        $request = new Request();
122
        $request->attributes->set('_invalidate_path', array(
123
            new InvalidatePath(array('value' => '/some/path')),
124
            new InvalidatePath(array('value' => array('/other/path', 'http://absolute.com/path'))),
125
        ));
126
127
        $event = $this->getEvent($request);
128
129
        $this->cacheManager
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
130
            ->shouldReceive('invalidatePath')->with('/some/path')->once()
131
            ->shouldReceive('invalidatePath')->with('/other/path')->once()
132
            ->shouldReceive('invalidatePath')->with('http://absolute.com/path')->once()
133
            ->shouldReceive('flush')->once();
134
135
        $this->getListener()->onKernelTerminate($event);
136
    }
137
138
    public function testInvalidateRoute()
139
    {
140
        $request = new Request();
141
        $request->attributes->set('request_id', 123);
142
        $request->attributes->set('_invalidate_route', array(
143
            new InvalidateRoute(array('name' => 'some_route')),
144
            new InvalidateRoute(array('name' => 'other_route', 'params' => array('id' => array('expression' => 'request_id')))),
145
        ));
146
147
        $event = $this->getEvent($request);
148
149
        $this->cacheManager
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
150
            ->shouldReceive('invalidateRoute')->with('some_route', array())->once()
151
            ->shouldReceive('invalidateRoute')->with('other_route', array('id' => 123))->once()
152
            ->shouldReceive('flush')->once();
153
154
        $this->getListener()->onKernelTerminate($event);
155
    }
156
157
    public function testOnConsoleTerminate()
158
    {
159
        $this->cacheManager->shouldReceive('flush')->once()->andReturn(2);
160
161
        $output = \Mockery::mock('\Symfony\Component\Console\Output\OutputInterface')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
162
            ->shouldReceive('getVerbosity')->once()->andReturn(OutputInterface::VERBOSITY_VERBOSE)
163
            ->shouldReceive('writeln')->with('Sent 2 invalidation request(s)')->once()
164
            ->getMock();
165
166
        $event = \Mockery::mock('\Symfony\Component\Console\Event\ConsoleEvent')
167
            ->shouldReceive('getOutput')->andReturn($output)
168
            ->getMock();
169
170
        $this->getListener()->onConsoleTerminate($event);
171
    }
172
173
    protected function getEvent(Request $request, Response $response = null)
174
    {
175
        return new PostResponseEvent(
176
            \Mockery::mock('\Symfony\Component\HttpKernel\HttpKernelInterface'),
177
            $request,
178
            null !== $response ? $response : new Response()
179
        );
180
    }
181
182
    protected function getListener()
183
    {
184
        return new InvalidationListener(
185
            $this->cacheManager,
186
            \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface')
187
        );
188
    }
189
}
190