Skip to content

Commit 48961bd

Browse files
committed
Merge pull request #104 from FriendsOfSymfony/more-tests
add some more tests
2 parents c63e30e + 49ac663 commit 48961bd

File tree

5 files changed

+76
-16
lines changed

5 files changed

+76
-16
lines changed

src/UserContext/HashGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace FOS\HttpCache\UserContext;
1313

14+
use FOS\HttpCache\Exception\InvalidArgumentException;
15+
1416
/**
1517
* Generate a hash for a UserContext by getting all the parameters needed across all registered services
1618
*/
@@ -26,12 +28,12 @@ class HashGenerator
2628
*
2729
* @param ContextProviderInterface[] $providers
2830
*
29-
* @throws \InvalidArgumentException If no providers are supplied
31+
* @throws InvalidArgumentException If no providers are supplied
3032
*/
3133
public function __construct(array $providers)
3234
{
3335
if (0 === count($providers)) {
34-
throw new \InvalidArgumentException('You must supply at least one provider');
36+
throw new InvalidArgumentException('You must supply at least one provider');
3537
}
3638

3739
foreach ($providers as $provider) {

tests/Unit/CacheInvalidatorTest.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
use FOS\HttpCache\Exception\UnsupportedProxyOperationException;
2020
use FOS\HttpCache\ProxyClient\Varnish;
2121
use \Mockery;
22+
use Symfony\Component\EventDispatcher\EventDispatcher;
2223

2324
class CacheInvalidatorTest extends \PHPUnit_Framework_TestCase
2425
{
2526
public function testSupportsTrue()
2627
{
27-
$httpCache = new Varnish(array('localhost'));
28+
$proxyClient = new Varnish(array('localhost'));
2829

29-
$cacheInvalidator = new CacheInvalidator($httpCache);
30+
$cacheInvalidator = new CacheInvalidator($proxyClient);
3031

3132
$this->assertTrue($cacheInvalidator->supports(CacheInvalidator::PATH));
3233
$this->assertTrue($cacheInvalidator->supports(CacheInvalidator::REFRESH));
@@ -35,9 +36,9 @@ public function testSupportsTrue()
3536

3637
public function testSupportsFalse()
3738
{
38-
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface');
39+
$proxyClient = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface');
3940

40-
$cacheInvalidator = new CacheInvalidator($httpCache);
41+
$cacheInvalidator = new CacheInvalidator($proxyClient);
4142

4243
$this->assertFalse($cacheInvalidator->supports(CacheInvalidator::PATH));
4344
$this->assertFalse($cacheInvalidator->supports(CacheInvalidator::REFRESH));
@@ -49,21 +50,21 @@ public function testSupportsFalse()
4950
*/
5051
public function testSupportsInvalid()
5152
{
52-
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface');
53+
$proxyClient = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface');
5354

54-
$cacheInvalidator = new CacheInvalidator($httpCache);
55+
$cacheInvalidator = new CacheInvalidator($proxyClient);
5556

5657
$cacheInvalidator->supports('garbage');
5758
}
5859

5960
public function testInvalidatePath()
6061
{
61-
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface')
62+
$purge = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface')
6263
->shouldReceive('purge')->once()->with('/my/route')
6364
->shouldReceive('flush')->once()
6465
->getMock();
6566

66-
$cacheInvalidator = new CacheInvalidator($httpCache);
67+
$cacheInvalidator = new CacheInvalidator($purge);
6768

6869
$cacheInvalidator
6970
->invalidatePath('/my/route')
@@ -74,12 +75,12 @@ public function testInvalidatePath()
7475
public function testRefreshPath()
7576
{
7677
$headers = array('X' => 'Y');
77-
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface')
78+
$refresh = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface')
7879
->shouldReceive('refresh')->once()->with('/my/route', $headers)
7980
->shouldReceive('flush')->never()
8081
->getMock();
8182

82-
$cacheInvalidator = new CacheInvalidator($httpCache);
83+
$cacheInvalidator = new CacheInvalidator($refresh);
8384

8485
$cacheInvalidator
8586
->refreshPath('/my/route', $headers)
@@ -142,8 +143,8 @@ public function testInvalidateTagsCustomHeader()
142143

143144
public function testMethodException()
144145
{
145-
$proxy = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface');
146-
$cacheInvalidator = new CacheInvalidator($proxy);
146+
$proxyClient = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface');
147+
$cacheInvalidator = new CacheInvalidator($proxyClient);
147148
try {
148149
$cacheInvalidator->invalidatePath('/');
149150
$this->fail('Expected exception');
@@ -187,11 +188,11 @@ public function testProxyClientExceptionsAreLogged()
187188
$exceptions = new ExceptionCollection();
188189
$exceptions->add($unreachableException)->add($responseException);
189190

190-
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface')
191+
$proxyClient = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface')
191192
->shouldReceive('flush')->once()->andThrow($exceptions)
192193
->getMock();
193194

194-
$cacheInvalidator = new CacheInvalidator($httpCache);
195+
$cacheInvalidator = new CacheInvalidator($proxyClient);
195196

196197
$logger = \Mockery::mock('\Psr\Log\LoggerInterface')
197198
->shouldReceive('log')->once()
@@ -212,4 +213,14 @@ public function testProxyClientExceptionsAreLogged()
212213
->flush()
213214
;
214215
}
216+
217+
public function testEventDispatcher()
218+
{
219+
$proxyClient = new Varnish(array('localhost'));
220+
221+
$cacheInvalidator = new CacheInvalidator($proxyClient);
222+
$eventDispatcher = new EventDispatcher();
223+
$cacheInvalidator->setEventDispatcher($eventDispatcher);
224+
$this->assertSame($eventDispatcher, $cacheInvalidator->getEventDispatcher());
225+
}
215226
}

tests/Unit/ProxyClient/VarnishTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ public function testBanEverything()
4545
$this->assertEquals('fos.lo', $headers->get('Host'));
4646
}
4747

48+
public function testBanHeaders()
49+
{
50+
$varnish = new Varnish(array('http://127.0.0.1:123'), 'fos.lo', $this->client);
51+
$varnish->setDefaultBanHeaders(
52+
array('A' => 'B')
53+
);
54+
$varnish->setDefaultBanHeader('Test', '.*');
55+
$varnish->ban(array())->flush();
56+
57+
$requests = $this->getRequests();
58+
$this->assertCount(1, $requests);
59+
$this->assertEquals('BAN', $requests[0]->getMethod());
60+
61+
$headers = $requests[0]->getHeaders();
62+
$this->assertEquals('.*', $headers->get('Test'));
63+
$this->assertEquals('B', $headers->get('A'));
64+
$this->assertEquals('fos.lo', $headers->get('Host'));
65+
}
66+
4867
public function testBanPath()
4968
{
5069
$varnish = new Varnish(array('http://127.0.0.1:123'), 'fos.lo', $this->client);
@@ -62,6 +81,17 @@ public function testBanPath()
6281
$this->assertEquals('text/html', $headers->get('X-Content-Type'));
6382
}
6483

84+
/**
85+
* @expectedException \FOS\HttpCache\Exception\InvalidArgumentException
86+
*/
87+
public function testBanPathEmptyHost()
88+
{
89+
$varnish = new Varnish(array('http://127.0.0.1:123'), 'fos.lo', $this->client);
90+
91+
$hosts = array();
92+
$varnish->banPath('/articles/.*', 'text/html', $hosts);
93+
}
94+
6595
public function testPurge()
6696
{
6797
$self = $this; // For PHP 5.3

tests/Unit/UserContext/HashGeneratorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public function testGenerateHash()
2525

2626
$this->assertEquals($expectedHash, $hashGenerator->generateHash());
2727
}
28+
29+
/**
30+
* @expectedException \FOS\HttpCache\Exception\InvalidArgumentException
31+
*/
32+
public function testConstructorError()
33+
{
34+
new HashGenerator(array());
35+
}
2836
}
2937

3038
class FooProvider implements ContextProviderInterface

tests/Unit/UserContext/UserContextTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,14 @@ public function testSetParameters()
4040
$this->assertFalse($userContext->hasParameter('authenticated'));
4141
$this->assertTrue($userContext->hasParameter('foo'));
4242
$this->assertTrue($userContext->hasParameter('roles'));
43+
44+
$parameters = array();
45+
foreach($userContext as $name => $value) {
46+
$parameters[$name] = $value;
47+
}
48+
$this->assertEquals(
49+
array('roles' => array('ROLE_USER'), 'foo' => 'bar'),
50+
$parameters
51+
);
4352
}
4453
}

0 commit comments

Comments
 (0)