<br />
<b>Warning</b>:  The magic method SFML_Singleton::__wakeup() must have public visibility in <b>/home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php</b> on line <b>72</b><br />
{"id":305,"date":"2021-02-10T14:26:33","date_gmt":"2021-02-10T21:26:33","guid":{"rendered":"https:\/\/www.munderwood.ca\/?p=305"},"modified":"2021-02-10T14:26:34","modified_gmt":"2021-02-10T21:26:34","slug":"json-responses-as-php-arrays-in-laravel-8-tests","status":"publish","type":"post","link":"https:\/\/www.munderwood.ca\/index.php\/2021\/02\/10\/json-responses-as-php-arrays-in-laravel-8-tests\/","title":{"rendered":"JSON responses as PHP arrays in Laravel 8 tests"},"content":{"rendered":"\n<p>I&#8217;m in the process of upgrading an API code base that has been in production since Laravel 5, and continues to evolve. The bulk of the update to version 8 went smoothly, but one breaking change that I couldn&#8217;t find documented anywhere held me up for some time this afternoon.<\/p>\n\n\n\n<p>A great many of the tests use a pattern of getting a response from an endpoint, running <code>$data\u00a0=\u00a0$response->decodeResponseJson()<\/code>, and then making assertions against the resulting PHP array in <code>$data<\/code>. Unfortunately, in Laravel 8 the return type is now <code>\\Illuminate\\Testing\\AssertableJsonString<\/code>, and I kept running into errors such as:<\/p>\n\n\n\n<p><code>Argument #2 of PHPUnit\\Framework\\Assert::assertArrayHasKey() must be an array or ArrayAccess<\/code><\/p>\n\n\n\n<p>I made various attempts to convert the output of <code>decodeResponseJson<\/code> into an array (and being confused as to why it <em>didn&#8217;t<\/em> work, since <code>AssertableJsonString\u00a0implements\u00a0ArrayAccess<\/code>), but in the end the solution was simple.<\/p>\n\n\n\n<p>Instead of refactoring every existing test to use Laravel&#8217;s new JSON-based assertions, I merely had to change each call to <code>decodeResponseJson<\/code> to just <code>json<\/code>. That is, switching to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$data = $response->json();<\/code><\/pre>\n\n\n\n<p>was all it took.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m in the process of upgrading an API code base that has been in production since Laravel 5, and continues to evolve. The bulk of the update to version 8 went smoothly, but one breaking change that I couldn&#8217;t find documented anywhere held me up for some time this afternoon. A great many of the &#8230; <span class=\"more\"><a class=\"more-link\" href=\"https:\/\/www.munderwood.ca\/index.php\/2021\/02\/10\/json-responses-as-php-arrays-in-laravel-8-tests\/\">[Read more&#8230;]<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/305"}],"collection":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":10,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}