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

TestListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSubscribedEvents() 0 4 1
A preHandle() 0 9 2
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\SymfonyCache;
13
14
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
15
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
16
use FOS\HttpCacheBundle\SymfonyCache\EventDispatchingHttpCache;
17
use FOS\HttpCache\SymfonyCache\CacheEvent;
18
use FOS\HttpCache\SymfonyCache\Events;
19
use FOS\HttpCache\SymfonyCache\UserContextSubscriber;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\HttpKernelInterface;
24
25
class EventDispatchingHttpCacheTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @return EventDispatchingHttpCache|\PHPUnit_Framework_MockObject_MockObject
29
     */
30
    protected function getHttpCachePartialMock(array $mockedMethods = null)
31
    {
32
        $mock = $this
33
            ->getMockBuilder('\FOS\HttpCacheBundle\SymfonyCache\EventDispatchingHttpCache')
34
            ->setMethods($mockedMethods)
35
            ->disableOriginalConstructor()
36
            ->getMock()
37
        ;
38
39
        // Force setting options property since we can't use original constructor.
40
        $options = array(
41
            'debug' => false,
42
            'default_ttl' => 0,
43
            'private_headers' => array('Authorization', 'Cookie'),
44
            'allow_reload' => false,
45
            'allow_revalidate' => false,
46
            'stale_while_revalidate' => 2,
47
            'stale_if_error' => 60,
48
        );
49
50
        $refHttpCache = new \ReflectionClass('Symfony\Component\HttpKernel\HttpCache\HttpCache');
51
        // Workaround for Symfony 2.3 where $options property is not defined.
52 View Code Duplication
        if (!$refHttpCache->hasProperty('options')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            $mock->options = $options;
54
        } else {
55
            $refOptions = $refHttpCache
56
                ->getProperty('options');
57
            $refOptions->setAccessible(true);
58
            $refOptions->setValue($mock, $options);
59
        }
60
61
        return $mock;
62
    }
63
64
    public function testCalled()
65
    {
66
        $catch = true;
67
        $request = Request::create('/foo', 'GET');
68
        $response = new Response();
69
70
        $httpCache = $this->getHttpCachePartialMock(array('lookup'));
71
        $listener = new TestListener($this, $httpCache, $request);
72
        $httpCache->addSubscriber($listener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCacheBundle\Symf...entDispatchingHttpCache, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCacheBundle\Symf...entDispatchingHttpCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
            ->expects($this->any())
75
            ->method('lookup')
76
            ->with($request)
77
            ->will($this->returnValue($response))
78
        ;
79
        $httpCache->handle($request);
0 ignored issues
show
Bug introduced by
The method handle does only exist in FOS\HttpCacheBundle\Symf...entDispatchingHttpCache, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
80
81
        $this->assertEquals(1, $listener->hits);
82
        $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
83
    }
84
85
    public function testAbort()
86
    {
87
        $catch = true;
88
        $request = Request::create('/foo', 'GET');
89
        $response = new Response();
90
91
        $httpCache = $this->getHttpCachePartialMock(array('lookup'));
92
        $listener = new TestListener($this, $httpCache, $request, $response);
93
        $httpCache->addSubscriber($listener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCacheBundle\Symf...entDispatchingHttpCache, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
94
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCacheBundle\Symf...entDispatchingHttpCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
95
            ->expects($this->never())
96
            ->method('lookup')
97
        ;
98
        $httpCache->handle($request);
0 ignored issues
show
Bug introduced by
The method handle does only exist in FOS\HttpCacheBundle\Symf...entDispatchingHttpCache, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
99
100
        $this->assertEquals(1, $listener->hits);
101
        $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
102
    }
103
104
    /**
105
     * @dataProvider configuredSubscribersProvider
106
     */
107
    public function testConfiguredSubscribers(array $options, array $expectedSubscribers)
108
    {
109
        $cacheKernel = $this->getHttpCachePartialMock(array('getOptions'));
110
        $cacheKernel
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCacheBundle\Symf...entDispatchingHttpCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
111
            ->expects($this->once())
112
            ->method('getOptions')
113
            ->will($this->returnValue($options))
114
        ;
115
        $refCache = new \ReflectionClass(get_class($cacheKernel));
116
        $refGetSubscribers = $refCache->getMethod('getDefaultSubscribers');
117
        $refGetSubscribers->setAccessible(true);
118
119
        $this->assertEquals($expectedSubscribers, $refGetSubscribers->invoke($cacheKernel));
120
    }
121
122
    public function configuredSubscribersProvider()
123
    {
124
        $all = array(new UserContextSubscriber(), new PurgeSubscriber(), new RefreshSubscriber());
125
126
        return array(
127
            array(array(), $all),
128
            array(
129
                array(
130
                    'fos_default_subscribers' => EventDispatchingHttpCache::SUBSCRIBER_ALL,
131
                ),
132
                $all,
133
            ),
134
            array(
135
                array(
136
                    'fos_default_subscribers' => EventDispatchingHttpCache::SUBSCRIBER_NONE,
137
                ),
138
                array(),
139
            ),
140
            array(
141
                array(
142
                    'fos_default_subscribers' => EventDispatchingHttpCache::SUBSCRIBER_USER_CONTEXT,
143
                ),
144
                array(new UserContextSubscriber()),
145
            ),
146
        );
147
    }
148
}
149
150
class TestListener implements EventSubscriberInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
151
{
152
    public $hits = 0;
153
    private $test;
154
    private $kernel;
155
    private $request;
156
    private $response;
157
158
    public function __construct($test, $kernel, $request, $response = null)
159
    {
160
        $this->test = $test;
161
        $this->kernel = $kernel;
162
        $this->request = $request;
163
        $this->response = $response;
164
    }
165
166
    public static function getSubscribedEvents()
167
    {
168
        return array(Events::PRE_HANDLE => 'preHandle');
169
    }
170
171
    public function preHandle(CacheEvent $event)
172
    {
173
        $this->test->assertSame($this->kernel, $event->getKernel());
174
        $this->test->assertSame($this->request, $event->getRequest());
175
        if ($this->response) {
176
            $event->setResponse($this->response);
177
        }
178
        ++$this->hits;
179
    }
180
}
181