Completed
Pull Request — master (#364)
by Alessandro
04:31
created

getHttpCachePartialMock()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 13
cts 14
cp 0.9286
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 1
crap 2.0014
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\Test;
13
14
use FOS\HttpCache\SymfonyCache\CacheEvent;
15
use FOS\HttpCache\SymfonyCache\CacheInvalidation;
16
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
17
use FOS\HttpCache\SymfonyCache\Events;
18
use 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 CacheInvalidation|EventDispatchingHttpCache|\PHPUnit_Framework_MockObject_MockObject
43
     */
44 9
    protected function getHttpCachePartialMock(array $mockedMethods = null)
45
    {
46
        $mock = $this
47 9
            ->getMockBuilder($this->getCacheClass())
48 9
            ->setMethods($mockedMethods)
49 9
            ->disableOriginalConstructor()
50 9
            ->getMock()
51
        ;
52
53 9
        $this->assertInstanceOf(CacheInvalidation::class, $mock);
54
55
        // Force setting options property since we can't use original constructor.
56
        $options = [
57 9
            '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 9
        $refHttpCache = new \ReflectionClass(HttpCache::class);
67
        // Workaround for Symfony 2 where $options property is not defined.
68 9
        if (!$refHttpCache->hasProperty('options')) {
69
            $mock->options = $options;
70
        } else {
71 9
            $refOptions = $refHttpCache->getProperty('options');
72 9
            $refOptions->setAccessible(true);
73 9
            $refOptions->setValue($mock, $options);
74
        }
75
76 9
        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 CacheInvalidation $httpCache
83
     * @param Request           $request
84
     * @param Response          $response
85
     */
86 2
    protected function setStoreMock(CacheInvalidation $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; use createMock() or getMockBuilder() instead

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
    }
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
    }
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
    }
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
    }
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
    }
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
    }
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
    }
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
    }
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...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
    }
300
301 1
    public function testAddListener()
302
    {
303 1
        $request = Request::create('/foo', 'GET');
304 1
        $response = new Response();
305
306 1
        $httpCache = $this->getHttpCachePartialMock(['lookup']);
307 1
        $simpleListener = new SimpleListener($this, $httpCache, $request);
0 ignored issues
show
Bug introduced by
It seems like $httpCache defined by $this->getHttpCachePartialMock(array('lookup')) on line 306 can also be of type object<FOS\HttpCache\Sym...ntDispatchingHttpCache> or object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\HttpCache\Test\SimpleListener::__construct() does only seem to accept object<FOS\HttpCache\Sym...ache\CacheInvalidation>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
308 1
        $httpCache->addListener(Events::PRE_HANDLE, [$simpleListener, 'callback']);
0 ignored issues
show
Bug introduced by
The method addListener does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in FOS\HttpCache\SymfonyCac...k_MockObject_MockObject.

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

Let’s take a look at an example:

class A
{
    public function foo() { }
}

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

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

Available Fixes

  1. Add an additional type-check:

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

    function someFunction(B $x) { /** ... */ }
    
Loading history...
309
310
        $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...
311 1
            ->expects($this->any())
312 1
            ->method('lookup')
313 1
            ->with($request)
314 1
            ->will($this->returnValue($response))
315
        ;
316
317 1
        $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST));
0 ignored issues
show
Bug introduced by
The method handle does only exist in FOS\HttpCache\SymfonyCac...entDispatchingHttpCache, but not in PHPUnit_Framework_MockObject_MockObject.

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

Let’s take a look at an example:

class A
{
    public function foo() { }
}

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

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

Available Fixes

  1. Add an additional type-check:

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

    function someFunction(B $x) { /** ... */ }
    
Loading history...
318 1
        $this->assertEquals(1, $simpleListener->calls);
319
    }
320
}
321
322
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...
323
{
324
    /**
325
     * @var int Count how many times preHandle has been called
326
     */
327
    public $preHandleCalls = 0;
328
329
    /**
330
     * @var int Count how many times postHandle has been called
331
     */
332
    public $postHandleCalls = 0;
333
334
    /**
335
     * @var int Count how many times preStore has been called
336
     */
337
    public $preStoreCalls = 0;
338
339
    /**
340
     * @var int Count how many times preInvalidate has been called
341
     */
342
    public $preInvalidateCalls = 0;
343
344
    /**
345
     * @var Response A response to set during the preHandle
346
     */
347
    public $preHandleResponse;
348
349
    /**
350
     * @var Response A response to set during the postHandle
351
     */
352
    public $postHandleResponse;
353
354
    /**
355
     * @var Response A response to set during the preStore
356
     */
357
    public $preStoreResponse;
358
359
    /**
360
     * @var Response A response to set during the preInvalidate
361
     */
362
    public $preInvalidateResponse;
363
364
    /**
365
     * @var EventDispatchingHttpCacheTestCase To do assertions
366
     */
367
    private $test;
368
369
    /**
370
     * @var CacheInvalidation The kernel to ensure the event carries the correct kernel
371
     */
372
    private $kernel;
373
374
    /**
375
     * @var Request The request to ensure the event carries the correct request
376
     */
377
    private $request;
378
379 8
    public function __construct(
380
        EventDispatchingHttpCacheTestCase $test,
381
        CacheInvalidation $kernel,
382
        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...
383
    ) {
384 8
        $this->test = $test;
385 8
        $this->kernel = $kernel;
386 8
        $this->request = $request;
387
    }
388
389 8
    public static function getSubscribedEvents()
390
    {
391
        return [
392 8
            Events::PRE_HANDLE => 'preHandle',
393 8
            Events::POST_HANDLE => 'postHandle',
394 8
            Events::PRE_STORE => 'preStore',
395 8
            Events::PRE_INVALIDATE => 'preInvalidate',
396
        ];
397
    }
398
399 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...
400
    {
401 4
        $this->test->assertSame($this->kernel, $event->getKernel());
402 4
        $this->test->assertSame($this->request, $event->getRequest());
403 4
        if ($this->preHandleResponse) {
404 2
            $event->setResponse($this->preHandleResponse);
405
        }
406 4
        ++$this->preHandleCalls;
407
    }
408
409 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...
410
    {
411 4
        $this->test->assertSame($this->kernel, $event->getKernel());
412 4
        $this->test->assertSame($this->request, $event->getRequest());
413 4
        if ($this->postHandleResponse) {
414 2
            $event->setResponse($this->postHandleResponse);
415
        }
416 4
        ++$this->postHandleCalls;
417
    }
418
419 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...
420
    {
421 2
        $this->test->assertSame($this->kernel, $event->getKernel());
422 2
        $this->test->assertSame($this->request, $event->getRequest());
423 2
        if ($this->preStoreResponse) {
424 1
            $event->setResponse($this->preStoreResponse);
425
        }
426 2
        ++$this->preStoreCalls;
427
    }
428
429 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...
430
    {
431 2
        $this->test->assertSame($this->kernel, $event->getKernel());
432 2
        $this->test->assertSame($this->request, $event->getRequest());
433 2
        if ($this->preInvalidateResponse) {
434 1
            $event->setResponse($this->preInvalidateResponse);
435
        }
436 2
        ++$this->preInvalidateCalls;
437
    }
438
}
439
440
class SimpleListener
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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

Loading history...
441
{
442
    public $calls = 0;
443
444
    /**
445
     * @var EventDispatchingHttpCacheTestCase To do assertions
446
     */
447
    private $test;
448
449
    /**
450
     * @var CacheInvalidation The kernel to ensure the event carries the correct kernel
451
     */
452
    private $kernel;
453
454
    /**
455
     * @var Request The request to ensure the event carries the correct request
456
     */
457
    private $request;
458
459 1
    public function __construct(
460
        EventDispatchingHttpCacheTestCase $test,
461
        CacheInvalidation $kernel,
462
        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...
463
    ) {
464 1
        $this->test = $test;
465 1
        $this->kernel = $kernel;
466 1
        $this->request = $request;
467
    }
468
469 1
    public function callback(CacheEvent $event)
470
    {
471 1
        $this->test->assertSame($this->kernel, $event->getKernel());
472 1
        $this->test->assertSame($this->request, $event->getRequest());
473 1
        ++$this->calls;
474
    }
475
}
476