Skip to content

Instantly share code, notes, and snippets.

@ezzatron
Created April 30, 2011 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezzatron/949397 to your computer and use it in GitHub Desktop.
Save ezzatron/949397 to your computer and use it in GitHub Desktop.
Situation where programmatic @Covers could be used in PHPUnit
<?php
namespace Typhoon\Type;
class StringTest extends \Typhoon\Test\TypeTestCase
{
protected function typeFixture()
{
return new String;
}
protected function expectedString()
{
return 'string';
}
public function validValues()
{
return array(
array('string'), // string
);
}
public function invalidValues()
{
return array(
array(null), // null
array(true), // boolean
array(1), // integer
array(.1), // float
array(array()), // array
array(new \stdClass), // object
array(function(){}), // closure
array($this->resourceFixture()), // resource
);
}
// methods below must be manually overridden to implement @covers
/**
* @covers \Typhoon\Type\String::string
* @group typhoon_types
*/
public function testString() { parent::testString(); }
/**
* @covers \Typhoon\Type\String::check
* @dataProvider validValues
* @group typhoon_types
*/
public function testCheckPass($value) { parent::testCheckPass($value); }
/**
* @covers \Typhoon\Type\String::check
* @dataProvider invalidValues
* @group typhoon_types
*/
public function testCheckFailure($value) { parent::testCheckFailure($value); }
}
<?php
namespace Typhoon\Test;
abstract class TypeTestCase extends \PHPUnit_Framework_TestCase
{
protected function tearDown()
{
if ($this->_resource) fclose($this->_resource);
}
/**
* @group typhoon_types
*/
public function testString()
{
$this->assertEquals($this->expectedString(), $this->typeFixture()->string());
}
/**
* @dataProvider validValues
* @group typhoon_types
*/
public function testCheckPass($value)
{
$this->assertTrue($this->typeFixture()->check($value));
}
/**
* @dataProvider invalidValues
* @group typhoon_types
*/
public function testCheckFailure($value)
{
$this->assertFalse($this->typeFixture()->check($value));
}
/**
* @return resource
*/
protected function resourceFixture()
{
if (!$this->_resource) $this->_resource = fopen(__FILE__, 'rb');
return $this->_resource;
}
/**
* @return \Typhoon\Type
*/
abstract protected function typeFixture();
/**
* @return string
*/
abstract protected function expectedString();
/**
* @return array
*/
abstract public function validValues();
/**
* @return array
*/
abstract public function invalidValues();
/**
* @var resource
*/
private $_resource;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment