Skip to content

Instantly share code, notes, and snippets.

@bm2ilabs
Forked from joemay/assert.php
Created March 21, 2013 10:08
Show Gist options
  • Save bm2ilabs/5211975 to your computer and use it in GitHub Desktop.
Save bm2ilabs/5211975 to your computer and use it in GitHub Desktop.
assert.php
<?php
/**
* Simple wrapper around the
* PHPUnit assertion library.
*
* Assert::equals(21, $age);
* Assert::greaterThan(20, $age);
* Assert::has('Joe', ['John', 'Joe']);
*/
class Assert extends PHPUnit_Framework_Assert {
/**
* Registered assertion aliases
*
* @var array
*/
protected $aliases = array(
'eq' => 'equals',
'instance' => 'instanceOf',
'has' => 'contains'
);
/**
* Intialization
*
* @param string $methodName
* @param array $args
*/
public function __construct($methodName, $args)
{
$methodName = $this->getMethod($methodName);
// If the assertion exists, run it
// Otherwise, throw an exception.
if (method_exists(__CLASS__, $methodName))
{
return call_user_func_array(array(__CLASS__, $methodName), $args);
}
throw new BadMethodCallException;
}
/**
* Handle dynamic, static calls to the object.
*
* @param string $methodName
* @param array $args
* @return Should
*/
public static function __callStatic($methodName, array $args)
{
return new static($methodName, $args);
}
/**
* Determine the correct assertion name
*
* @param string $methodName
*/
protected function getMethod($methodName)
{
// Do we have an alias registered for this method name?
// If so, then use that.
if (array_key_exists($methodName, $this->aliases))
{
$methodName = $this->aliases[$methodName];
}
return 'assert' . ucwords($methodName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment