1

I'm working on a Laravel applications and testing whether my POST request has actually added the record to the database using

$this->assertDatabaseHas('profiles', [
  'physical_address'          => [
   'city'          => $physical_address['city'],
   'line_1'        => $physical_address['line_1'],
   'line_2'        => $physical_address['line_2'],
   'country'       => $physical_address['country'],
   'province'      => $physical_address['province'],
   'postal_code'   => $physical_address['postal_code'],
  ],
]);

I'm making a POST request to the endpoint and passing in.

$physical_address = [
    'line_1'            => $this->faker->secondaryAddress,
    'line_2'            => $this->faker->streetAddress,
    'city'              => $this->faker->city,
    'province'          => $this->faker->state,
    'postal_code'       => $this->faker->postcode,
    'country'           => $this->faker->country,
];

The physical_address column is a JSON column and is added to the $casts array on the model.

The test, however keeps failing with the error message:

1) Tests\Feature\ProfileTest::testCreateProfile
Failed asserting that a row in the table [profiles] matches the attributes {
    "physical_address": {
        "city": "Nicoview",
        "line_1": "Apt. 664",
        "line_2": "136 Mueller Mountain Suite 956",
        "country": "Malta",
        "province": "Arkansas",
        "postal_code": "19176-5371"
    }
}.

When I dump the record that was added to the database, the column structure looks as follows:

"physical_address" => array:6 [
    "city" => "Nicoview"
    "line_1" => "Apt. 664"
    "line_2" => "136 Mueller Mountain Suite 956"
    "country" => "Malta"
    "province" => "Arkansas"
    "postal_code" => "19176-5371"
  ]

I've made sure the keys are in the right order but I don't know what I should try anymore or if I'm missing something obvious.

2
  • 1
    It may just be the display, but where it has "physical_address": { the open brace may mean it's expecting an object rather than an array. Commented Jun 1, 2020 at 14:10
  • but in the code I'm expecting an array. Commented Jun 1, 2020 at 14:19

1 Answer 1

2

AssertDatabaseHas() underlying is just where queries, it will end up trying to parse your assertation as it was another nested array, most likely into city = Nicoview and line_1 = Apt. 664 ....

In Laravel Queries you can access JSON columns with -> as the accessor separator. So physical_address->city will represent the city representation. So i believe you will achieve the results you want by doing this.

$this->assertDatabaseHas('profiles', [
    'physical_address->city' => $physical_address['city'],
    'physical_address->line_1' => $physical_address['line_1'],
    'physical_address->line_2' => $physical_address['line_2'],
    'physical_address->country' => $physical_address['country'],
    'physical_address->province' => $physical_address['province'],
    'physical_address->postal_code' => $physical_address['postal_code'],
]);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not running mysql as my local dev database, didn't test it but looked through the code and i think this will work for you :) please get back if something is still a problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.