Completed
Pull Request — master (#316)
by David
17:11 queued 14:25
created

testPreInvalidateReturnEarly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
crap 1
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\CacheInvalidationInterface;
15
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
16
use FOS\HttpCache\SymfonyCache\CacheEvent;
17
use FOS\HttpCache\SymfonyCache\Events;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
22
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
23
use Symfony\Component\HttpKernel\HttpKernelInterface;
24
25
/**
26
 * This test ensures that the EventDispatchingHttpCache trait is correctly used.
27
 */
28
abstract class EventDispatchingHttpCacheTestCase extends \PHPUnit_Framework_TestCase
29
{
30
    /**
31
     * Specify the CacheInvalidationInterface HttpCache class to test.
32
     *
33
     * @return string Fully qualified class name of the AppCache
34
     */
35
    abstract protected function getCacheClass();
36
37
    /**
38
     * Create a partial mock of the HttpCache to only test some methods.
39
     *
40
     * @param array $mockedMethods List of methods to mock
41
     *
42
     * @return CacheInvalidationInterface|EventDispatchingHttpCache|\PHPUnit_Framework_MockObject_MockObject
43
     */
44 8
    protected function getHttpCachePartialMock(array $mockedMethods = null)
45
    {
46 8
        $mock = $this
47 8
            ->getMockBuilder($this->getCacheClass())
48 8
            ->setMethods($mockedMethods)
49 8
            ->disableOriginalConstructor()
50 8
            ->getMock()
51 8
        ;
52
53 8
        $this->assertInstanceOf(CacheInvalidationInterface::class, $mock);
54
55
        // Force setting options property since we can't use original constructor.
56
        $options = [
57 8
            'debug' => false,
58 8
            'default_ttl' => 0,
59 8
            'private_headers' => ['Authorization', 'Cookie'],
60 8
            'allow_reload' => false,
61 8
            'allow_revalidate' => false,
62 8
            'stale_while_revalidate' => 2,
63 8
            'stale_if_error' => 60,
64 8
        ];
65
66 8
        $refHttpCache = new \ReflectionClass(HttpCache::class);
67 8
        $refOptions = $refHttpCache->getProperty('options');
68 8
        $refOptions->setAccessible(true);
69 8
        $refOptions->setValue($mock, $options);
70
71 8
        return $mock;
72
    }
73
74
    /**
75
     * Set the store property on a HttpCache to a StoreInterface expecting one write with request and response.
76
     *
77
     * @param CacheInvalidationInterface $httpCache
78
     * @param Request                    $request
79
     * @param Response                   $response
80
     */
81 2
    protected function setStoreMock(CacheInvalidationInterface $httpCache, Request $request, Response $response)
82
    {
83 2
        $store = $this->getMock(StoreInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
        $store
85 2
            ->expects($this->once())
86 2
            ->method('write')
87 2
            ->with($request, $response)
88
        ;
89 2
        $refHttpCache = new \ReflectionClass(HttpCache::class);
90 2
        $refStore = $refHttpCache->getProperty('store');
91 2
        $refStore->setAccessible(true);
92 2
        $refStore->setValue($httpCache, $store);
93 2
    }
94
95
    /**
96
     * Assert that preHandle and postHandle are called.
97
     */
98 1
    public function testHandleCalled()
99
    {
100 1
        $catch = true;
101 1
        $request = Request::create('/foo', 'GET');
102 1
        $response = new Response();
103
104 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
105 1
        $subscriber = new TestSubscriber($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 104 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestSubscriber::__construct() does only seem to accept object<FOS\HttpCache\Sym...eInvalidationInterface>, 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...
106 1
        $httpCache->addSubscriber($subscriber);
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...
107
        $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...
108 1
            ->expects($this->any())
109 1
            ->method('lookup')
110 1
            ->with($request)
111 1
            ->will($this->returnValue($response))
112
        ;
113
114 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...
115 1
        $this->assertEquals(1, $subscriber->preHandleCalls);
116 1
        $this->assertEquals(1, $subscriber->postHandleCalls);
117 1
    }
118
119
    /**
120
     * Assert that when preHandle returns a response, that response is used and the normal kernel flow stopped.
121
     *
122
     * @depends testHandleCalled
123
     */
124 1
    public function testPreHandleReturnEarly()
125
    {
126 1
        $catch = true;
127 1
        $request = Request::create('/foo', 'GET');
128 1
        $response = new Response();
129
130 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
131 1
        $subscriber = new TestSubscriber($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 130 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestSubscriber::__construct() does only seem to accept object<FOS\HttpCache\Sym...eInvalidationInterface>, 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...
132 1
        $subscriber->preHandleResponse = $response;
133 1
        $httpCache->addSubscriber($subscriber);
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...
134
        $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...
135 1
            ->expects($this->never())
136 1
            ->method('lookup')
137
        ;
138
139 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...
140 1
        $this->assertEquals(1, $subscriber->preHandleCalls);
141 1
        $this->assertEquals(1, $subscriber->postHandleCalls);
142 1
    }
143
144
    /**
145
     * Assert that postHandle can update the response.
146
     *
147
     * @depends testHandleCalled
148
     */
149 1
    public function testPostHandleReturn()
150
    {
151 1
        $catch = true;
152 1
        $request = Request::create('/foo', 'GET');
153 1
        $regularResponse = new Response();
154 1
        $postResponse = new Response();
155
156 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
157 1
        $subscriber = new TestSubscriber($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 156 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestSubscriber::__construct() does only seem to accept object<FOS\HttpCache\Sym...eInvalidationInterface>, 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...
158 1
        $subscriber->postHandleResponse = $postResponse;
159 1
        $httpCache->addSubscriber($subscriber);
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...
160
        $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...
161 1
            ->expects($this->any())
162 1
            ->method('lookup')
163 1
            ->with($request)
164 1
            ->will($this->returnValue($regularResponse))
165
        ;
166
167 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...
168 1
        $this->assertEquals(1, $subscriber->preHandleCalls);
169 1
        $this->assertEquals(1, $subscriber->postHandleCalls);
170 1
    }
171
172
    /**
173
     * Assert that postHandle is called and the response can be updated even when preHandle returned a response.
174
     *
175
     * @depends testHandleCalled
176
     */
177 1
    public function testPostHandleAfterPreHandle()
178
    {
179 1
        $catch = true;
180 1
        $request = Request::create('/foo', 'GET');
181 1
        $preResponse = new Response();
182 1
        $postResponse = new Response();
183
184 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
185 1
        $subscriber = new TestSubscriber($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 184 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestSubscriber::__construct() does only seem to accept object<FOS\HttpCache\Sym...eInvalidationInterface>, 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...
186 1
        $subscriber->preHandleResponse = $preResponse;
187 1
        $subscriber->postHandleResponse = $postResponse;
188 1
        $httpCache->addSubscriber($subscriber);
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...
189
        $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...
190 1
            ->expects($this->never())
191 1
            ->method('lookup')
192
        ;
193
194 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...
195 1
        $this->assertEquals(1, $subscriber->preHandleCalls);
196 1
        $this->assertEquals(1, $subscriber->postHandleCalls);
197 1
    }
198
199
    /**
200
     * Assert that preStore is called.
201
     */
202 1
    public function testPreStoreCalled()
203
    {
204 1
        $request = Request::create('/foo', 'GET');
205 1
        $response = new Response();
206
207 1
        $httpCache = $this->getHttpCachePartialMock();
208 1
        $subscriber = new TestSubscriber($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock() on line 207 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestSubscriber::__construct() does only seem to accept object<FOS\HttpCache\Sym...eInvalidationInterface>, 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...
209 1
        $httpCache->addSubscriber($subscriber);
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...
210
211 1
        $this->setStoreMock($httpCache, $request, $response);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock() on line 207 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...eInvalidationInterface>, 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...
212
213 1
        $refHttpCache = new \ReflectionObject($httpCache);
214 1
        $method = $refHttpCache->getMethod('store');
215 1
        $method->setAccessible(true);
216 1
        $method->invokeArgs($httpCache, [$request, $response]);
217 1
        $this->assertEquals(1, $subscriber->preStoreCalls);
218 1
    }
219
220
    /**
221
     * Assert that preStore response is used when provided.
222
     */
223 1
    public function testPreStoreResponse()
224 1
    {
225 1
        $request = Request::create('/foo', 'GET');
226 1
        $regularResponse = new Response();
227 1
        $preStoreResponse = new Response();
228
229 1
        $httpCache = $this->getHttpCachePartialMock();
230 1
        $subscriber = new TestSubscriber($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock() on line 229 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestSubscriber::__construct() does only seem to accept object<FOS\HttpCache\Sym...eInvalidationInterface>, 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...
231 1
        $subscriber->preStoreResponse = $preStoreResponse;
232 1
        $httpCache->addSubscriber($subscriber);
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...
233
234 1
        $this->setStoreMock($httpCache, $request, $preStoreResponse);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock() on line 229 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...eInvalidationInterface>, 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...
235
236 1
        $refHttpCache = new \ReflectionObject($httpCache);
237 1
        $method = $refHttpCache->getMethod('store');
238 1
        $method->setAccessible(true);
239 1
        $method->invokeArgs($httpCache, [$request, $regularResponse]);
240 1
        $this->assertEquals(1, $subscriber->preStoreCalls);
241 1
    }
242
243
    /**
244
     * Assert that preInvalidate is called.
245
     */
246 1
    public function testPreInvalidateCalled()
247
    {
248 1
        $catch = true;
249 1
        $request = Request::create('/foo', 'GET');
250 1
        $response = new Response('', 500);
251
252 1
        $httpCache = $this->getHttpCachePartialMock(['pass']);
253 1
        $subscriber = new TestSubscriber($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('pass')) on line 252 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestSubscriber::__construct() does only seem to accept object<FOS\HttpCache\Sym...eInvalidationInterface>, 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...
254 1
        $httpCache->addSubscriber($subscriber);
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...
255
        $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...
256 1
            ->expects($this->any())
257 1
            ->method('pass')
258 1
            ->with($request)
259 1
            ->will($this->returnValue($response))
260
        ;
261 1
        $refHttpCache = new \ReflectionObject($httpCache);
262 1
        $method = $refHttpCache->getMethod('invalidate');
263 1
        $method->setAccessible(true);
264
265 1
        $this->assertSame($response, $method->invokeArgs($httpCache, [$request, $catch]));
266 1
        $this->assertEquals(1, $subscriber->preInvalidateCalls);
267 1
    }
268
269
    /**
270
     * Assert that when preInvalidate returns a response, that response is used and the normal kernel flow stopped.
271
     *
272
     * @depends testPreInvalidateCalled
273
     */
274 2
    public function testPreInvalidateReturnEarly()
275
    {
276 1
        $catch = true;
277 1
        $request = Request::create('/foo', 'GET');
278 1
        $response = new Response('', 400);
279
280 2
        $httpCache = $this->getHttpCachePartialMock(['pass']);
281 1
        $subscriber = new TestSubscriber($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('pass')) on line 280 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\TestSubscriber::__construct() does only seem to accept object<FOS\HttpCache\Sym...eInvalidationInterface>, 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...
282 1
        $subscriber->preInvalidateResponse = $response;
283 1
        $httpCache->addSubscriber($subscriber);
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...
284
        $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...
285 1
            ->expects($this->never())
286 1
            ->method('pass')
287
        ;
288 1
        $refHttpCache = new \ReflectionObject($httpCache);
289 1
        $method = $refHttpCache->getMethod('invalidate');
290 1
        $method->setAccessible(true);
291
292 1
        $this->assertSame($response, $method->invokeArgs($httpCache, [$request, $catch]));
293 1
        $this->assertEquals(1, $subscriber->preInvalidateCalls);
294 1
    }
295
}
296
297
class TestSubscriber 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...
298
{
299
    /**
300
     * @var int Count how many times preHandle has been called
301
     */
302
    public $preHandleCalls = 0;
303
304
    /**
305
     * @var int Count how many times postHandle has been called
306
     */
307
    public $postHandleCalls = 0;
308
309
    /**
310
     * @var int Count how many times preStore has been called
311
     */
312
    public $preStoreCalls = 0;
313
314
    /**
315
     * @var int Count how many times preInvalidate has been called
316
     */
317
    public $preInvalidateCalls = 0;
318
319
    /**
320
     * @var Response A response to set during the preHandle
321
     */
322
    public $preHandleResponse;
323
324
    /**
325
     * @var Response A response to set during the postHandle
326
     */
327
    public $postHandleResponse;
328
329
    /**
330
     * @var Response A response to set during the preStore
331
     */
332
    public $preStoreResponse;
333
334
    /**
335
     * @var Response A response to set during the preInvalidate
336
     */
337
    public $preInvalidateResponse;
338
339
    /**
340
     * @var EventDispatchingHttpCacheTestCase To do assertions
341
     */
342
    private $test;
343
344
    /**
345
     * @var CacheInvalidationInterface The kernel to ensure the event carries the correct kernel
346
     */
347
    private $kernel;
348
349
    /**
350
     * @var Request The request to ensure the event carries the correct request
351
     */
352
    private $request;
353
354 8
    public function __construct(
355
        EventDispatchingHttpCacheTestCase $test,
356
        CacheInvalidationInterface $kernel,
357
        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...
358
    ) {
359 8
        $this->test = $test;
360 8
        $this->kernel = $kernel;
361 8
        $this->request = $request;
362 8
    }
363
364 8
    public static function getSubscribedEvents()
365
    {
366
        return [
367 8
            Events::PRE_HANDLE => 'preHandle',
368 8
            Events::POST_HANDLE => 'postHandle',
369 8
            Events::PRE_STORE => 'preStore',
370 8
            Events::PRE_INVALIDATE => 'preInvalidate',
371 8
        ];
372
    }
373
374 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...
375
    {
376 4
        $this->test->assertSame($this->kernel, $event->getKernel());
377 4
        $this->test->assertSame($this->request, $event->getRequest());
378 4
        if ($this->preHandleResponse) {
379 2
            $event->setResponse($this->preHandleResponse);
380 2
        }
381 4
        ++$this->preHandleCalls;
382 4
    }
383
384 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...
385
    {
386 4
        $this->test->assertSame($this->kernel, $event->getKernel());
387 4
        $this->test->assertSame($this->request, $event->getRequest());
388 4
        if ($this->postHandleResponse) {
389 2
            $event->setResponse($this->postHandleResponse);
390 2
        }
391 4
        ++$this->postHandleCalls;
392 4
    }
393
394 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...
395
    {
396 2
        $this->test->assertSame($this->kernel, $event->getKernel());
397 2
        $this->test->assertSame($this->request, $event->getRequest());
398 2
        if ($this->preStoreResponse) {
399 1
            $event->setResponse($this->preStoreResponse);
400 1
        }
401 2
        ++$this->preStoreCalls;
402 2
    }
403
404 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...
405
    {
406 2
        $this->test->assertSame($this->kernel, $event->getKernel());
407 2
        $this->test->assertSame($this->request, $event->getRequest());
408 2
        if ($this->preInvalidateResponse) {
409 1
            $event->setResponse($this->preInvalidateResponse);
410 1
        }
411 2
        ++$this->preInvalidateCalls;
412 2
    }
413
}
414