12/19/2016 - 4:30 PM

Sample usage of Laravel PHPUnit Test

Sample usage of Laravel PHPUnit Test


*  php artisan make:test UserTest creates a test

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

class UserTest extends TestCase
  public function testBasicExample()
    //  call a route
    $response = $this->call('GET', 'user/profile');
    //  call HTTPS
    ->callSecure('GET', 'user/profile');
    //  call a controller
    ->action('GET', 'HomeController@index');
    ->action('GET', 'UserController@profile', array('user' => 1));
    //  get the view of the response
    $view = $response->original;
    //  get a crawler with the call
    $crawler = $this->client->request('GET', '/');
    ->assertCount(1, $crawler->filter('h1:contains("Hello World!")'));
    //  getContent returns the evaluated string contents of the response
    ->assertEquals('Hello World', $response->getContent());

    ->assertViewHas('age', $value);
    ->assertSessionHas('age', $value);
    //  session has errors for a given key
    //  session has errors for several keys
    ->assertSessionHasErrors(array('name', 'age'));
    //  session has old input
    //  set a user as the currently authenticated one
    $user = new User(array('name' => 'John'));
    //  reseed DB
    //  reset any extra bindings (e.g. mocks) placed in the IoC container since test started