Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

This comment has been minimized.

Show comment Hide comment
@proofek

proofek 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.
Owner

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

This comment has been minimized.

Show comment Hide comment
@proofek

proofek Feb 9, 2012

$ phpunit testCaseTest.php 
PHPUnit 3.6.10 by Sebastian Bergmann.

..

Time: 1 second, Memory: 5.50Mb

OK (2 tests, 2 assertions)
Owner

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