Lumen-framework: phpunit tearDownAfterClass() failing on DB facade call

Created on 1 Dec 2016  路  3Comments  路  Source: laravel/lumen-framework

I have a simple tearDownAfterClass() method in one of my tests that looks like this

public static function tearDownAfterClass() {
    parent::tearDownAfterClass();

    $query = "DROP TABLE IF EXISTS `SEAT`;";
    try {
        $result = DB::unprepared($query);
    } catch (Exception $e) {
        echo $e->getMessage();
        echo $e->getTraceAsString();
    }
}

I added the try/catch after running into trouble with it.
but the message output is

Class config does not exist

The trace is this

 #0 /Users/xxxxxxx/apps/theapp/vendor/illuminate/container/Container.php(749): ReflectionClass->__construct('config')
#1 /Users/xxxxxxx/apps/theapp/vendor/illuminate/container/Container.php(644): Illuminate\Container\Container->build('config', Array)
#2 /Users/xxxxxxx/apps/theapp/vendor/laravel/lumen-framework/src/Application.php(211): Illuminate\Container\Container->make('config', Array)
#3 /Users/xxxxxxx/apps/theapp/vendor/illuminate/container/Container.php(1203): Laravel\Lumen\Application->make('config')
#4 /Users/xxxxxxx/apps/theapp/vendor/illuminate/database/DatabaseManager.php(252): Illuminate\Container\Container->offsetGet('config')
#5 /Users/xxxxxxx/apps/theapp/vendor/illuminate/database/DatabaseManager.php(86): Illuminate\Database\DatabaseManager->getDefaultConnection()
#6 /Users/xxxxxxx/apps/theapp/vendor/illuminate/database/DatabaseManager.php(62): Illuminate\Database\DatabaseManager->parseConnectionName(NULL)
#7 /Users/xxxxxxx/apps/theapp/vendor/illuminate/database/DatabaseManager.php(317): Illuminate\Database\DatabaseManager->connection()
#8 /Users/xxxxxxx/apps/theapp/vendor/illuminate/support/Facades/Facade.php(237): Illuminate\Database\DatabaseManager->__call('unprepared', Array)
#9 /Users/xxxxxxx/apps/theapp/vendor/illuminate/support/Facades/Facade.php(237): Illuminate\Database\DatabaseManager->unprepared('DROP TABLE IF E...')
#10 /Users/xxxxxxx/apps/theapp/functional_tests/app/Models/SeatReserveTest.php(41): Illuminate\Support\Facades\Facade::__callStatic('unprepared', Array)
#11 /Users/xxxxxxx/apps/theapp/functional_tests/app/Models/SeatReserveTest.php(41): Illuminate\Support\Facades\DB::unprepared('DROP TABLE IF E...')
#12 [internal function]: SeatReserveTest::tearDownAfterClass()
#13 phar:///usr/local/Cellar/phpunit/5.6.2/libexec/phpunit-5.6.2.phar/phpunit/Framework/TestSuite.php(758): call_user_func(Array)
#14 phar:///usr/local/Cellar/phpunit/5.6.2/libexec/phpunit-5.6.2.phar/phpunit/TextUI/TestRunner.php(465): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#15 phar:///usr/local/Cellar/phpunit/5.6.2/libexec/phpunit-5.6.2.phar/phpunit/TextUI/Command.php(185): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array, true)
#16 phar:///usr/local/Cellar/phpunit/5.6.2/libexec/phpunit-5.6.2.phar/phpunit/TextUI/Command.php(115): PHPUnit_TextUI_Command->run(Array, true)
#17 /usr/local/Cellar/phpunit/5.6.2/libexec/phpunit-5.6.2.phar(572): PHPUnit_TextUI_Command::main()
#18 {main}

This feels like a bug. It looks like the db config isn't getting held on to after the test is done?

Using Lumen 5.3.1

Most helpful comment

The app is destroyed in tearDown, which is called before tearDownAfterClass (source). The app is destroyed for every method and rebuilt, and your code is running at the very end of the test class after it's already been destroyed.

You should probably use the DatabaseMigrations trait. If that won't work for what you are doing, you can register a callback to run right before the app is destroyed like this:

$this->beforeApplicationDestroyed(function () {
    // your code
});

All 3 comments

The app is destroyed in tearDown, which is called before tearDownAfterClass (source). The app is destroyed for every method and rebuilt, and your code is running at the very end of the test class after it's already been destroyed.

You should probably use the DatabaseMigrations trait. If that won't work for what you are doing, you can register a callback to run right before the app is destroyed like this:

$this->beforeApplicationDestroyed(function () {
    // your code
});

Why the issue was closed if there is no resolution?

+1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rtheunissen picture rtheunissen  路  3Comments

rmblstrp picture rmblstrp  路  5Comments

cvinothkumar picture cvinothkumar  路  3Comments

dunice picture dunice  路  3Comments

georgeboot picture georgeboot  路  4Comments