Issue
I'm writing a unit test for a method that uses the CakePHP TimeHelper
public function testData()
{
$timestamp = (string)Time::now()->i18nFormat('YYYY-MM-dd HH:mm:ss');
// dump($timestamp);
// "2015-10-14 23:25:33"
...
# data request should update the last_checkin field
$screen = TableRegistry::get('Screens')->get(1);
$this->assertEquals($timestamp, $screen->last_checkin);
}
I'm really confused why it works in the controller, and the return from dump()
is correct. But the unit test fails with the following message:
1) App\Test\TestCase\Controller\ScreensControllerTest::testData
Failed asserting that Cake\I18n\Time Object &000000001a943b50000000016cb13674 (
'date' => '2015-10-14 23:25:33.000000'
'timezone_type' => 3
'timezone' => 'UTC'
) matches expected '2015-10-14 23:25:33'.
/mysite/tests/TestCase/Controller/ScreensControllerTest.php:103
Why doesassertEquals()
think it's an object when dump()
(and save()
in my controller) thinks it's a string?
Is this a TimeHelper bug? A phpunit bug? Or am I missing something?
Solution
Is this a TimeHelper bug? A phpunit bug? Or am I missing something?
Start with reading about __toString(). Reading about the other magic methods won't hurt either by the way! There is fancy stuff you can do with them. Then check the time class:
public function __toString()
{
return $this->i18nFormat();
}
assertEquals() sees it as what it really is - an object.
Assuming Table::save() here for your save() does a little more complexity in the background, however at the end of the process it ends up as datetime string in the DB if the field is of type datetime. This is because Cake is aware of the field type in the schema and casts the object to the right format.
I guess you mean Debugger::dump() (be more specific!) - casts it to string for some reason I guess, honestly I'm to lazy to look the exact line and reason up right now. But I think you already understood what's going on.
Answered By - floriank
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.