Skip to content

Commit

Permalink
Merge pull request #111 from FriendsOfSymfony/src-test-coverage
Browse files Browse the repository at this point in the history
Add tests for src/Test
  • Loading branch information
dbu committed Aug 6, 2014
2 parents fb6fbe9 + f01f4ea commit 6841917
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Test/Proxy/AbstractProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function getIp()
public function setConfigFile($configFile)
{
if (!file_exists($configFile)) {
throw new \InvalidArgumentException('Cannot find specified Nginx config file: ' . $configFile);
throw new \InvalidArgumentException('Cannot find config file: ' . $configFile);
}

$this->configFile = $configFile;
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCache\Tests\Unit\Test\PHPUnit;

use FOS\HttpCache\Test\PHPUnit\IsCacheHitConstraint;

class IsCacheHitConstraintTest extends \PHPUnit_Framework_TestCase
{
/**
* @var IsCacheHitConstraint
*/
private $constraint;

public function setUp()
{
$this->constraint = new IsCacheHitConstraint('cache-header');
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
*/
public function testMatches()
{
$response = \Mockery::mock('\Guzzle\Http\Message\Response[getHeader]', array(null))
->shouldReceive('getHeader')
->once()
->andReturn('MISS')
->getMock();

$this->constraint->evaluate($response);
}
}
41 changes: 41 additions & 0 deletions tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCache\Tests\Unit\Test\PHPUnit;

use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint;

class IsCacheMissConstraintTest extends \PHPUnit_Framework_TestCase
{
/**
* @var IsCacheMissConstraint
*/
private $constraint;

public function setUp()
{
$this->constraint = new IsCacheMissConstraint('cache-header');
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
*/
public function testMatches()
{
$response = \Mockery::mock('\Guzzle\Http\Message\Response[getHeader]', array(null))
->shouldReceive('getHeader')
->once()
->andReturn('HIT')
->getMock();

$this->constraint->evaluate($response);
}
}
85 changes: 85 additions & 0 deletions tests/Unit/Test/Proxy/VarnishProxyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCache\Tests\Unit\Test\Proxy;

use FOS\HttpCache\Test\Proxy\VarnishProxy;

class VarnishTestCaseTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot find config file: nope.vcl
*/
public function testInvalidConfigFileThrowsException()
{
new VarnishProxy('nope.vcl');
}

public function testStart()
{
$proxy = new VarnishProxyMock('config.vcl');
$proxy->setBinary('/usr/sbin/varnishd');
$proxy->setConfigDir('/my/varnish/dir');
$proxy->setIp('192.168.0.1');
$proxy->setManagementPort(1331);
$proxy->setCacheDir('/tmp/cache/dir');
$proxy->start();

$this->assertEquals('/usr/sbin/varnishd', $proxy->command);
$this->assertEquals(
array(
'-a', '192.168.0.1:6181',
'-T', '192.168.0.1:1331',
'-f', 'config.vcl',
'-n', '/tmp/cache/dir',
'-p', 'vcl_dir=/my/varnish/dir',
'-P', '/tmp/foshttpcache-varnish.pid',
),
$proxy->arguments
);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Caching proxy cannot be reached at 127.0.0.1:6181
*/
public function testWaitThrowsException()
{
$proxy = new VarnishProxyMock('config.vcl');
$proxy->wait = false;

$proxy->start();
}
}

class VarnishProxyMock extends VarnishProxy
{
public $command;
public $arguments;
public $wait = true;

public function setConfigFile($configFile)
{
$this->configFile = $configFile;
}

protected function runCommand($command, array $arguments)
{
$this->command = $command;
$this->arguments = $arguments;
}

protected function wait($timeout, $callback)
{
return $this->wait;
}
}

0 comments on commit 6841917

Please sign in to comment.