Skip to content

Instantly share code, notes, and snippets.

@ezzatron
Created May 3, 2011 03:33
Show Gist options
  • Save ezzatron/952773 to your computer and use it in GitHub Desktop.
Save ezzatron/952773 to your computer and use it in GitHub Desktop.
Example of using a test fixture method in a PHPUnit test case base class
* ExceptionTestCase.php is an abstract test case base class. It is capable of being used to test both abstract and concrete exception classes.
* ExceptionTest.php is an example of a test for an abstract exception.
* NotImplementedTest.php is an example of a test for a concrete exception.
* Child classes are forced to implement methods providing the name of the class, and default arguments to instantiate with. These are used by the test case class to create a fixture.
* In this case it works especially well because it allows you to test the fact that no matter what the child class may do in its constructor, it can never break the Exception chaining mechanism without causing a failing test.
<?php
namespace Typhoon\Exception;
use Typhoon\Primitive\String;
use Typhoon\Test\ExceptionTestCase;
class ExceptionTest extends ExceptionTestCase
{
/**
* @return string
*/
protected function exceptionClass()
{
return __NAMESPACE__.'\Exception';
}
/**
* @return array
*/
protected function defaultArguments()
{
return array($this->_message);
}
protected function setUp()
{
$this->_message = new String('foo');
}
/**
* @covers \Typhoon\Exception\Exception::__construct
*/
public function testConstructor()
{
$this->assertEquals((string)$this->_message, $this->exceptionFixture()->getMessage());
parent::testConstructor();
}
/**
* @var String
*/
protected $_message;
}
<?php
namespace Typhoon\Test;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
use Typhoon\Exception\Exception;
abstract class ExceptionTestCase extends PHPUnit_Framework_TestCase
{
/**
* @return Exception
*/
protected function exceptionFixture(array $arguments = null, \Exception $previous = null)
{
if (null === $arguments) $arguments = $this->defaultArguments();
if ($previous) $arguments[] = $previous;
$reflector = new ReflectionClass($this->exceptionClass());
if ($reflector->isAbstract())
{
return $this->getMockForAbstractClass($this->exceptionClass(), $arguments);
}
return $reflector->newInstanceArgs($arguments);
}
protected function setUp()
{
$this->_previous = new \Exception();
}
public function testConstructor()
{
$this->assertNull($this->exceptionFixture()->getPrevious());
$this->assertSame($this->_previous, $this->exceptionFixture(null, $this->_previous)->getPrevious());
}
/**
* @return string
*/
abstract protected function exceptionClass();
/**
* @return array
*/
abstract protected function defaultArguments();
/**
* @var \Exception
*/
protected $_previous;
}
<?php
namespace Typhoon\Exception;
use Typhoon\Primitive\String;
use Typhoon\Test\ExceptionTestCase;
class NotImplementedTest extends ExceptionTestCase
{
/**
* @return string
*/
protected function exceptionClass()
{
return __NAMESPACE__.'\NotImplemented';
}
/**
* @return array
*/
protected function defaultArguments()
{
return array($this->_feature);
}
protected function setUp()
{
$this->_feature = new String('foo');
}
/**
* @covers \Typhoon\Exception\NotImplemented::__construct
*/
public function testConstructor()
{
$this->assertEquals($this->_feature.' is not implemented.', $this->exceptionFixture()->getMessage());
parent::testConstructor();
}
/**
* @var String
*/
protected $_feature;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment