AppDividend
Latest Web Development Tutorials

Laravel 5.5 Testing With Example

Testing in Laravel

628

Laravel 5.5 Testing With Example is today’ s main topic. We are using PHPUnit test for this article. PHPUnit Test in Laravel 5.5.  The phpunit.xml file is already configured for our Laravel 5.5 application. By default Laravel 5.5 ships with two directories and two files.

  1. Feature Directory
  2. Unit Directory
  3. CreatesApplication.php
  4. TestCase.php

PHPUnit Environment

When we are running the tests via phpunit then Laravel will set the environment to test automatically.  It configures the cache and session environment to the array driver. So while testing, no session or cache data is persisted. You can actually see it in below code. Go to phpunit.xml file.

<php>
     <env name="APP_ENV" value="testing"/>
     <env name="CACHE_DRIVER" value="array"/>
     <env name="SESSION_DRIVER" value="array"/>
     <env name="QUEUE_DRIVER" value="sync"/>
</php>

Laravel 5.5 Testing: Creating and Running Tests

The first step would be to install brand new Laravel 5.5 Project.

Step 1: Install Laravel 5.5 Testing Project

composer create-project laravel/laravel laraveltesting --prefer-dist

Now, configure the database in the .env file.

Type the following command.

php artisan migrate

It will create two tables in the database.

Now, for testing, we will create another model and migration file. So type the following command.

php artisan make:model Stock -m

It will create stocks table also and we need to define the schema for it.

<?php

// create_stocks_table

  /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('stocks', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('price');
            $table->timestamps();
        });
    }

Next, type the below command.

php artisan migrate

It will make stocks table.

Step 2: Mass assignment exception

In model Stock.php file, add the following property in it.

protected $fillable = ['name','price'];

Step 3: Make the test file.

To make a test file, there is an artisan command for it, so type in the terminal,

Related Posts
1 of 8
php artisan make:test StockTest

This will create feature test file inside tests  >>  Feature  >>  StockTest file.

Now, the file will look like this.

<?php

// StockTest.php 

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class StockTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->assertTrue(true);
    }
}




Next step would be to write one simple test in the testExample() function.

<?php

// StockTest.php

namespace Tests\Feature;

use Tests\TestCase;
use App\Stock;
use Illuminate\Foundation\Testing\RefreshDatabase;

class StockTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
      $stock = new Stock(['name'=>'Tesla']);
      $this->assertEquals('Tesla', $stock->name);
    }
}

Here, I have included our model Stock.php in the StockTest.php file and created an object and try to insert one stock in the array. Remeber by default we are using array drivers. If I have not included the model correctly or I have not added the fillable property in the model then this test will fail otherwise It will pass the test.

Step 4: Run the test.

Please type the following command to run the test.

phpunit

Note: If this will throw an error then please run following command.

vendor/bin/phpunit

or

vendor\bin\phpunit

One of this options will definitely run your tests.

From the above example, we can see the output like this.

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.
… 3 / 3 (100%)
Time: 845 ms, Memory: 10.00MB
OK (3 tests, 3 assertions)
So, we have successfully run the test. Now, if you want to fail the test, just for fun then remove the following line from the StockTest.php file.
use App\Stock;

Now, again run the test.

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

.E. 3 / 3 (100%)

Time: 471 ms, Memory: 10.00MB

There was 1 error:

1) Tests\Feature\StockTest::testExample
Error: Class ‘Tests\Feature\Stock’ not found

L:\testingLaravel\tests\Feature\StockTest.php:17

ERRORS!
Tests: 3, Assertions: 2, Errors: 1.

The test will fail.

Finally, Laravel 5.5 Unit Testing is over. Find more info in this URL: https://laravel.com/docs/5.5/testing

This is just a phpUnit test other browser test and HTTP test will be in the next articles so stay tuned. Also, we will talk about Laravel Dusk in future.

Leave A Reply

Your email address will not be published.