Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4; How to run unit tests from a Laravel package?

I am trying to develop a Laravel composer package and run unit tests from within it. After spending the last couple of days reading various outdated and contradictory guides and blogposts, I am completely confused as to how to go about this.

Here's what I know so far:

  • I shouldn't run tests from the main Laravel installation. Tests should all be contained within the package. I'll admit this has a certain logic to it.
  • There's something called Orchestra Testbench. If you're developing a Laravel package then apparently you should use this.
  • There's also something called Laravel Dusk, which is included in Laravel 5.4.

I can get Orchestra Test Bench working with some basic tests from the examples given, but I don't really understand what's going on and the documentation explains almost nothing. When it comes to testing my application's routes, I can't get anything to work.

I don't understand if Orchestra and Dusk can play together, or if I have to choose between them. If so, which one should I use? And if it's Laravel Dusk I should be using, then how do I run it from within my package directory?

like image 533
Inigo Avatar asked Jul 02 '17 00:07

Inigo


1 Answers

I agree with:

I shouldn't run tests from the main Laravel installation. Tests should all be contained within the package

To avoid installing Laravel in your application and bootstraping it by hand to test your package, you can use Orchestral Testbench.

The docs of Orchestral are pretty good today, but I will give anyway a short example how to use it, if you want to make unit tests on some Eloquent models.

First, make sure that your package has a ServiceProvider class which points to your migration files as described in the docs.

Then, require the orchestral/testbench package into your package and create a file in tests/TestCase.php.

The file TestCase should do the following:

  • Extend Orchestra TestCase
  • Point to your Service-Provider with getPackageProviders
  • Setup a SQLite database in memory with getEnvironmentSetUp
  • Run migrate to create you tables in setUp()

Here is an example:

<?php

namespace MyVendorName\MyPackageName\Tests;

use MyVendorName\MyPackageName\MyServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{
    public function setUp(): void
    {
        parent::setUp();

        $this->artisan('migrate', ['--database' => 'testbench'])->run();
    }

    /**
     * add the package provider
     *
     * @param $app
     * @return array
     */
    protected function getPackageProviders($app)
    {
        return [MyServiceProvider::class];
    }


    /**
     * Define environment setup.
     *
     * @param  \Illuminate\Foundation\Application  $app
     * @return void
     */
    protected function getEnvironmentSetUp($app)
    {
        // Setup default database to use sqlite :memory:
        $app['config']->set('database.default', 'testbench');
        $app['config']->set('database.connections.testbench', [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        ]);
    }
}

And to answer your second question: Laravel Dusk works well with Orchestral Testbench. Similar as above where you needed to mock up a database to test your Eloquent package models, you have to mock up routes & views with Orchestral to make the Browser tests with Laravel Dusk.

For more details, check out my blog post.

like image 52
Adam Avatar answered Oct 22 '22 11:10

Adam