mercredi 7 janvier 2015

Laravel unit testing calling a Route from a Test with authentication

I'm testing my api. Before calling a route I log in user to application. Problem is that after authentication user's id is not assigned to Auth::id() within route call.


Here is the scenario:


Test method:



public function testApiGetOrder()
{
var_dump($this->user); // first dump
Auth::login($this->user); // Can't use $this->be($this->user) here, it would not help anyway...
var_dump(Auth::id()); // second dump

$response = $this->call('GET', '/order/' . $this->order->getKey());

$this->assertResponseOk();
$this->assertJson($response->getContent());
$this->assertJsonStringEqualsJsonString($this->order->toJson(), $response->getContent());
}


OrderController's method:



public function show($id)
{
var_dump(Auth::id()); // third dump
var_dump(Auth::user()->getKey()); // fourth dump

// Calling model's logic here
}


Output of of testApiGetOrder:


First dump: object(User)

Second dump: int(1)

Third dump: NULL

Fourth dump: int(1)


Why the value of user's id is not assigned to Auth::id() ?


Aucun commentaire:

Enregistrer un commentaire