Skip to content

Commit 6841917

Browse files
committed
Merge pull request #111 from FriendsOfSymfony/src-test-coverage
Add tests for src/Test
2 parents fb6fbe9 + f01f4ea commit 6841917

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
lines changed

src/Test/Proxy/AbstractProxy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function getIp()
175175
public function setConfigFile($configFile)
176176
{
177177
if (!file_exists($configFile)) {
178-
throw new \InvalidArgumentException('Cannot find specified Nginx config file: ' . $configFile);
178+
throw new \InvalidArgumentException('Cannot find config file: ' . $configFile);
179179
}
180180

181181
$this->configFile = $configFile;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Unit\Test\PHPUnit;
13+
14+
use FOS\HttpCache\Test\PHPUnit\IsCacheHitConstraint;
15+
16+
class IsCacheHitConstraintTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var IsCacheHitConstraint
20+
*/
21+
private $constraint;
22+
23+
public function setUp()
24+
{
25+
$this->constraint = new IsCacheHitConstraint('cache-header');
26+
}
27+
28+
/**
29+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
30+
*/
31+
public function testMatches()
32+
{
33+
$response = \Mockery::mock('\Guzzle\Http\Message\Response[getHeader]', array(null))
34+
->shouldReceive('getHeader')
35+
->once()
36+
->andReturn('MISS')
37+
->getMock();
38+
39+
$this->constraint->evaluate($response);
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Unit\Test\PHPUnit;
13+
14+
use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint;
15+
16+
class IsCacheMissConstraintTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var IsCacheMissConstraint
20+
*/
21+
private $constraint;
22+
23+
public function setUp()
24+
{
25+
$this->constraint = new IsCacheMissConstraint('cache-header');
26+
}
27+
28+
/**
29+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
30+
*/
31+
public function testMatches()
32+
{
33+
$response = \Mockery::mock('\Guzzle\Http\Message\Response[getHeader]', array(null))
34+
->shouldReceive('getHeader')
35+
->once()
36+
->andReturn('HIT')
37+
->getMock();
38+
39+
$this->constraint->evaluate($response);
40+
}
41+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Unit\Test\Proxy;
13+
14+
use FOS\HttpCache\Test\Proxy\VarnishProxy;
15+
16+
class VarnishTestCaseTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @expectedException \InvalidArgumentException
20+
* @expectedExceptionMessage Cannot find config file: nope.vcl
21+
*/
22+
public function testInvalidConfigFileThrowsException()
23+
{
24+
new VarnishProxy('nope.vcl');
25+
}
26+
27+
public function testStart()
28+
{
29+
$proxy = new VarnishProxyMock('config.vcl');
30+
$proxy->setBinary('/usr/sbin/varnishd');
31+
$proxy->setConfigDir('/my/varnish/dir');
32+
$proxy->setIp('192.168.0.1');
33+
$proxy->setManagementPort(1331);
34+
$proxy->setCacheDir('/tmp/cache/dir');
35+
$proxy->start();
36+
37+
$this->assertEquals('/usr/sbin/varnishd', $proxy->command);
38+
$this->assertEquals(
39+
array(
40+
'-a', '192.168.0.1:6181',
41+
'-T', '192.168.0.1:1331',
42+
'-f', 'config.vcl',
43+
'-n', '/tmp/cache/dir',
44+
'-p', 'vcl_dir=/my/varnish/dir',
45+
'-P', '/tmp/foshttpcache-varnish.pid',
46+
),
47+
$proxy->arguments
48+
);
49+
}
50+
51+
/**
52+
* @expectedException \RuntimeException
53+
* @expectedExceptionMessage Caching proxy cannot be reached at 127.0.0.1:6181
54+
*/
55+
public function testWaitThrowsException()
56+
{
57+
$proxy = new VarnishProxyMock('config.vcl');
58+
$proxy->wait = false;
59+
60+
$proxy->start();
61+
}
62+
}
63+
64+
class VarnishProxyMock extends VarnishProxy
65+
{
66+
public $command;
67+
public $arguments;
68+
public $wait = true;
69+
70+
public function setConfigFile($configFile)
71+
{
72+
$this->configFile = $configFile;
73+
}
74+
75+
protected function runCommand($command, array $arguments)
76+
{
77+
$this->command = $command;
78+
$this->arguments = $arguments;
79+
}
80+
81+
protected function wait($timeout, $callback)
82+
{
83+
return $this->wait;
84+
}
85+
}

0 commit comments

Comments
 (0)