Skip to content

Commit

Permalink
Modified constraint subframework to use the new comparator classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek committed Jun 14, 2011
1 parent d38ea8f commit 7b37221
Show file tree
Hide file tree
Showing 54 changed files with 1,369 additions and 2,129 deletions.
1 change: 1 addition & 0 deletions PHPUnit/Autoload.php
Expand Up @@ -94,6 +94,7 @@ function phpunit_autoload($class = NULL) {
'phpunit_framework_constraint_attribute' => '/Framework/Constraint/Attribute.php',
'phpunit_framework_constraint_classhasattribute' => '/Framework/Constraint/ClassHasAttribute.php',
'phpunit_framework_constraint_classhasstaticattribute' => '/Framework/Constraint/ClassHasStaticAttribute.php',
'phpunit_framework_constraint_composite' => '/Framework/Constraint/Composite.php',
'phpunit_framework_constraint_count' => '/Framework/Constraint/Count.php',
'phpunit_framework_constraint_fileexists' => '/Framework/Constraint/FileExists.php',
'phpunit_framework_constraint_greaterthan' => '/Framework/Constraint/GreaterThan.php',
Expand Down
12 changes: 9 additions & 3 deletions PHPUnit/Extensions/PhptTestCase.php
Expand Up @@ -55,6 +55,7 @@
* @package PHPUnit
* @subpackage Extensions_PhptTestCase
* @author Sebastian Bergmann <sebastian@phpunit.de>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
Expand Down Expand Up @@ -202,11 +203,16 @@ public function run(PHPUnit_Framework_TestResult $result = NULL, array $options
}

else if ($buffer != 'PASSED') {
$expContent = file_get_contents($expFile);
$outContent = file_get_contents($outFile);

$result->addFailure(
$this,
PHPUnit_Framework_ComparisonFailure::diffEqual(
file_get_contents($expFile),
file_get_contents($outFile)
new PHPUnit_Framework_ComparisonFailure(
$expContent,
$outContent,
$expContent,
$outContent
),
$time
);
Expand Down
4 changes: 1 addition & 3 deletions PHPUnit/Framework/Assert.php
Expand Up @@ -2059,9 +2059,7 @@ public static function assertThat($value, PHPUnit_Framework_Constraint $constrai
{
self::$count += count($constraint);

if (!$constraint->evaluate($value)) {
$constraint->fail($value, $message);
}
$constraint->evaluate($value, $message);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions PHPUnit/Framework/Comparator.php
Expand Up @@ -36,7 +36,7 @@
*
* @package PHPUnit
* @subpackage Framework
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.phpunit.de/
Expand All @@ -48,7 +48,7 @@
*
* @package PHPUnit
* @subpackage Framework
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
Expand Down
133 changes: 78 additions & 55 deletions PHPUnit/Framework/Comparator/Array.php
Expand Up @@ -36,7 +36,7 @@
*
* @package PHPUnit
* @subpackage Framework
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.phpunit.de/
Expand All @@ -48,7 +48,7 @@
*
* @package PHPUnit
* @subpackage Framework_Comparator
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
Expand Down Expand Up @@ -88,87 +88,110 @@ public function accepts($a, $b)
*/
public function assertEquals($a, $b, $delta = 0, $canonicalize = FALSE, $ignoreCase = FALSE, array &$processed = array())
{
$remaining = $b;

if ($canonicalize) {
sort($a);
sort($b);
}

$remaining = $b;
$aString = $bString = "Array\n(\n";
$equal = TRUE;

foreach ($a as $key => $value) {
if (!isset($b[$key])) {
throw new PHPUnit_Framework_ComparisonFailure($a/*->dumpExcerpt($key, $comparator)*/, $b);
unset($remaining[$key]);

if (!array_key_exists($key, $b)) {
$aString .= sprintf(
" [%s] => %s\n",

print_r($key, true),
$this->shortenedExport($value)
);
$equal = FALSE;
continue;
}

try {
self::getInstance($value, $b[$key])->assertEquals($value, $b[$key], $delta, $canonicalize, $ignoreCase, $processed);
$aString .= sprintf(
" [%s] => %s\n",

print_r($key, true),
$this->shortenedExport($value)
);
$bString .= sprintf(
" [%s] => %s\n",

print_r($key, true),
$this->shortenedExport($b[$key])
);
}

catch (PHPUnit_Framework_ComparisonFailure $e) {
throw new PHPUnit_Framework_ComparisonFailure($a/*->dumpExcerpt($key, $e->getExpected())*/, $b/*->dumpExcerpt($key, $e->getActual())*/);
$aString .= sprintf(
" [%s] => %s\n",

print_r($key, true),
$e->getExpectedAsString()
? $this->indent($e->getExpectedAsString())
: print_r($e->getExpected(), true)
);
$bString .= sprintf(
" [%s] => %s\n",

print_r($key, true),
$e->getActualAsString()
? $this->indent($e->getActualAsString())
: print_r($e->getActual(), true)
);
$equal = FALSE;
}

unset($remaining[$key]);
}

foreach ($remaining as $key => $value) {
throw new PHPUnit_Framework_ComparisonFailure($a, $b/*->dumpExcerpt($key, $value)*/);
}
}

protected function dumpAll()
{
$result = $this->getType().' (';
$bString .= sprintf(
" [%s] => %s\n",

if (!empty($this->value)) {
$result .= "\n";

foreach ($this->value as $k => $v) {
$result .= sprintf(" %s => %s,\n", var_export($k, true), $this->indent($v));
}
print_r($key, true),
$this->shortenedExport($value)
);
$equal = FALSE;
}

$result .= ')';

return $result;
$aString .= ')';
$bString .= ')';

if (!$equal) {
throw new PHPUnit_Framework_ComparisonFailure(
$a,
$b,
$aString,
$bString,
FALSE,
'Failed asserting that two arrays are equal.'
);
}
}

protected function dumpExcerpt($key = null, $value = null)
protected function indent($lines)
{
$result = $this->getType().' (';

if (!empty($this->value)) {
$truncated = false;
$result .= "\n";

foreach ($this->value as $k => $v) {
if ((is_null($key) || $key !== $k) && !$truncated) {
$result .= " ...\n";
$truncated = true;
}
else if ($k === $key) {
$value = null === $value
? $v
: ($value instanceof PHPUnit_Framework_SelfDescribing ? $value->toString() : $value);
$result .= sprintf(" %s => %s,\n", var_export($k, true), $this->indent($value));
$truncated = false;
}
}
}

$result .= ')';

return $result;
return trim(str_replace("\n", "\n ", $lines));
}

protected function indent($lines)
protected function shortenedExport($value)
{
$lines = explode("\n", $lines);
if (is_array($value)) {
if (count($value) > 0) {
return 'Array (...)';
}

return 'Array';
}

foreach ($lines as $key => $line) {
$lines[$key] = ' '.$line;
if (is_object($value)) {
return get_class($value) . ' Object (...)';
}

return trim(implode("\n", $lines));
return print_r($value, true);
}
}
28 changes: 25 additions & 3 deletions PHPUnit/Framework/Comparator/DOMDocument.php
Expand Up @@ -36,7 +36,7 @@
*
* @package PHPUnit
* @subpackage Framework
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.phpunit.de/
Expand All @@ -48,7 +48,7 @@
*
* @package PHPUnit
* @subpackage Framework_Comparator
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
Expand Down Expand Up @@ -89,7 +89,29 @@ public function accepts($a, $b)
public function assertEquals($a, $b, $delta = 0, $canonicalize = FALSE, $ignoreCase = FALSE)
{
if ($a->C14N() !== $b->C14N()) {
throw new PHPUnit_Framework_ComparisonFailure($a, $b);
throw new PHPUnit_Framework_ComparisonFailure(
$a,
$b,
$this->domToText($a),
$this->domToText($b),
FALSE,
'Failed asserting that two DOM documents are equal.'
);
}
}

/**
* Returns the normalized, whitespace-cleaned, and indented textual
* representation of a DOMDocument.
*
* @param DOMDocument $document
* @return string
*/
protected function domToText(DOMDocument $document)
{
$document->formatOutput = TRUE;
$document->normalizeDocument();

return $document->saveXML();
}
}
18 changes: 15 additions & 3 deletions PHPUnit/Framework/Comparator/Double.php
Expand Up @@ -36,7 +36,7 @@
*
* @package PHPUnit
* @subpackage Framework
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.phpunit.de/
Expand All @@ -48,7 +48,7 @@
*
* @package PHPUnit
* @subpackage Framework_Comparator
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
Expand Down Expand Up @@ -104,7 +104,19 @@ public function assertEquals($a, $b, $delta = 0, $canonicalize = FALSE, $ignoreC
}

if (abs($b - $a) >= $delta) {
throw new PHPUnit_Framework_ComparisonFailure($a, $b);
throw new PHPUnit_Framework_ComparisonFailure(
$a,
$b,
'',
'',
FALSE,
sprintf(
'Failed asserting that %s matches expected %s.',

PHPUnit_Util_Type::toString($b),
PHPUnit_Util_Type::toString($a)
)
);
}
}
}
4 changes: 2 additions & 2 deletions PHPUnit/Framework/Comparator/Exception.php
Expand Up @@ -36,7 +36,7 @@
*
* @package PHPUnit
* @subpackage Framework
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.phpunit.de/
Expand All @@ -48,7 +48,7 @@
*
* @package PHPUnit
* @subpackage Framework_Comparator
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@2bepublished.at>
* @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
Expand Down

0 comments on commit 7b37221

Please sign in to comment.