FriendsOfSymfony /
FOSHttpCacheBundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the FOSHttpCacheBundle package. |
||
| 5 | * |
||
| 6 | * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace FOS\HttpCacheBundle\Tests\Unit\SymfonyCache; |
||
| 13 | |||
| 14 | use FOS\HttpCache\SymfonyCache\PurgeSubscriber; |
||
| 15 | use FOS\HttpCache\SymfonyCache\RefreshSubscriber; |
||
| 16 | use FOS\HttpCacheBundle\SymfonyCache\EventDispatchingHttpCache; |
||
| 17 | use FOS\HttpCache\SymfonyCache\CacheEvent; |
||
| 18 | use FOS\HttpCache\SymfonyCache\Events; |
||
| 19 | use FOS\HttpCache\SymfonyCache\UserContextSubscriber; |
||
| 20 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
| 21 | use Symfony\Component\HttpFoundation\Request; |
||
| 22 | use Symfony\Component\HttpFoundation\Response; |
||
| 23 | use Symfony\Component\HttpKernel\HttpKernelInterface; |
||
| 24 | |||
| 25 | class EventDispatchingHttpCacheTest extends \PHPUnit_Framework_TestCase |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @return EventDispatchingHttpCache|\PHPUnit_Framework_MockObject_MockObject |
||
| 29 | */ |
||
| 30 | protected function getHttpCachePartialMock(array $mockedMethods = null) |
||
| 31 | { |
||
| 32 | $mock = $this |
||
| 33 | ->getMockBuilder('\FOS\HttpCacheBundle\SymfonyCache\EventDispatchingHttpCache') |
||
| 34 | ->setMethods($mockedMethods) |
||
| 35 | ->disableOriginalConstructor() |
||
| 36 | ->getMock() |
||
| 37 | ; |
||
| 38 | |||
| 39 | // Force setting options property since we can't use original constructor. |
||
| 40 | $options = array( |
||
| 41 | 'debug' => false, |
||
| 42 | 'default_ttl' => 0, |
||
| 43 | 'private_headers' => array('Authorization', 'Cookie'), |
||
| 44 | 'allow_reload' => false, |
||
| 45 | 'allow_revalidate' => false, |
||
| 46 | 'stale_while_revalidate' => 2, |
||
| 47 | 'stale_if_error' => 60, |
||
| 48 | ); |
||
| 49 | |||
| 50 | $refHttpCache = new \ReflectionClass('Symfony\Component\HttpKernel\HttpCache\HttpCache'); |
||
| 51 | // Workaround for Symfony 2.3 where $options property is not defined. |
||
| 52 | View Code Duplication | if (!$refHttpCache->hasProperty('options')) { |
|
| 53 | $mock->options = $options; |
||
| 54 | } else { |
||
| 55 | $refOptions = $refHttpCache |
||
| 56 | ->getProperty('options'); |
||
| 57 | $refOptions->setAccessible(true); |
||
| 58 | $refOptions->setValue($mock, $options); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $mock; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function testCalled() |
||
| 65 | { |
||
| 66 | $catch = true; |
||
| 67 | $request = Request::create('/foo', 'GET'); |
||
| 68 | $response = new Response(); |
||
| 69 | |||
| 70 | $httpCache = $this->getHttpCachePartialMock(array('lookup')); |
||
| 71 | $listener = new TestListener($this, $httpCache, $request); |
||
| 72 | $httpCache->addSubscriber($listener); |
||
| 73 | $httpCache |
||
| 74 | ->expects($this->any()) |
||
| 75 | ->method('lookup') |
||
| 76 | ->with($request) |
||
| 77 | ->will($this->returnValue($response)) |
||
| 78 | ; |
||
| 79 | $httpCache->handle($request); |
||
| 80 | |||
| 81 | $this->assertEquals(1, $listener->hits); |
||
| 82 | $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function testAbort() |
||
| 86 | { |
||
| 87 | $catch = true; |
||
| 88 | $request = Request::create('/foo', 'GET'); |
||
| 89 | $response = new Response(); |
||
| 90 | |||
| 91 | $httpCache = $this->getHttpCachePartialMock(array('lookup')); |
||
| 92 | $listener = new TestListener($this, $httpCache, $request, $response); |
||
| 93 | $httpCache->addSubscriber($listener); |
||
|
0 ignored issues
–
show
|
|||
| 94 | $httpCache |
||
| 95 | ->expects($this->never()) |
||
| 96 | ->method('lookup') |
||
| 97 | ; |
||
| 98 | $httpCache->handle($request); |
||
| 99 | |||
| 100 | $this->assertEquals(1, $listener->hits); |
||
| 101 | $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @dataProvider configuredSubscribersProvider |
||
| 106 | */ |
||
| 107 | public function testConfiguredSubscribers(array $options, array $expectedSubscribers) |
||
| 108 | { |
||
| 109 | $cacheKernel = $this->getHttpCachePartialMock(array('getOptions')); |
||
| 110 | $cacheKernel |
||
| 111 | ->expects($this->once()) |
||
| 112 | ->method('getOptions') |
||
| 113 | ->will($this->returnValue($options)) |
||
| 114 | ; |
||
| 115 | $refCache = new \ReflectionClass(get_class($cacheKernel)); |
||
| 116 | $refGetSubscribers = $refCache->getMethod('getDefaultSubscribers'); |
||
| 117 | $refGetSubscribers->setAccessible(true); |
||
| 118 | |||
| 119 | $this->assertEquals($expectedSubscribers, $refGetSubscribers->invoke($cacheKernel)); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function configuredSubscribersProvider() |
||
| 123 | { |
||
| 124 | $all = array(new UserContextSubscriber(), new PurgeSubscriber(), new RefreshSubscriber()); |
||
| 125 | |||
| 126 | return array( |
||
| 127 | array(array(), $all), |
||
| 128 | array( |
||
| 129 | array( |
||
| 130 | 'fos_default_subscribers' => EventDispatchingHttpCache::SUBSCRIBER_ALL, |
||
| 131 | ), |
||
| 132 | $all, |
||
| 133 | ), |
||
| 134 | array( |
||
| 135 | array( |
||
| 136 | 'fos_default_subscribers' => EventDispatchingHttpCache::SUBSCRIBER_NONE, |
||
| 137 | ), |
||
| 138 | array(), |
||
| 139 | ), |
||
| 140 | array( |
||
| 141 | array( |
||
| 142 | 'fos_default_subscribers' => EventDispatchingHttpCache::SUBSCRIBER_USER_CONTEXT, |
||
| 143 | ), |
||
| 144 | array(new UserContextSubscriber()), |
||
| 145 | ), |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | class TestListener implements EventSubscriberInterface |
||
| 151 | { |
||
| 152 | public $hits = 0; |
||
| 153 | private $test; |
||
| 154 | private $kernel; |
||
| 155 | private $request; |
||
| 156 | private $response; |
||
| 157 | |||
| 158 | public function __construct($test, $kernel, $request, $response = null) |
||
| 159 | { |
||
| 160 | $this->test = $test; |
||
| 161 | $this->kernel = $kernel; |
||
| 162 | $this->request = $request; |
||
| 163 | $this->response = $response; |
||
| 164 | } |
||
| 165 | |||
| 166 | public static function getSubscribedEvents() |
||
| 167 | { |
||
| 168 | return array(Events::PRE_HANDLE => 'preHandle'); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function preHandle(CacheEvent $event) |
||
| 172 | { |
||
| 173 | $this->test->assertSame($this->kernel, $event->getKernel()); |
||
| 174 | $this->test->assertSame($this->request, $event->getRequest()); |
||
| 175 | if ($this->response) { |
||
| 176 | $event->setResponse($this->response); |
||
| 177 | } |
||
| 178 | ++$this->hits; |
||
| 179 | } |
||
| 180 | } |
||
| 181 |
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: