February 9, 2012

PHPUnit assertion gotcha

A recent discovery. If you found yourself upgrading from PHPUnit 3.5.x to PHPUnit 3.6.x be aware of a subtle change. assertEquals() and I guess all related assertions will behave a bit differently after the upgrade. When you assert 2 values and one of them is an object with defined __toString() function PHPUnit 3.5.x will fail the test, while PHPUnit 3.6 will happily pass it (by I am guessing converting the object to string). See an example on gisthub for more details:

$ phpunit testCaseTest.php
PHPUnit 3.5.15 by Sebastian Bergmann.
F.
Time: 1 second, Memory: 4.50Mb
There was 1 failure:
1) TestCase::testAssertStringWithObject
Failed asserting that
myClass Object
(
)
matches expected .
/private/tmp/Testing/testCaseTest.php:16
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
$ phpunit testCaseTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
..
Time: 1 second, Memory: 5.50Mb
OK (2 tests, 2 assertions)

Leave a Reply

Your email address will not be published. Required fields are marked *