Testing

Laravel is built with unit testing in mind. In fact, support for testing with PHPUnit is included out of the box, and a file is already setup for your application.

An example test file is provided in the tests directory. After installing a new Laravel application, simply run phpunit on the command line to run your tests.

Defining & Running Tests

To create a test case, simply create a new test file in the tests directory. The test class should extend TestCase. You may then define test methods as you normally would when using PHPUnit.

An Example Test Class

You may run all of the tests for your application by executing the phpunit command from your terminal.

When running unit tests, Laravel will automatically set the configuration environment to testing. Also, Laravel includes configuration files for session and cache in the test environment. Both of these drivers are set to array while in the test environment, meaning no session or cache data will be persisted while testing. You are free to create other testing environment configurations as necessary.

The testing environment variables may be configured in the phpunit.xml file.

Calling Routes From Tests

Calling A Route From A Test

You may easily call one of your routes for a test using the call method:

  1. $response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);

You may then inspect the Illuminate\Http\Response object:

  1. $this->assertEquals('Hello World', $response->getContent());

Calling A Controller From A Test

You may also call a controller from a test:

  1. $response = $this->action('GET', '[email protected]');
  2. $response = $this->action('GET', '', ['user' => 1]);

The getContent method will return the evaluated string contents of the response. If your route returns a View, you may access it using the original property:

  1. $view = $response->original;
  2. $this->assertEquals('John', $view['name']);

To call a HTTPS route, you may use the method:

  1. $response = $this->callSecure('GET', 'foo/bar');

When testing, you may often want to mock a call to a Laravel static facade. For example, consider the following controller action:

We can mock the call to the Event class by using the shouldReceive method on the facade, which will return an instance of a mock.

Mocking A Facade

  1. public function testGetIndex()
  2. {
  3. Event::shouldReceive('fire')->once()->with('foo', ['name' => 'Dayle']);
  4. $this->call('GET', '/');
  5. }

Framework Assertions

Laravel ships with several assert methods to make testing a little easier:

Asserting Responses Are OK

  1. public function testMethod()
  2. {
  3. $this->call('GET', '/');
  4. $this->assertResponseOk();
  5. }

Asserting Response Statuses

  1. $this->assertResponseStatus(403);

Asserting Responses Are Redirects

  1. $this->assertRedirectedTo('foo');
  2. $this->assertRedirectedToRoute('route.name');
  3. $this->assertRedirectedToAction('[email protected]');

Asserting A View Has Some Data

  1. public function testMethod()
  2. {
  3. $this->assertViewHas('name');
  4. $this->assertViewHas('age', $value);

Asserting The Session Has Some Data

Asserting The Session Has Errors

  1. public function testMethod()
  2. {
  3. $this->call('GET', '/');
  4. $this->assertSessionHasErrors();
  5. // Asserting the session has errors for a given key...
  6. $this->assertSessionHasErrors('name');
  7. // Asserting the session has errors for several keys...
  8. $this->assertSessionHasErrors(['name', 'age']);
  9. }

Asserting Old Input Has Some Data

  1. public function testMethod()
  2. {
  3. $this->call('GET', '/');
  4. $this->assertHasOldInput();
  5. }

The TestCase class contains several helper methods to make testing your application easier.

Setting And Flushing Sessions From Tests

  1. $this->session(['foo' => 'bar']);
  2. $this->flushSession();

Setting The Currently Authenticated User

You may set the currently authenticated user using the be method:

  1. $user = new User(['name' => 'John']);
  2. $this->be($user);

Re-Seeding Database From Tests

You may re-seed your database from a test using the seed method:

  1. $this->seed();

More information on creating seeds may be found in the section of the documentation.

Refreshing The Application

As you may already know, you can access your Application (service container) via from any test method. This service container instance is refreshed for each test class. If you wish to manually force the Application to be refreshed for a given method, you may use the refreshApplication method from your test method. This will reset any extra bindings, such as mocks, that have been placed in the IoC container since the test case started running.