Skip to content

Instantly share code, notes, and snippets.

@pcdinh
Created July 17, 2009 12:39
Show Gist options
  • Save pcdinh/149032 to your computer and use it in GitHub Desktop.
Save pcdinh/149032 to your computer and use it in GitHub Desktop.
<?php
/**
* A test case defines the fixture to run multiple tests. To define a test case
* 1. implement a subclass of TinyTestCase
* 2. define instance variables that store the state of the fixture
* 3. initialize the fixture state by overriding setUp()
* 4. clean-up after a test by overriding tearDown().
*
* @author pcdinh
* @since July 17, 2009
*/
class TinyTestCase
{
/**
* Tests in this test case.
*
* @var array
*/
public $tests = array();
/**
* Assertion results of a test.
*
* @var array
*/
public $asserted = array();
/**
* Warnings on executing test cases.
*
* @var array
*/
public $warnings = array();
/**
* Assertion count in a test.
*
* @var int
*/
public $assertionCount = 0;
/**
* PHP error messages.
*
* @var string
*/
public $phpMessages;
/**
* Asserts that value in question is a boolean TRUE.
*
* @param mixed $value
* @param string $message
*/
public function assertTrue($value, $message = '')
{
$this->assertionCount++;
$this->asserted[] = new TinyBooleanTrueAssertion($value, $message);
}
/**
* Sets up the fixture, for example, open a network connection.
*/
public function setUp()
{
}
/**
* Tears down the fixture, for example, close a network connection.
*/
public function tearDown()
{
}
/**
* Executes all the test cases.
*/
public function run()
{
$this->setUp();
// Test case class name
$class = get_class($this);
echo "\n";
echo "|----------------------------------------------------------------|\n";
echo "| Running tests |\n";
echo "|----------------------------------------------------------------|\n";
echo "\n";
echo "\n";
echo "Processing test case $class ... \n";
echo "\n";
ob_start();
// Create an instance of the ReflectionClass class
$reflector = new ReflectionClass($class);
$methods = $reflector->getMethods();
foreach ($methods as $method)
{
$name = $method->getName();
if (0 === strpos($name, 'test'))
{
$method->invoke($this);
if (0 === count($this->asserted))
{
$this->warnings[] = 'Test '.$class.'->'.$name.'() does not contain any assertion.';
continue;
}
$this->tests[$name] = $this->asserted;
}
$this->asserted = array();
}
$this->phpMessages = ob_get_contents();
ob_end_clean();
$count = 0;
$failed = array();
$passed = 0;
foreach ($this->tests as $name => $test)
{
foreach ($test as $idx => $assertion)
{
if (true === $assertion->valid())
{
echo '.';
$passed++;
}
else
{
$failed[] = 'Assertion No.'.$idx.' in '.$class.'->'.$name.'(): '.$assertion->getFeedback();
echo 'x';
}
}
}
echo "\n";
echo "-----------------------------------------------------------------\n";
echo "\n";
echo "\n";
echo "Warning\n";
echo "-------\n";
if (count($this->warnings) > 0)
{
echo implode("\n", $this->warnings);
echo "\n";
}
else
{
echo "No warnings.\n";
}
echo "\n";
echo "\n";
echo "Passed\n";
echo "-------\n";
if (empty($failed))
{
echo "\nAll tests ($passed) passed.\n";
}
elseif ($passed < 2)
{
echo "$passed test passed.\n";
}
else
{
echo "$passed tests passed.\n";
}
echo "\n";
echo "\n";
echo "Failed\n";
echo "-------\n";
$cfailed = count($failed);
if ($cfailed < 2)
{
echo "$cfailed test failed.\n\n";
}
else
{
echo "$cfailed tests failed.\n\n";
}
if ($cfailed > 0)
{
echo implode("\n-\n", $failed);
echo "\n";
}
if (false === empty($this->phpMessages))
{
echo $this->phpMessages;
}
echo "\n";
echo "-------\n";
echo 'Completed'."\n";
$this->tearDown();
return;
}
}
/**
* A set of methods in an assertion class.
*
* @author pcdinh
* @since July 17, 2009
*/
interface TinyTestAssertion
{
public function getMessage();
public function valid();
public function getType();
public function getExpected();
public function getFeedback();
}
/**
* This class asserts that a condition is true.
*
* @author pcdinh
* @since July 17, 2009
*/
class TinyBooleanTrueAssertion implements TinyTestAssertion
{
/**
* Provided value.
*
* @var mixed
*/
public $value;
/**
* User message.
*
* @var string
*/
public $message;
/**
* Constructs an object of <code>TinyBooleanTrueAssertion</code>.
*
* @param mixed $value
* @param string $message
*/
public function __construct($value, $message = '')
{
$this->value = $value;
$this->message = $message;
}
/**
* (non-PHPdoc)
* @see trunk/migration/TinyTestAssertion#getMessage()
*/
public function getMessage()
{
return $this->message;
}
/**
* (non-PHPdoc)
* @see trunk/migration/TinyTestAssertion#valid()
*/
public function valid()
{
return true === $this->value;
}
/**
* (non-PHPdoc)
* @see trunk/migration/TinyTestAssertion#getType()
*/
public function getType()
{
return gettype($this->value);
}
/**
* (non-PHPdoc)
* @see trunk/migration/TinyTestAssertion#getExpected()
*/
public function getExpected()
{
return 'boolean(true)';
}
/**
* (non-PHPdoc)
* @see trunk/migration/TinyTestAssertion#getFeedback()
*/
public function getFeedback()
{
if (true === $this->valid())
{
return 'Expected boolean(true) is met.';
}
$message = $this->getMessage();
if (false === empty($message))
{
return $message;
}
$type = $this->getType();
if ('boolean' === $type)
{
$type = $type.'(false)';
}
return 'Type of the provided value is a "'.$type.'" but a '.$this->getExpected().' is expected.';
}
}
?>
<?php
include_once '../TinyTestCase.php';
class FileNameFormatDetectorTest extends TinyTestCase
{
/**
* File name detector.
*
* @var FileNameFormatDetector
*/
public $d;
/**
* Constructs an object of <code>FileNameFormatDetector</code>.
*/
public function __construct()
{
include_once '../FileNameFormatDetector.php';
$this->d = new FileNameFormatDetector();
}
public function test1()
{
$f1 = $this->d->parse('AUS000000037.2006.A.00.L.00.00.PDF');
$this->assertTrue($f1->format !== FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format === FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format === FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format !== FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format === FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format === FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format !== FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format === FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format === FileNameFormat::NEW_FORMAT_2);
$this->assertTrue($f1->format === FileNameFormat::NEW_FORMAT_2);
}
public function test2()
{
}
}
$test = new FileNameFormatDetectorTest();
$test->run();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment