{"id":492,"date":"2020-03-29T21:45:51","date_gmt":"2020-03-29T21:45:51","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=492"},"modified":"2020-03-29T21:48:10","modified_gmt":"2020-03-29T21:48:10","slug":"tdd-date-assertions-laravel-7-php-7-carbon-2-changes","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/tdd-date-assertions-laravel-7-php-7-carbon-2-changes.html","title":{"rendered":"TDD: Date Assertions &#8211; Laravel 7, PHP 7.* &#8211; Carbon 2 &#8211; Changes"},"content":{"rendered":"\n<p>If you follow a TDD approach to develop your software, and also a Laravel user, working with a JSON API, you might have experienced some date assertion issues while asserting Json with Laravel 7. Previously, when Laravel was using Carbon 1 for date management, it would not return the whole Carbon object for assertJson. Which is why, the following would work in a sample PHPUnit Test:<\/p>\n\n\n\n<p><code>$contact = factory(Contact::class)-&gt;create();<br>$response = $this-&gt;get('\/api\/contacts' . $contact-&gt;id);<br><br>$response-&gt;assertJson([<br>'name' =&gt; $contact-&gt;name,<br>'email' =&gt; $contact-&gt;email,<br>'meeting_date' =&gt; $contact-&gt;meeting_date,<br>'company' =&gt; $contact-&gt;company,<br>]);<\/code><\/p>\n\n\n\n<p>But as of now, Carbon 2, returns the whole Carbon object for $contact-&gt;meeting_date here, the assertion will fair, because the $response, didn&#8217;t get a Carbon object, instead a Json string here. <\/p>\n\n\n\n<p>If you go through the Carbon documentation <a href=\"https:\/\/carbon.nesbot.com\/docs\/\">here<\/a>, you can see the following under the section &#8216;Migrate to Carbon 2&#8217;:<\/p>\n\n\n\n<p><code>$date-&gt;jsonSerialize()&nbsp;and&nbsp;json_encode($date)&nbsp;no longer returns arrays but simple strings:&nbsp;\"2017-06-27T13:14:15.000000Z\". This allows to create dates from it easier in JavaScript.&nbsp; <\/code><\/p>\n\n\n\n<p>This is basically what you need to use if you are on Laravel 7 with Carbon 2. You need to serialize the data as Laravel is not doing it for you automatically here to fix this up. Just change the &#8216;meeting_date&#8217; to the following:<\/p>\n\n\n\n<p><code>'meeting_date' =&gt; $contact-&gt;meeting_date-&gt;jsonSerialize(),<\/code><\/p>\n\n\n\n<p>and this should let your assertion pass. <\/p>\n\n\n\n<p>Remember, you might not need to explicitly do this in your controller return until you are following a TDD based development where an assertion to pass is important to continue the process. Laravel resource would implicitly serialize your data before returning them from controller.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you follow a TDD approach to develop your software, and also a Laravel user, working with a JSON API, you might have experienced some date assertion issues while asserting Json with Laravel 7. Previously, when Laravel was using Carbon 1 for date management, it would not return the whole Carbon object for assertJson. Which &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/tdd-date-assertions-laravel-7-php-7-carbon-2-changes.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;TDD: Date Assertions &#8211; Laravel 7, PHP 7.* &#8211; Carbon 2 &#8211; Changes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[318,272,273],"tags":[320,325,324,326,319,321,322,323],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/492"}],"collection":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":4,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":496,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions\/496"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}