Completed
Pull Request — master (#365)
by Alessandro
03:29
created

getHttpCachePartialMock()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 13
cts 14
cp 0.9286
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 1
crap 2.0014
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache 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\HttpCache\Test;
13
14
use FOS\HttpCache\SymfonyCache\CacheEvent;
15
use FOS\HttpCache\SymfonyCache\CacheInvalidation;
16
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
17
use FOS\HttpCache\SymfonyCache\Events;
18
use PHPUnit\Framework\TestCase;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
23
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
24
use Symfony\Component\HttpKernel\HttpKernelInterface;
25
26
/**
27
 * This test ensures that the EventDispatchingHttpCache trait is correctly used.
28
 */
29
abstract class EventDispatchingHttpCacheTestCase extends TestCase
30
{
31
    /**
32
     * Specify the CacheInvalidationInterface HttpCache class to test.
33
     *
34
     * @return string Fully qualified class name of the AppCache
35
     */
36
    abstract protected function getCacheClass();
37
38
    /**
39
     * Create a partial mock of the HttpCache to only test some methods.
40
     *
41
     * @param array $mockedMethods List of methods to mock
42
     *
43
     * @return CacheInvalidation|EventDispatchingHttpCache|\PHPUnit_Framework_MockObject_MockObject
44
     */
45 9
    protected function getHttpCachePartialMock(array $mockedMethods = null)
46
    {
47
        $mock = $this
48 9
            ->getMockBuilder($this->getCacheClass())
49 9
            ->setMethods($mockedMethods)
50 9
            ->disableOriginalConstructor()
51 9
            ->getMock()
52
        ;
53
54 9
        $this->assertInstanceOf(CacheInvalidation::class, $mock);
55
56
        // Force setting options property since we can't use original constructor.
57
        $options = [
58 9
            'debug' => false,
59
            'default_ttl' => 0,
60
            'private_headers' => ['Authorization', 'Cookie'],
61
            'allow_reload' => false,
62
            'allow_revalidate' => false,
63
            'stale_while_revalidate' => 2,
64
            'stale_if_error' => 60,
65
        ];
66
67 9
        $refHttpCache = new \ReflectionClass(HttpCache::class);
68
        // Workaround for Symfony 2 where $options property is not defined.
69 9
        if (!$refHttpCache->hasProperty('options')) {
70
            $mock->options = $options;
71
        } else {
72 9
            $refOptions = $refHttpCache->getProperty('options');
73 9
            $refOptions->setAccessible(true);
74 9
            $refOptions->setValue($mock, $options);
75
        }
76
77 9
        return $mock;
78
    }
79
80
    /**
81
     * Set the store property on a HttpCache to a StoreInterface expecting one write with request and response.
82
     *
83
     * @param CacheInvalidation $httpCache
84
     * @param Request           $request
85
     * @param Response          $response
86
     */
87 2
    protected function setStoreMock(CacheInvalidation $httpCache, Request $request, Response $response)
88
    {
89 2
        $store = $this->createMock(StoreInterface::class);
90
        $store
91 2
            ->expects($this->once())
92 2
            ->method('write')
93 2
            ->with($request, $response)
94
        ;
95 2
        $refHttpCache = new \ReflectionClass(HttpCache::class);
96 2
        $refStore = $refHttpCache->getProperty('store');
97 2
        $refStore->setAccessible(true);
98 2
        $refStore->setValue($httpCache, $store);
99 2
    }
100
101
    /**
102
     * Assert that preHandle and postHandle are called.
103
     */
104 1
    public function testHandleCalled()
105
    {
106 1
        $catch = true;
107 1
        $request = Request::create('/foo', 'GET');
108 1
        $response = new Response();
109
110 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
111 1
        $testListener = new TestListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 110 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
112 1
        $httpCache->addSubscriber($testListener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
113
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCache\SymfonyCac...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...
114 1
            ->expects($this->any())
115 1
            ->method('lookup')
116 1
            ->with($request)
117 1
            ->will($this->returnValue($response))
118
        ;
119
120 1
        $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
0 ignored issues
show
Bug introduced by
The method handle does only exist in FOS\HttpCache\SymfonyCac...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...
121 1
        $this->assertEquals(1, $testListener->preHandleCalls);
122 1
        $this->assertEquals(1, $testListener->postHandleCalls);
123 1
    }
124
125
    /**
126
     * Assert that when preHandle returns a response, that response is used and the normal kernel flow stopped.
127
     *
128
     * @depends testHandleCalled
129
     */
130 1
    public function testPreHandleReturnEarly()
131
    {
132 1
        $catch = true;
133 1
        $request = Request::create('/foo', 'GET');
134 1
        $response = new Response();
135
136 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
137 1
        $testListener = new TestListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 136 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
138 1
        $testListener->preHandleResponse = $response;
139 1
        $httpCache->addSubscriber($testListener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
140
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCache\SymfonyCac...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...
141 1
            ->expects($this->never())
142 1
            ->method('lookup')
143
        ;
144
145 1
        $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
0 ignored issues
show
Bug introduced by
The method handle does only exist in FOS\HttpCache\SymfonyCac...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...
146 1
        $this->assertEquals(1, $testListener->preHandleCalls);
147 1
        $this->assertEquals(1, $testListener->postHandleCalls);
148 1
    }
149
150
    /**
151
     * Assert that postHandle can update the response.
152
     *
153
     * @depends testHandleCalled
154
     */
155 1
    public function testPostHandleReturn()
156
    {
157 1
        $catch = true;
158 1
        $request = Request::create('/foo', 'GET');
159 1
        $regularResponse = new Response();
160 1
        $postResponse = new Response();
161
162 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
163 1
        $testListener = new TestListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 162 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
164 1
        $testListener->postHandleResponse = $postResponse;
165 1
        $httpCache->addSubscriber($testListener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
166
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCache\SymfonyCac...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...
167 1
            ->expects($this->any())
168 1
            ->method('lookup')
169 1
            ->with($request)
170 1
            ->will($this->returnValue($regularResponse))
171
        ;
172
173 1
        $this->assertSame($postResponse, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
0 ignored issues
show
Bug introduced by
The method handle does only exist in FOS\HttpCache\SymfonyCac...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...
174 1
        $this->assertEquals(1, $testListener->preHandleCalls);
175 1
        $this->assertEquals(1, $testListener->postHandleCalls);
176 1
    }
177
178
    /**
179
     * Assert that postHandle is called and the response can be updated even when preHandle returned a response.
180
     *
181
     * @depends testHandleCalled
182
     */
183 1
    public function testPostHandleAfterPreHandle()
184
    {
185 1
        $catch = true;
186 1
        $request = Request::create('/foo', 'GET');
187 1
        $preResponse = new Response();
188 1
        $postResponse = new Response();
189
190 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
191 1
        $testListener = new TestListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 190 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
192 1
        $testListener->preHandleResponse = $preResponse;
193 1
        $testListener->postHandleResponse = $postResponse;
194 1
        $httpCache->addSubscriber($testListener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
195
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCache\SymfonyCac...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...
196 1
            ->expects($this->never())
197 1
            ->method('lookup')
198
        ;
199
200 1
        $this->assertSame($postResponse, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
0 ignored issues
show
Bug introduced by
The method handle does only exist in FOS\HttpCache\SymfonyCac...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...
201 1
        $this->assertEquals(1, $testListener->preHandleCalls);
202 1
        $this->assertEquals(1, $testListener->postHandleCalls);
203 1
    }
204
205
    /**
206
     * Assert that preStore is called.
207
     */
208 1
    public function testPreStoreCalled()
209
    {
210 1
        $request = Request::create('/foo', 'GET');
211 1
        $response = new Response();
212
213 1
        $httpCache = $this->getHttpCachePartialMock();
214 1
        $testListener = new TestListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock() on line 213 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
215 1
        $httpCache->addSubscriber($testListener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
216
217 1
        $this->setStoreMock($httpCache, $request, $response);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock() on line 213 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\Event...estCase::setStoreMock() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
218
219 1
        $refHttpCache = new \ReflectionObject($httpCache);
220 1
        $method = $refHttpCache->getMethod('store');
221 1
        $method->setAccessible(true);
222 1
        $method->invokeArgs($httpCache, [$request, $response]);
223 1
        $this->assertEquals(1, $testListener->preStoreCalls);
224 1
    }
225
226
    /**
227
     * Assert that preStore response is used when provided.
228
     */
229 1
    public function testPreStoreResponse()
230
    {
231 1
        $request = Request::create('/foo', 'GET');
232 1
        $regularResponse = new Response();
233 1
        $preStoreResponse = new Response();
234
235 1
        $httpCache = $this->getHttpCachePartialMock();
236 1
        $testListener = new TestListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock() on line 235 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
237 1
        $testListener->preStoreResponse = $preStoreResponse;
238 1
        $httpCache->addSubscriber($testListener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
239
240 1
        $this->setStoreMock($httpCache, $request, $preStoreResponse);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock() on line 235 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\Event...estCase::setStoreMock() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
241
242 1
        $refHttpCache = new \ReflectionObject($httpCache);
243 1
        $method = $refHttpCache->getMethod('store');
244 1
        $method->setAccessible(true);
245 1
        $method->invokeArgs($httpCache, [$request, $regularResponse]);
246 1
        $this->assertEquals(1, $testListener->preStoreCalls);
247 1
    }
248
249
    /**
250
     * Assert that preInvalidate is called.
251
     */
252 1
    public function testPreInvalidateCalled()
253
    {
254 1
        $catch = true;
255 1
        $request = Request::create('/foo', 'GET');
256 1
        $response = new Response('', 500);
257
258 1
        $httpCache = $this->getHttpCachePartialMock(['pass']);
259 1
        $testListener = new TestListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('pass')) on line 258 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
260 1
        $httpCache->addSubscriber($testListener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
261
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCache\SymfonyCac...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...
262 1
            ->expects($this->any())
263 1
            ->method('pass')
264 1
            ->with($request)
265 1
            ->will($this->returnValue($response))
266
        ;
267 1
        $refHttpCache = new \ReflectionObject($httpCache);
268 1
        $method = $refHttpCache->getMethod('invalidate');
269 1
        $method->setAccessible(true);
270
271 1
        $this->assertSame($response, $method->invokeArgs($httpCache, [$request, $catch]));
272 1
        $this->assertEquals(1, $testListener->preInvalidateCalls);
273 1
    }
274
275
    /**
276
     * Assert that when preInvalidate returns a response, that response is used and the normal kernel flow stopped.
277
     *
278
     * @depends testPreInvalidateCalled
279
     */
280 1
    public function testPreInvalidateReturnEarly()
281
    {
282 1
        $catch = true;
283 1
        $request = Request::create('/foo', 'GET');
284 1
        $response = new Response('', 400);
285
286 1
        $httpCache = $this->getHttpCachePartialMock(['pass']);
287 1
        $testListener = new TestListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('pass')) on line 286 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
288 1
        $testListener->preInvalidateResponse = $response;
289 1
        $httpCache->addSubscriber($testListener);
0 ignored issues
show
Bug introduced by
The method addSubscriber does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
290
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCache\SymfonyCac...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...
291 1
            ->expects($this->never())
292 1
            ->method('pass')
293
        ;
294 1
        $refHttpCache = new \ReflectionObject($httpCache);
295 1
        $method = $refHttpCache->getMethod('invalidate');
296 1
        $method->setAccessible(true);
297
298 1
        $this->assertSame($response, $method->invokeArgs($httpCache, [$request, $catch]));
299 1
        $this->assertEquals(1, $testListener->preInvalidateCalls);
300 1
    }
301
302 1
    public function testAddListener()
303
    {
304 1
        $request = Request::create('/foo', 'GET');
305 1
        $response = new Response();
306
307 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
308 1
        $simpleListener = new SimpleListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 307 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\SimpleListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
309 1
        $httpCache->addListener(Events::PRE_HANDLE, [$simpleListener, 'callback']);
0 ignored issues
show
Bug introduced by
The method addListener does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_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...
310
311
        $httpCache
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in FOS\HttpCache\SymfonyCac...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...
312 1
            ->expects($this->any())
313 1
            ->method('lookup')
314 1
            ->with($request)
315 1
            ->will($this->returnValue($response))
316
        ;
317
318 1
        $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST));
0 ignored issues
show
Bug introduced by
The method handle does only exist in FOS\HttpCache\SymfonyCac...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...
319 1
        $this->assertEquals(1, $simpleListener->calls);
320 1
    }
321
}
322
323
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...
324
{
325
    /**
326
     * @var int Count how many times preHandle has been called
327
     */
328
    public $preHandleCalls = 0;
329
330
    /**
331
     * @var int Count how many times postHandle has been called
332
     */
333
    public $postHandleCalls = 0;
334
335
    /**
336
     * @var int Count how many times preStore has been called
337
     */
338
    public $preStoreCalls = 0;
339
340
    /**
341
     * @var int Count how many times preInvalidate has been called
342
     */
343
    public $preInvalidateCalls = 0;
344
345
    /**
346
     * @var Response A response to set during the preHandle
347
     */
348
    public $preHandleResponse;
349
350
    /**
351
     * @var Response A response to set during the postHandle
352
     */
353
    public $postHandleResponse;
354
355
    /**
356
     * @var Response A response to set during the preStore
357
     */
358
    public $preStoreResponse;
359
360
    /**
361
     * @var Response A response to set during the preInvalidate
362
     */
363
    public $preInvalidateResponse;
364
365
    /**
366
     * @var EventDispatchingHttpCacheTestCase To do assertions
367
     */
368
    private $test;
369
370
    /**
371
     * @var CacheInvalidation The kernel to ensure the event carries the correct kernel
372
     */
373
    private $kernel;
374
375
    /**
376
     * @var Request The request to ensure the event carries the correct request
377
     */
378
    private $request;
379
380 8
    public function __construct(
381
        EventDispatchingHttpCacheTestCase $test,
382
        CacheInvalidation $kernel,
383
        Request $request
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
384
    ) {
385 8
        $this->test = $test;
386 8
        $this->kernel = $kernel;
387 8
        $this->request = $request;
388 8
    }
389
390 8
    public static function getSubscribedEvents()
391
    {
392
        return [
393 8
            Events::PRE_HANDLE => 'preHandle',
394 8
            Events::POST_HANDLE => 'postHandle',
395 8
            Events::PRE_STORE => 'preStore',
396 8
            Events::PRE_INVALIDATE => 'preInvalidate',
397
        ];
398
    }
399
400 4 View Code Duplication
    public function preHandle(CacheEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
401
    {
402 4
        $this->test->assertSame($this->kernel, $event->getKernel());
403 4
        $this->test->assertSame($this->request, $event->getRequest());
404 4
        if ($this->preHandleResponse) {
405 2
            $event->setResponse($this->preHandleResponse);
406
        }
407 4
        ++$this->preHandleCalls;
408 4
    }
409
410 4 View Code Duplication
    public function postHandle(CacheEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
411
    {
412 4
        $this->test->assertSame($this->kernel, $event->getKernel());
413 4
        $this->test->assertSame($this->request, $event->getRequest());
414 4
        if ($this->postHandleResponse) {
415 2
            $event->setResponse($this->postHandleResponse);
416
        }
417 4
        ++$this->postHandleCalls;
418 4
    }
419
420 2 View Code Duplication
    public function preStore(CacheEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
421
    {
422 2
        $this->test->assertSame($this->kernel, $event->getKernel());
423 2
        $this->test->assertSame($this->request, $event->getRequest());
424 2
        if ($this->preStoreResponse) {
425 1
            $event->setResponse($this->preStoreResponse);
426
        }
427 2
        ++$this->preStoreCalls;
428 2
    }
429
430 2 View Code Duplication
    public function preInvalidate(CacheEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
431
    {
432 2
        $this->test->assertSame($this->kernel, $event->getKernel());
433 2
        $this->test->assertSame($this->request, $event->getRequest());
434 2
        if ($this->preInvalidateResponse) {
435 1
            $event->setResponse($this->preInvalidateResponse);
436
        }
437 2
        ++$this->preInvalidateCalls;
438 2
    }
439
}
440
441
class SimpleListener
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...
442
{
443
    public $calls = 0;
444
445
    /**
446
     * @var EventDispatchingHttpCacheTestCase To do assertions
447
     */
448
    private $test;
449
450
    /**
451
     * @var CacheInvalidation The kernel to ensure the event carries the correct kernel
452
     */
453
    private $kernel;
454
455
    /**
456
     * @var Request The request to ensure the event carries the correct request
457
     */
458
    private $request;
459
460 1
    public function __construct(
461
        EventDispatchingHttpCacheTestCase $test,
462
        CacheInvalidation $kernel,
463
        Request $request
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
464
    ) {
465 1
        $this->test = $test;
466 1
        $this->kernel = $kernel;
467 1
        $this->request = $request;
468 1
    }
469
470 1
    public function callback(CacheEvent $event)
471
    {
472 1
        $this->test->assertSame($this->kernel, $event->getKernel());
473 1
        $this->test->assertSame($this->request, $event->getRequest());
474 1
        ++$this->calls;
475 1
    }
476
}
477