Is there any way to test a route using the PHP unit in Laravel?

8

I really like to use PHPunit to create unit tests for my libraries.

Always, before doing git commit , I run it to see if any changes affected what was previously working.

I'd also like to be able to test route by route in Laravel , similar to PHPUnit , so that when I make a change, I may already have automated tests, instead of going out testing route by route, by the browser.

How could I do this in Laravel ?

    
asked by anonymous 20.04.2016 / 22:15

1 answer

8

In Laravel specifically you can use the classes of the Illuminate\Foundation\Testing\ namespace (source: link )

  

Note that the example here is for Laravel5.2

Within the tests folder within your application Laravel should contain two files, the TestCase.php which is the "base" for creating your tests and the ExampleTest.php that you can use to create your tests or then you can create a new file, all files you create must end with Test.php , for example:

  • FooTest.php
  • BarTest.php
  • BazTest.php

An example:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    public function testBasicExample()
    {
        $this->visit('/')
             ->see('Laravel 5')
             ->dontSee('Teste');
    }
}
  • The visit method makes a GET request in the application.
  • The see method states that we should see in the application response the text Laravel 5
  • The dontSee method says that we should not see response Teste text in the application response

To get all the routes you can use the method ->getRoutes(); of class \Illuminate\Routing\Router that will return an object of type:

20.04.2016 / 22:59