1
0
Fork 0

- Added PHPUnit TestCase for custom assertions.

Dieser Commit ist enthalten in:
D4rk4ng3l 2012-09-12 20:18:55 +00:00
Ursprung a2ac0b33e4
Commit 7df515daf7
2 geänderte Dateien mit 133 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,89 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage PHPUnit_Constraint
* @version SVN: $Rev$
* @author $Author$
*/
/**
* PHPUnit constraint to check an array for the given structure.
*
* @package MySQLDumper
* @subpackage Archive
*/
class Msd_PHPUnit_Constraint_ArrayHasStructure extends PHPUnit_Framework_Constraint
{
/**
* Expected array structure.
*
* @var array
*/
private $_structure;
/**
* Class constructor, sets the expected array structure.
*
* @param array $structure Array containing the expected array structure.
*
* @return Msd_PHPUnit_Constraint_ArrayHasStructure
*/
public function __construct($structure)
{
$this->_structure = $structure;
}
/**
* Checks an array against the expected structure.
*
* @param mixed $other Array to check.
* @param null $structure Expected structure (used fpr recursion).
*
* @return bool|int
*/
protected function matches($other, $structure = null)
{
if ($structure === null) {
$structure = $this->_structure;
}
$isValid = true;
foreach ($structure as $key => $value) {
if (is_array($value)) {
$isValid = $isValid && isset($other[$key]);
if (isset($other[$key])) {
$isValid = $isValid & $this->matches($other[$key], $value);
}
continue;
}
$isValid = $isValid && isset($other[$value]);
}
return $isValid;
}
/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString()
{
return 'has the structure' . PHPUnit_Util_Type::export($this->_structure);
}
/**
* Returns the failure message.
*
* @param mixed $other Checked array.
*
* @return string
*/
protected function failureDescription($other)
{
return 'an array ' . $this->toString();
}
}

Datei anzeigen

@ -0,0 +1,44 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage PHPUnit
* @version SVN: $Rev$
* @author $Author$
*/
/**
* Abstract class to extend PHPUnit tests with custom assertions.
*
* @package MySQLDumper
* @subpackage PHPUnit
*/
abstract class Msd_PHPUnit_TestCase extends PHPUnit_Framework_TestCase
{
/**
* Assertion to test that an array has at least a structure.
*
* @param array $structure Excepted structure of the array.
* @param array $array Array to test.
* @param string $message Additional information about the test.
*
* @return void
*/
public static function assertArrayHasStructure($structure, $array, $message = '')
{
self::assertThat($array, self::arrayHasStructure($structure, $message));
}
/**
* Returns the constraint for the assertion.
*
* @param array $structure Excepted structure of the array.
*
* @return Msd_PHPUnit_Constraint_ArrayHasStructure
*/
public static function arrayHasStructure($structure)
{
return new Msd_PHPUnit_Constraint_ArrayHasStructure($structure);
}
}