if my data that passes to view has structure like this:
[
'foo' => [
'a' => 1,
'b' => 2,
],
'bar' => [
'c' => 3,
'd' => 4,
],
]
how can I write a test for this, that assert this array should have foo,a,b,bar... keys?
it likely checks the structure of array, but since assertjsonstructure() method exist only for json response I can not use this method for this issue.
to make it easy understand what I need here my code:
public function show($slug): View
{
$post = Post::whereStatus('RELEASED')
->whereSlug($slug)
->firstOrFail();
$permissions['access_to_edit'] = FALSE;
$permissions['access_to_delete'] = FALSE;
return view('post.news.show',compact('post','permissions'));
}
/**
* @test
*/
public function news_can_be_shown()
{
$this->withoutExceptionHandling();
$post = PostFactory::released()->withImage()->create();
$response = $this->get(route('news.show',['slug' => $post->slug]))
->assertOk()
->getOriginalContent()
->getData();
// I need something like this:
$response->assertArrayStruct([
'post' => [
'id','main_title','secondary_title',...
],
'permissions' => [
'access_to_edit','access_to_delete'
]
]);
// But so far I have not been able to write anything better than this:
$this->assertEquals($news->id,$response['post']['id']);
$this->assertEquals($news->main_title,$response['post']['main_title']);
$this->assertEquals($news->secondary_title,$response['post']['secondary_title']);
$this->assertEquals($news->context,$response['post']['context']);
$this->assertEquals($news->slug,$response['post']['slug']);
$this->assertEquals($news->user->name,$response['post']['user']['name']);
$this->assertEquals($news->image->path,$response['post']['image']['path']);
$this->assertEquals($news->comment,$response['post']['comment']);
$this->assertEquals($news->like,$response['post']['like']);
$this->assertEquals($news->dislike,$response['post']['dislike']);
$this->assertEquals($news->view,$response['post']['view']);
$this->assertEquals($news->created_at,$response['post']['created_at']);
}