How to get view data during unit testing in Laravel

laravel, laravel-4, php, phpunit

Solution

TL;DR; Try `$data = $response->getOriginalContent()->getData();`

I found a better way to do it. I wrote a function in the TestCase which returns the array I want from the view data.

protected function getResponseData($response, $key){

    $content = $response->getOriginalContent();

    $data = $content->getData();

    return $data[$key]->all();

}

So to get a value from the $data object I simply use `$user = $this->getResponseData($response, 'user');`

Problem

I would like to check the array given to a view in a controller function has certain key value pairs. How do I do this using phpunit testing? ``` //my controller I am testing public function getEdit ($user_id) { $this->data['user'] = $user = \Models\User::find($user_id); $this->data['page_title'] = "Users | Edit"; $this->data['clients'] = $user->account()->firstOrFail()->clients()->lists('name', 'id'); $this->layout->with($this->data); $this->layout->content = \View::make('user/edit', $this->data); } //my test public function testPostEdit (){ $user = Models\User::find(parent::ACCOUNT_1_USER_1); $this->be($user); $response = $this->call('GET', 'user/edit/'.parent::ACCOUNT_1_USER_1); //clients is an array. I want to get this //array and use $this->assetArrayContains() or something $this->assertViewHas('clients'); $this->assertViewHas('content'); } ```

Original source