-
-
Save bm2ilabs/5211975 to your computer and use it in GitHub Desktop.
assert.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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