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');
}
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();
$content = $content->getData();
return $content[$key]->all();
}
So to get a value from the $data object I simply use $user = $this->getResponseData($response, 'user');
Inside a test case use:
$data = $this->response->getOriginalContent()->getData();
Example:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$data = $this->response->getOriginalContent()->getData();
// do your tests on the data
}
}
Example dumping data so you can see what in data(array) passed to view:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$data = $this->response->getOriginalContent()->getData();
dd($data);
}
}
Should get something back like what's in image:
So looking at how assertViewHas
is implemented HERE it looks like, what the method does, is access the view's data after this call:
$response = $this->client->getResponse()->original;
In your code, the line:
$response = $this->call('GET', 'user/edit/'.parent::ACCOUNT_1_USER_1);
essentially returns the same thing as the line above it, namely a \\Illuminate\\Http\\Response
(which extends the symfony component \\HttpFoundation\\Response
)
So, inside the assertViewHas
function it looks like laravel accesses the data using $response->$key
, so I would try to access the clients
and 'content' variables through the $response
object.
If that doesn't work try searching around the TestCase
file in the Laravel framework ... I'm sure the answer is in there somewhere. Also try to dump the $response
object and see what it looks like, there should be some clues there.
The first thing I would try, though, is accessing your data through the $response
object.
I managed it by doing it in a messy way. I used assertViewHas
:
$this->assertViewHas('clients', array('1' => 'Client 1', '6' => 'Client2'));
You can access data in the response and it can be checked..
public function testSimpleLastProducts() {
$res = $this->call('GET', '/');
$this->assertResponseOk();
$this->assertViewHas('lastProducts');
$lastProductOnView = $res->original['lastProducts'];
$this->assertEquals(6, count($lastProductOnView));
}
This worked for me:
$response->getSession()->get("errors")
And from there you can check the contents of the message box for whatever error you might want verify.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.