Skip to content

Instantly share code, notes, and snippets.

@emersonbroga
Created April 16, 2013 19:17
Show Gist options
  • Save emersonbroga/5398753 to your computer and use it in GitHub Desktop.
Save emersonbroga/5398753 to your computer and use it in GitHub Desktop.
<?php
class MyClass
{
public function strToDate( $str )
{
return new \DateTime($str);
}
}
class MyClassTest extends PHPUnit_Framework_TestCase
{
public function testStrToDate()
{
$myClass = new \MyClass();
$actual = '2013-04-16';
$expected = new \DateTime('2013-04-16');
//Pass
$this->assertEquals($expected, $myClass->strToDate($actual));
//Fails
$this->assertSame($expected, $myClass->strToDate($actual));
}
}
//Result
//PHPUnit 3.7.15 by Sebastian Bergmann.
//
//F
//
//Time: 0 seconds, Memory: 3.25Mb
//
//There was 1 failure:
//
//1) MyClassTest::testStrToDate
//Failed asserting that two variables reference the same object.
//
///var/www/teste.php:25
//
//FAILURES!
//Tests: 1, Assertions: 2, Failures: 1.
@arthuralmeidap
Copy link

No manual do PHPUnit tem a seguinte citação ao usar assertSame com objetos:
"Reports an error identified by $message if the two variables $expected and $actual do not reference the same object."
Então ele não vai funcionar no teu caso. Caso você queira saber se é uma objeto DateTime é melhor tu usar o assertInstanceOf () ou caso seja para verificar se a data é a mesma converta as duas para um timestamp e compare com o assertEquals ( eu faria isso pelo menos )

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