Skip to content

Commit 18624c5

Browse files
authored
Merge pull request #316 from FriendsOfSymfony/improve-coverage
improve test coverage and some cleanup
2 parents a9714d8 + 23421fc commit 18624c5

15 files changed

+382
-31
lines changed

src/Test/EventDispatchingHttpCacheTestCase.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
22+
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
2123
use Symfony\Component\HttpKernel\HttpKernelInterface;
2224

2325
/**
@@ -61,8 +63,8 @@ protected function getHttpCachePartialMock(array $mockedMethods = null)
6163
'stale_if_error' => 60,
6264
];
6365

64-
$refHttpCache = new \ReflectionClass('Symfony\Component\HttpKernel\HttpCache\HttpCache');
65-
// Workaround for Symfony 2.3 where $options property is not defined.
66+
$refHttpCache = new \ReflectionClass(HttpCache::class);
67+
// Workaround for Symfony 2 where $options property is not defined.
6668
if (!$refHttpCache->hasProperty('options')) {
6769
$mock->options = $options;
6870
} else {
@@ -83,13 +85,13 @@ protected function getHttpCachePartialMock(array $mockedMethods = null)
8385
*/
8486
protected function setStoreMock(CacheInvalidationInterface $httpCache, Request $request, Response $response)
8587
{
86-
$store = $this->getMock('Symfony\Component\HttpKernel\HttpCache\StoreInterface');
88+
$store = $this->getMock(StoreInterface::class);
8789
$store
8890
->expects($this->once())
8991
->method('write')
9092
->with($request, $response)
9193
;
92-
$refHttpCache = new \ReflectionClass('Symfony\Component\HttpKernel\HttpCache\HttpCache');
94+
$refHttpCache = new \ReflectionClass(HttpCache::class);
9395
$refStore = $refHttpCache->getProperty('store');
9496
$refStore->setAccessible(true);
9597
$refStore->setValue($httpCache, $store);

src/Test/NginxTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ protected function tearDown()
7070
*/
7171
protected function getConfigFile()
7272
{
73+
// @codeCoverageIgnoreStart
7374
if (!defined('NGINX_FILE')) {
7475
throw new \Exception(
7576
'Specify the NGINX'
7677
.' configuration file path in phpunit.xml or override getConfigFile()'
7778
);
7879
}
80+
// @codeCoverageIgnoreEnd
7981

8082
// NGINX needs an absolute path
8183
return realpath(NGINX_FILE);
@@ -118,11 +120,13 @@ protected function getCacheDir()
118120
*/
119121
protected function getHostName()
120122
{
123+
// @codeCoverageIgnoreStart
121124
if (!defined('WEB_SERVER_HOSTNAME')) {
122125
throw new \Exception(
123126
'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml'
124127
);
125128
}
129+
// @codeCoverageIgnoreEnd
126130

127131
return WEB_SERVER_HOSTNAME;
128132
}

src/Test/Proxy/SymfonyProxy.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ class SymfonyProxy implements ProxyInterface
2626
*/
2727
public function getCacheDir()
2828
{
29-
return defined('SYMFONY_CACHE_DIR') ? SYMFONY_CACHE_DIR : sys_get_temp_dir().'/foshttpcache-symfony';
29+
$path = defined('SYMFONY_CACHE_DIR') ? SYMFONY_CACHE_DIR : sys_get_temp_dir().'/foshttpcache-symfony';
30+
if (!$path || '/' === $path) {
31+
throw new \RuntimeException('Invalid test setup, the cache dir is '.$path);
32+
}
33+
34+
return $path;
3035
}
3136

3237
/**
@@ -50,16 +55,20 @@ public function stop()
5055
*/
5156
public function clear()
5257
{
53-
if (is_dir($this->getCacheDir())) {
54-
$path = realpath($this->getCacheDir());
55-
if (!$this->getCacheDir() || '/' == $path) {
56-
throw new \Exception('Invalid test setup, the cache dir is '.$path);
57-
}
58-
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
59-
system('DEL /S '.$path);
60-
} else {
61-
system('rm -r '.$path);
62-
}
58+
$path = realpath($this->getCacheDir());
59+
60+
// false means the directory does not exist yet - it surely is empty then
61+
if (!is_dir($path)) {
62+
return;
63+
}
64+
65+
$path = $this->getCacheDir();
66+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
67+
// @codeCoverageIgnoreStart
68+
system('DEL /S '.$path);
69+
} else {
70+
// @codeCoverageIgnoreEnd
71+
system('rm -r '.$path);
6372
}
6473
}
6574
}

src/Test/SymfonyTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ protected function setUp()
6363
*/
6464
protected function getCachingProxyPort()
6565
{
66+
// @codeCoverageIgnoreStart
6667
if (!defined('WEB_SERVER_PORT')) {
6768
throw new \Exception('Set WEB_SERVER_PORT in your phpunit.xml');
6869
}
70+
// @codeCoverageIgnoreEnd
6971

7072
return WEB_SERVER_PORT;
7173
}
@@ -79,11 +81,13 @@ protected function getCachingProxyPort()
7981
*/
8082
protected function getHostName()
8183
{
84+
// @codeCoverageIgnoreStart
8285
if (!defined('WEB_SERVER_HOSTNAME')) {
8386
throw new \Exception(
8487
'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml'
8588
);
8689
}
90+
// @codeCoverageIgnoreEnd
8791

8892
return WEB_SERVER_HOSTNAME;
8993
}

src/Test/VarnishTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ protected function tearDown()
8181
*/
8282
protected function getConfigFile()
8383
{
84+
// @codeCoverageIgnoreStart
8485
if (!defined('VARNISH_FILE')) {
8586
throw new \Exception(
8687
'Specify the varnish configuration file path in phpunit.xml or override getConfigFile()'
8788
);
8889
}
90+
// @codeCoverageIgnoreEnd
8991

9092
return VARNISH_FILE;
9193
}
@@ -194,11 +196,13 @@ protected function getProxyClient()
194196
*/
195197
protected function getHostName()
196198
{
199+
// @codeCoverageIgnoreStart
197200
if (!defined('WEB_SERVER_HOSTNAME')) {
198201
throw new \Exception(
199202
'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml'
200203
);
201204
}
205+
// @codeCoverageIgnoreEnd
202206

203207
return WEB_SERVER_HOSTNAME;
204208
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\Tests\Functional\Varnish;
13+
14+
use FOS\HttpCache\SymfonyCache\CacheInvalidationInterface;
15+
use FOS\HttpCache\SymfonyCache\CustomTtlListener;
16+
use FOS\HttpCache\SymfonyCache\DebugListener;
17+
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
18+
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
19+
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
20+
use FOS\HttpCache\SymfonyCache\UserContextSubscriber;
21+
use Symfony\Component\HttpFoundation\Request;
22+
use Symfony\Component\HttpFoundation\Response;
23+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
24+
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
25+
use Symfony\Component\HttpKernel\HttpKernelInterface;
26+
27+
/**
28+
* @group symfony
29+
*/
30+
class EventDispatchingHttpCacheTest extends \PHPUnit_Framework_TestCase
31+
{
32+
public function testSubscribers()
33+
{
34+
$request = new Request();
35+
$expectedResponse = new Response();
36+
$expectedResponse->headers->set('X-Reverse-Proxy-TTL', 60);
37+
38+
$httpKernel = \Mockery::mock(HttpKernelInterface::class)
39+
->shouldReceive('handle')
40+
->withArgs([$request, HttpKernelInterface::MASTER_REQUEST, true])
41+
->andReturn($expectedResponse)
42+
->getMock();
43+
$store = \Mockery::mock(StoreInterface::class)
44+
// need to declare the cleanup function explicitly to avoid issue between register_shutdown_function and mockery
45+
->shouldReceive('cleanup')
46+
->atMost(1)
47+
->getMock();
48+
$kernel = new AppCache($httpKernel, $store);
49+
$kernel->addSubscriber(new CustomTtlListener());
50+
$kernel->addSubscriber(new DebugListener());
51+
$kernel->addSubscriber(new PurgeSubscriber());
52+
$kernel->addSubscriber(new RefreshSubscriber());
53+
$kernel->addSubscriber(new UserContextSubscriber());
54+
55+
$response = $kernel->handle($request);
56+
$this->assertSame($expectedResponse, $response);
57+
$this->assertFalse($response->headers->has('X-Reverse-Proxy-TTL'));
58+
}
59+
}
60+
61+
class AppCache extends HttpCache implements CacheInvalidationInterface
62+
{
63+
use EventDispatchingHttpCache;
64+
65+
/**
66+
* Made public to allow event subscribers to do refresh operations.
67+
*
68+
* {@inheritdoc}
69+
*/
70+
public function fetch(Request $request, $catch = false)
71+
{
72+
parent::fetch($request, $catch);
73+
}
74+
}

tests/Unit/SymfonyCache/CustomTtlListenerTest.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
use FOS\HttpCache\SymfonyCache\CacheEvent;
1515
use FOS\HttpCache\SymfonyCache\CacheInvalidationInterface;
1616
use FOS\HttpCache\SymfonyCache\CustomTtlListener;
17+
use Mockery\MockInterface;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpFoundation\Response;
1920

2021
class CustomTtlListenerTest extends \PHPUnit_Framework_TestCase
2122
{
2223
/**
23-
* @var CacheInvalidationInterface|\PHPUnit_Framework_MockObject_MockObject
24+
* @var CacheInvalidationInterface|MockInterface
2425
*/
2526
private $kernel;
2627

@@ -65,6 +66,23 @@ public function testCustomTtlNoSmaxage()
6566
$this->assertSame('false', $response->headers->get(CustomTtlListener::SMAXAGE_BACKUP));
6667
}
6768

69+
public function testNoCustomTtl()
70+
{
71+
$ttlListener = new CustomTtlListener();
72+
$request = Request::create('http://example.com/foo', 'GET');
73+
$response = new Response('', 200, array(
74+
'Cache-Control' => 'max-age=30, s-maxage=33',
75+
));
76+
$event = new CacheEvent($this->kernel, $request, $response);
77+
78+
$ttlListener->useCustomTtl($event);
79+
$response = $event->getResponse();
80+
81+
$this->assertInstanceOf(Response::class, $response);
82+
$this->assertSame('33', $response->headers->getCacheControlDirective('s-maxage'));
83+
$this->assertFalse($response->headers->has(CustomTtlListener::SMAXAGE_BACKUP));
84+
}
85+
6886
public function testCleanup()
6987
{
7088
$ttlListener = new CustomTtlListener();
@@ -92,7 +110,7 @@ public function testCleanupNoSmaxage()
92110
$request = Request::create('http://example.com/foo', 'GET');
93111
$response = new Response('', 200, array(
94112
'X-Reverse-Proxy-TTL' => '120',
95-
'Cache-Control' => 's-maxage: 120, max-age: 30',
113+
'Cache-Control' => 's-maxage=120, max-age=30',
96114
CustomTtlListener::SMAXAGE_BACKUP => 'false',
97115
));
98116
$event = new CacheEvent($this->kernel, $request, $response);
@@ -105,4 +123,22 @@ public function testCleanupNoSmaxage()
105123
$this->assertFalse($response->headers->has('X-Reverse-Proxy-TTL'));
106124
$this->assertFalse($response->headers->has(CustomTtlListener::SMAXAGE_BACKUP));
107125
}
126+
127+
public function testCleanupNoCustomTtl()
128+
{
129+
$ttlListener = new CustomTtlListener();
130+
$request = Request::create('http://example.com/foo', 'GET');
131+
$response = new Response('', 200, array(
132+
'Cache-Control' => 's-maxage=60, max-age=30',
133+
));
134+
$event = new CacheEvent($this->kernel, $request, $response);
135+
136+
$ttlListener->cleanResponse($event);
137+
$response = $event->getResponse();
138+
139+
$this->assertInstanceOf(Response::class, $response);
140+
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
141+
$this->assertFalse($response->headers->has('X-Reverse-Proxy-TTL'));
142+
$this->assertFalse($response->headers->has(CustomTtlListener::SMAXAGE_BACKUP));
143+
}
108144
}

tests/Unit/SymfonyCache/EventDispatchingHttpCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class EventDispatchingHttpCacheTest extends EventDispatchingHttpCacheTestCase
2424
{
2525
protected function getCacheClass()
2626
{
27-
return '\FOS\HttpCache\Tests\Unit\SymfonyCache\AppCache';
27+
return AppCache::class;
2828
}
2929
}
3030

tests/Unit/SymfonyCache/PurgeSubscriberTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222

2323
class PurgeSubscriberTest extends \PHPUnit_Framework_TestCase
2424
{
25+
/**
26+
* This tests a sanity check in the AbstractControlledListener.
27+
*
28+
* @expectedException \InvalidArgumentException
29+
* @expectedExceptionMessage You may not set both a request matcher and an IP
30+
*/
31+
public function testConstructorOverspecified()
32+
{
33+
new PurgeSubscriber([
34+
'client_matcher' => new RequestMatcher('/forbidden'),
35+
'client_ips' => ['1.2.3.4'],
36+
]);
37+
}
38+
2539
public function testPurgeAllowed()
2640
{
2741
/** @var StoreInterface $store */

tests/Unit/SymfonyCache/UserContextSubscriberTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public function testUserHashUserWithSession($arg, $options)
118118
{
119119
$userContextSubscriber = new UserContextSubscriber($arg);
120120

121-
$catch = true;
122121
$sessionId1 = 'my_session_id';
123122
$sessionId2 = 'another_session_id';
124123
$cookies = [

0 commit comments

Comments
 (0)