I’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’t find documented anywhere held me up for some time this afternoon.<\/p>\n\n\n\n
A great many of the tests use a pattern of getting a response from an endpoint, running I made various attempts to convert the output of Instead of refactoring every existing test to use Laravel’s new JSON-based assertions, I merely had to change each call to was all it took.<\/p>\n","protected":false},"excerpt":{"rendered":" I’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’t find documented anywhere held me up for some time this afternoon. A great many of the … [Read more…]<\/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}]}}$data\u00a0=\u00a0$response->decodeResponseJson()<\/code>, and then making assertions against the resulting PHP array in
$data<\/code>. Unfortunately, in Laravel 8 the return type is now
\\Illuminate\\Testing\\AssertableJsonString<\/code>, and I kept running into errors such as:<\/p>\n\n\n\n
Argument #2 of PHPUnit\\Framework\\Assert::assertArrayHasKey() must be an array or ArrayAccess<\/code><\/p>\n\n\n\n
decodeResponseJson<\/code> into an array (and being confused as to why it didn’t<\/em> work, since
AssertableJsonString\u00a0implements\u00a0ArrayAccess<\/code>), but in the end the solution was simple.<\/p>\n\n\n\n
decodeResponseJson<\/code> to just
json<\/code>. That is, switching to<\/p>\n\n\n\n
$data = $response->json();<\/code><\/pre>\n\n\n\n