Skip to content

Instantly share code, notes, and snippets.

@edvakf
Last active December 25, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvakf/6971606 to your computer and use it in GitHub Desktop.
Save edvakf/6971606 to your computer and use it in GitHub Desktop.
don't use phpunit's assertEquals to compare closures.
<?php
class ClosureCompraisonTest extends PHPUnit_Framework_TestCase
{
// passes. WTF???
public function test_assertEquals()
{
$f1 = function () { return 1; };
$f2 = function () { return 2; };
$this->assertEquals($f1, $f2);
}
// fails, don't kid me
public function test_assertNotEquals()
{
$f1 = function () { return 1; };
$f2 = function () { return 2; };
$this->assertNotEquals($f1, $f2);
}
// fails, of course
public function test_assertSame()
{
$f1 = function () { return 1; };
$f2 = function () { return 2; };
$this->assertSame($f1, $f2);
}
// passes
public function test_assertNotSame()
{
$f1 = function () { return 1; };
$f2 = function () { return 2; };
$this->assertNotSame($f1, $f2);
}
// passes
public function test_doubleEqual()
{
$f1 = function () { return 1; };
$f2 = function () { return 2; };
$this->assertFalse($f1 == $f2);
}
// passes
public function test_tripleEqual()
{
$f1 = function () { return 1; };
$f2 = function () { return 2; };
$this->assertFalse($f1 === $f2);
}
}
% ./vendor/phpunit/phpunit/phpunit.php ClosureComparisonTest.php
PHPUnit 3.7.27 by Sebastian Bergmann.
Configuration read from /Users/atsushi/prog/phpmethodreplacer/phpunit.xml.dist
.FF...
Time: 63 ms, Memory: 2.75Mb
There were 2 failures:
1) ClosureCompraisonTest::test_assertNotEquals
Failed asserting that Closure Object (
0 => Closure Object (*RECURSION*)
) is not equal to Closure Object (
0 => Closure Object (*RECURSION*)
).
/Users/atsushi/prog/phpmethodreplacer/ClosureComparisonTest.php:18
2) ClosureCompraisonTest::test_assertSame
Failed asserting that two variables reference the same object.
/Users/atsushi/prog/phpmethodreplacer/ClosureComparisonTest.php:26
FAILURES!
Tests: 6, Assertions: 6, Failures: 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment