2

I have an array output in laravel, the structure looks like follows:

array:1 [
  0 => {#35
    +"id": 152
    +"user_id": 123
    +"post_id": 456
    +"content": "impedit"
    +"created_at": "04-09-2016 01:24"
    +"updated_at": "2016-09-04 01:24:51"
  }
]

How to test the array structure contains keys iduser_id ...?

I tried assertArrayHasKey('user_id', $array),but it shows failed. Thanks.

4
  • It fails because the keys in your array are 0, 1, 2, .... And values in the array are objects. Commented Sep 4, 2016 at 1:59
  • @linuxartisan Thanks for the reply. If I got these structure, is it possible to testing by PHPUnit ? Commented Sep 4, 2016 at 2:13
  • Try using property_exists function. php.net/manual/en/function.property-exists.php Commented Sep 4, 2016 at 2:21
  • @linuxartisan Thanks a lot. I finally convert the object to array and it works well. Thanks again ! Commented Sep 4, 2016 at 2:26

1 Answer 1

4

Thanks for the @linuxartisan. Finally I convert the value object to array for testing.

$this->assertArrayHasKey('user_id', (array)$comment[0]);
Sign up to request clarification or add additional context in comments.

Comments

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.