framework: [BUG][Lumen] Unit test with migrations: Class config does not exist

I’m trying to setup unit tests for my Lumen application. Sadly, that only works well for the first test. So I’m assuming Lumen doesn’t refresh correctly. Have a look at my TestCase class:

public function createApplication()
{
    return require __DIR__ . '/../bootstrap/app.php';
}

public function artisanMigrateRefresh()
{
    Artisan::call('migrate');
}

public function artisanMigrateReset()
{
   Artisan::call('migrate:reset');
}

When running a test, I’m setting up the database using:

public function setUp()
{
    parent::setUp();
    $this->artisanMigrateRefresh();
}

public function tearDown()
{
    $this->artisanMigrateReset();
    parent::tearDown();  // Moving that call to the top of the function didn't work either.
}

This works well for the first test. The second test fails, saying “ReflectionException: Class config does not exist” (Full error trace).

I’ve tried loading Lumen once for all tests (just for clarification), and that kind of worked, but as you may have guessed it just made things worse.

The migration (and call) itself works… I’ve adjusted the phpunit.xml to use a sqlite memory-database:

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

To reproduce this behaviour, have a look at my test repository. Just check it out, run composer update and then run phpunit. You will see the test failing. Remove the second test at tests/ExampleTest.php, run phpunit again and it will succeed.

Things I’ve done:

  • Extended tests as described.
  • Added a migration-script.
  • Let the main page create and fetch something from the database (to test that this actually working).
  • Enabled Facades.

(See the forum post for additional information)

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 30 (2 by maintainers)

Most upvoted comments

Changing the setUp method to use createApplication each time fixed this for me.

public function setUp()
{
    parent::setUp();

    $this->createApplication();

    $this->artisanMigrateRefresh();
}

Hope this helps!

I was running into a similar problem when running PHPUnit 7.5 tests with Laravel Lumen 5.4, which was solved by Benjamin Kimball’s solution to add or move the setUp() method to the base TestCase class.

The error message I was getting was “ReflectionException: Class config does not exist” when running PHPUnit:

Screen Shot 2019-08-07 at 1 06 40 PM

All of the values in my .env were properly quoted, but the following changes to tests/TestCase.php fixed the issue:

<?php

abstract class TestCase extends Laravel\Lumen\Testing\TestCase
{
    public function createApplication()
    {
        return require __DIR__.'/../bootstrap/app.php';
    }

    public function setUp()
    {
        parent::setUp();

        $this->createApplication();
    }
}

[Using Laravel] I forgot to add parent::setUp(); in my setUp() method and was getting this error. After adding it, everything worked fine for me.

@KarimGeiger Sorry, should have mentioned that you should move the setUp to the base TestCase!

<?php

use Illuminate\Support\Facades\Artisan as Artisan;

class TestCase extends Laravel\Lumen\Testing\TestCase {

    /**
     * Creates the application.
     *
     * @return \Laravel\Lumen\Application
     */
    public function createApplication()
    {
        return require __DIR__.'/../bootstrap/app.php';
    }

    public function setUp()
    {
        parent::setUp();

        $this->createApplication();

        $this->artisanMigrateRefresh();
    }

    public function tearDown()
    {
        $this->artisanMigrateReset();

        parent::tearDown();
    }

    protected function artisanMigrateRefresh()
    {
        Artisan::call('migrate');
    }

    protected function artisanMigrateReset()
    {
        Artisan::call('migrate:reset');
    }
}

I just confirmed this with your test repo!