This may be a really dumb question, but why isn't there a PHPUnit_Assert::assertNotEquals() method? Equality is a complex enough operation, I'd hate to have to do this in my PHPUnit tests:
function equals($a_Actual, $a_Expected) { $equivalent = false; // All the inverse logic of PHPUnit_Assert::assertEquals() ... return $equivalent; } ... function testFoo() { $this->assertFalse(equals($actual, $expected), ...); } or worse: // What if this passes? I'll never know unless I'm looking for it! $this->assertEquals($actual, $expected, 'This test should fail.'); Attached is a patch to add assertNotEquals() to PHPUnit_Assert. Please consider including it (or something like it) in future releases. (Pretty please?) Here's the legalese for the patch which basically says you can do with it whatever you want, but you can't sue me if it doesn't work or breaks something (stolen from the MIT license): Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -- Please do not sell or give away my information.
diff -Naur PHPUnit.old/Assert.php PHPUnit/Assert.php --- PHPUnit.old/Assert.php Wed Oct 16 11:17:37 2002 +++ PHPUnit/Assert.php Sat Oct 19 16:02:35 2002 @@ -33,61 +33,45 @@ * @access public */ function assertEquals($expected, $actual, $message = '', $delta = 0) { - if ((is_array($actual) && is_array($expected)) || - (is_object($actual) && is_object($expected))) { - if (is_array($actual) && is_array($expected)) { - ksort($actual); - ksort($expected); - } - $actual = serialize($actual); - $expected = serialize($expected); + $default = null; + $equiv = $this->_checkEquivalence($expected, $actual, $default, $delta); - if (empty($message)) { - $message = sprintf( - 'expected %s, actual %s', - - $expected, - $actual - ); - } + if (empty($message)) + { + $message = &$default; + } - if (serialize($actual) != serialize($expected)) { - return $this->fail($message); - } + if ($equiv) + { + return $this->pass(); } - elseif (is_numeric($actual) && is_numeric($expected)) { - if (empty($message)) { - $_delta = ($delta != 0) ? ('+/- ' . $delta) : ''; + return $this->fail($message); + } - $message = sprintf( - 'expected %s%s, actual %s', + /** + * Asserts that two variables are not equal. + * + * @param mixed + * @param mixed + * @param string + * @param mixed + * @access public + */ + function assertNotEquals($expected, $actual, $message = '', $delta = 0) { - $expected, - $_delta, - $actual - ); - } + $default = null; + $equiv = $this->_checkEquivalence($expected, $actual, $default, $delta); - if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) { - return $this->fail($message); - } + if (empty($message)) + { + $message = &$default; } - else { - if (empty($message)) { - $message = sprintf( - 'expected %s, actual %s', - - $expected, - $actual - ); - } - - if ($actual != $expected) { - return $this->fail($message); - } + if ($equiv) + { + return $this->fail($message); } return $this->pass(); @@ -253,5 +237,57 @@ * @abstract */ function pass() { /* abstract */ } + + /** + * Checks to see if two variables are equivalent. + * + * @param mixed + * @param mixed + * @param mixed + * @return bool + * @access private + */ + function _checkEquivalence($expected, $actual, &$message, $delta) { + if ((is_array($actual) && is_array($expected)) || + (is_object($actual) && is_object($expected))) { + if (is_array($actual) && is_array($expected)) { + ksort($actual); + ksort($expected); + } + + $actual = serialize($actual); + $expected = serialize($expected); + + $message = sprintf('expected %s, actual %s', + $expected, $actual); + + // Why do we have to re-serialize these? + if (serialize($actual) != serialize($expected)) { + return false; + } + } + + elseif (is_numeric($actual) && is_numeric($expected)) { + $_delta = ($delta != 0) ? ('+/- ' . $delta) : ''; + $message = sprintf('expected %s%s, actual %s', + $expected, $_delta, $actual); + + if (!($actual >= ($expected - $delta) + && $actual <= ($expected + $delta))) { + return false; + } + } + + else { + $message = sprintf('expected %s, actual %s', + $expected, $actual); + + if ($actual != $expected) { + return false; + } + } + + return true; + } } ?>
signature.asc
Description: This is a digitally signed message part