Reputation: 1476
I am working through upgrading my PHP 5.4, Laravel 4.2 application to PHP 7.4, Laravel 5.8.
In some of my tests, on tearDownAfterClass, I will do some DB cleanup. With Laravel 5.8, these don't work and I can't figure out why.
The following test using Laravel 5.4, works. With 5.8, does not work.
I have updated my 5.8 configs from the defaults, they are proper. My database connection works, the tests all pass with DB activity.
The error only fails in the static tearDownAfterClass, I added the same \DB::table(static::$audittable)->truncate();
to one of my tests, it works without error.
How do I fix the exception below?
Using PHP 7.4.6, Laravel 5.8:
class ModelAuditQueriesTest extends TestCase
{
static $audittable = 'contactnotesaudit';
public static function tearDownAfterClass()
{
\DB::table(static::$audittable)->truncate(); **// <-- this fails, error below; line 11**
parent::tearDownAfterClass();
}
public function testInsertSingle()
{
\DB::table(static::$audittable)->truncate(); **// <-- this works, no errors**
/**
* testing stuff with models that insert and select with database, successfully
*/
}
}
With PHPUNIT.XML, processIsolation = false
PHP Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php:960
Stack trace:
#0 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php(794): Illuminate\Container\Container->notInstantiable('Illuminate\\Cont...')
#1 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php(667): Illuminate\Container\Container->build('Illuminate\\Cont...')
#2 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php(615): Illuminate\Container\Container->resolve('Illuminate\\Cont...', Array)
#3 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(767): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)
#4 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php(170): Illuminate\Foundation\Application->make('Illuminate\\ in C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 960
PHP Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php:960
Stack trace:
#0 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php(794): Illuminate\Container\Container->notInstantiable('Illuminate\\Cont...')
#1 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php(667): Illuminate\Container\Container->build('Illuminate\\Cont...')
#2 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php(615): Illuminate\Container\Container->resolve('Illuminate\\Cont...', Array)
#3 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(767): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)
#4 C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php(170): Illuminate\Foundation\Application->make('Illuminate\\ in C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 960
With PHPUNIT.XML, processIsolation = true
PHP Fatal error: Uncaught Error: Class 'DB' not found in C:\CODE\IDWEB5\tests\commonmodelaudit\ModelAuditQueriesTest.php:11
Stack trace:
#0 C:\CODE\IDWEB5\vendor\phpunit\phpunit\src\Framework\TestSuite.php(760): Tests\commonmodelaudit\ModelAuditQueriesTest::tearDownAfterClass()
#1 C:\CODE\IDWEB5\vendor\phpunit\phpunit\src\TextUI\TestRunner.php(545): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#2 C:\CODE\IDWEB5\vendor\phpunit\phpunit\src\TextUI\Command.php(195): PHPUnit\TextUI\TestRunner->doRun(Object(PHPUnit\Framework\TestSuite), Array, true)
#3 C:\CODE\IDWEB5\vendor\phpunit\phpunit\src\TextUI\Command.php(148): PHPUnit\TextUI\Command->run(Array, true)
#4 C:\CODE\IDWEB5\vendor\phpunit\phpunit\phpunit(53): PHPUnit\TextUI\Command::main()
#5 {main}
thrown in C:\CODE\IDWEB5\tests\commonmodelaudit\ModelAuditQueriesTest.php on line 11
Error : Cannot use object of type Illuminate\Support\Facades\Config as array
C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php:270
C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php:101
C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php:77
C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php:349
C:\CODE\IDWEB5\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:239
C:\CODE\IDWEB5\tests\commonmodelaudit\ModelAuditQueriesTest.php:11
I have run the following to make sure all is clean:
composer dump-autoload
php artisan clear-compiled
php artisan optimize
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
Upvotes: 0
Views: 3291
Reputation: 40730
I think the application has already shut down at the tearDownAfterClass
part of the code.
Since you are trying to truncate a table, I think you might find it easier to instead use the trait DatabaseTransactions
e.g.
class ModelAuditQueriesTest extends TestCase
{
use DatabaseTransactions;
}
this will make any changes you make in the database during this test not be rolled back since they are all done within a transaction which is never committed.
Another hacky way to do what you want is to "restart" the application.
The TestCase that comes in the boilerplate has a trait CreatesApplication
. This unfortunately is not static but you can just copy-paste it and "restart" the application:
class ModelAuditQueriesTest extends TestCase
{
static $audittable = 'contactnotesaudit';
public static function tearDownAfterClass()
{
$app = require __DIR__.'/../bootstrap/app.php'; //You might need to adjust the path
$app->make(Kernel::class)->bootstrap();
\DB::table(static::$audittable)->truncate();
parent::tearDownAfterClass();
}
}
Now unfortunately the error you are seeing is that the error handler could not be instantiated which means once you do this you will see the actual reason that the error handler was being instantiated which is probably going to be another exception. However, at least that might be more informative.
Upvotes: 1