Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Testing what happens after a redirect

I have a controller that after submitting a email, performs a redirect to the home, like this:

return Redirect::route('home')->with("message", "Ok!"); 

I am writing the tests for it, and I am not sure how to make phpunit to follow the redirect, to test the success message:

public function testMessageSucceeds() {     $crawler = $this->client->request('POST', '/contact', ['email' => '[email protected]', 'message' => "lorem ipsum"]);      $this->assertResponseStatus(302);     $this->assertRedirectedToRoute('home');      $message = $crawler->filter('.success-message');      // Here it fails     $this->assertCount(1, $message); } 

If I substitute the code on the controller for this, and I remove the first 2 asserts, it works

Session::flash('message', 'Ok!'); return $this->makeView('staticPages.home'); 

But I would like to use the Redirect::route. Is there a way to make PHPUnit to follow the redirect?

like image 778
Enrique Moreno Tent Avatar asked Dec 03 '14 22:12

Enrique Moreno Tent


People also ask

What is redirect in Laravel?

Redirect responses are instances of the Illuminate\Http\RedirectResponse class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a RedirectResponse instance. The simplest method is to use the global redirect helper: Route::get('/dashboard', function () {

How do I return a redirect text with Laravel?

Just set the flash message and redirect to back from your controller functiion. session()->flash('msg', 'Successfully done the operation. '); return redirect()->back(); And then you can get the message in the view blade file.

How do you pass a variable redirect in Laravel?

You can use the compact function to pass it to your view and reference it by the name. Show activity on this post. return view('viewfile')->with('card',$card)->with('another',$another); You can send data using redirect method.

Can we send data with redirect in Laravel?

$data=['id'=>1,'name'=>'test']; return Redirect::route('Controller. method')->with('data' => $data);


2 Answers

You can get PHPUnit to follow redirects with:

Laravel >= 5.5.19:

$this->followingRedirects(); 

Laravel < 5.4.12:

$this->followRedirects(); 

Usage:

$response = $this->followingRedirects()     ->post('/login', ['email' => '[email protected]'])     ->assertStatus(200); 

Note: This needs to be set explicitly for each request.


For versions between these two:

See https://github.com/laravel/framework/issues/18016#issuecomment-322401713 for a workaround.

like image 149
huntie Avatar answered Sep 22 '22 18:09

huntie


You can tell crawler to follow a redirect this way:

$crawler = $this->client->followRedirect(); 

so in your case that would be something like:

public function testMessageSucceeds() {     $this->client->request('POST', '/contact', ['email' => '[email protected]', 'message' => "lorem ipsum"]);      $this->assertResponseStatus(302);     $this->assertRedirectedToRoute('home');      $crawler = $this->client->followRedirect();      $message = $crawler->filter('.success-message');      $this->assertCount(1, $message); } 
like image 23
jsawo Avatar answered Sep 20 '22 18:09

jsawo