Skip to content

Instantly share code, notes, and snippets.

@proofek
Created February 9, 2012 10:55
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 proofek/1779262 to your computer and use it in GitHub Desktop.
Save proofek/1779262 to your computer and use it in GitHub Desktop.
phpunit assertion gotcha
<?php
class myClass
{
public function __toString()
{
return "mememe";
}
}
class TestCase extends PHPUnit_Framework_TestCase
{
public function testAssertStringWithObject()
{
$class = new myClass();
$this->assertEquals('mememe', $class);
}
public function testAssertStringWithSerialisedObject()
{
$class = new myClass();
$this->assertEquals('mememe', "$class");
}
}
@proofek
Copy link
Author

proofek commented Feb 9, 2012

$ 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 <string:mememe>.

/private/tmp/Testing/testCaseTest.php:16

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

@proofek
Copy link
Author

proofek commented Feb 9, 2012

$ phpunit testCaseTest.php 
PHPUnit 3.6.10 by Sebastian Bergmann.

..

Time: 1 second, Memory: 5.50Mb

OK (2 tests, 2 assertions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment