Completed
Pull Request — master (#327)
by David de
16:26 queued 13:21
created

testPreHandleReturnEarly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

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