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
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.
Related
I've been stuck trying to figure out why I get this every time I try to run php artisan key:generate (or any other php artisan command).
PHP Fatal error: Uncaught ReflectionException: Class log does not exist in C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php:734
Stack trace:
#0 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php(734): ReflectionClass->__construct('log')
#1 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php(629): Illuminate\Container\Container->build('log', Array)
#2 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(697): Illuminate\Container\Container->make('log', Array)
#3 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php(849): Illuminate\Foundation\Application->make('log')
#4 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php(804): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter))
#5 C:\Users\Sam\P in C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 734
Fatal error: Uncaught ReflectionException: Class log does not exist in C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php:734
Stack trace:
#0 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php(734): ReflectionClass->__construct('log')
#1 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php(629): Illuminate\Container\Container->build('log', Array)
#2 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(697): Illuminate\Container\Container->make('log', Array)
#3 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php(849): Illuminate\Foundation\Application->make('log')
#4 C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php(804): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter))
#5 C:\Users\Sam\P in C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 734
Some suggested putting this at the top of vendor\laravel\framework\src\Illuminate\Container\Container.php because it would reveal the underlying error:
namespace {
use Monolog\Logger as Monolog;
class log extends Illuminate\Log\Writer {
function __construct()
{
$this->monolog = new Monolog("local");
}
}
}
But instead I started getting:
PHP Fatal error: Uncaught ReflectionException: Class env does not exist in C:\Users\Sam\PhpstormProjects\banqo.net\vendor\laravel\
framework\src\Illuminate\Container\Container.php:744
with a similar stacktrace to the initial error. I've been researching online what could be causing it. Someone suggested running 'phpunit', so I did and I got the error:
Dotenv\Exception\InvalidFileException: Dotenv values containing spaces must be surrounded by quotes.
I thought this was interesting because people had suggested that .env values containing spaces could have caused the initial problem, but I have thoroughly and repeatedly checked my .env file for spaces and there are none where there shouldn't be. I even tried wrapping literally every single .env value in quotes. On top of all this my IDE (Phpstorm) tells me that there are no errors in any of the files in my config folder.
What could be causing this issue?
STEP 1
Make sure you have installed all packages properly composer install.
STEP 2
Run composer dump-autoload to generate class references again.
I just took a project from a couple of months ago which was made in Laravel. This project also utilizes Laravel Voyager - the admin panel package.
After browsing to http://example.com/admin, I got this weird error which came out of nowhere and I'm also unsure how to debug it. It looks like this:
Fatal error: Cannot declare class Symfony\Component\HttpFoundation\AcceptHeader, because the name is already in use in /c/Users/User/Code/example-website/vendor/symfony/http-foundation/AcceptHeader.php on line 22
Fatal error: Uncaught Error: Class 'Symfony\Component\HttpFoundation\AcceptHeaderItem' not found in /c/Users/User/Code/example-website/vendor/symfony/http-foundation/AcceptHeader.php:61 Stack trace: #0 [internal function]: Symfony\Component\HttpFoundation\AcceptHeader::Symfony\Component\HttpFoundation\{closure}(Array) #1 /c/Users/User/Code/example-website/vendor/symfony/http-foundation/AcceptHeader.php(57): array_map(Object(Closure), Array) #2 /c/Users/User/Code/example-website/vendor/symfony/http-foundation/Request.php(1664): Symfony\Component\HttpFoundation\AcceptHeader::fromString('text/html,appli...') #3 /c/Users/User/Code/example-website/vendor/laravel/framework/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php(54): Symfony\Component\HttpFoundation\Request->getAcceptableContentTypes() #4 /c/Users/User/Code/example-website/vendor/laravel/framework/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php(44): Illuminate\Http\Request->wantsJson() #5 /c/Users/User/Code/example-website/vendor/laravel/framework/src/Illumina in /c/Users/User/Code/example-website/vendor/symfony/http-foundation/AcceptHeader.php on line 61
This error is very expected and I've never seen it before in my project (or anywhere else for that matter).
How can I debug this issue?
As a sanity check, delete the vendor directory and then run composer install to re-fetch your dependencies and see if the error is resolved. If not, then attempt composer update to fetch the latest versions of your dependencies in which this issue may have been resolved.
We have a project that uses Lumen 5.3, however when we always install PHP7 on the server it gives this error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Uncaught TypeError: Argument 1 passed to Symfony\Component\Debug\ExceptionHandler::handle() must be an instance of Exception, instance of Error given in /var/www/retail-api/vendor/symfony/symfony/src/Symfony/Component/Debug/Excepti
onHandler.php:105
Stack trace:
#0 [internal function]: Symfony\Component\Debug\ExceptionHandler->handle(Object(Error))
#1 {main}
thrown
When we revert it back to PHP5.6 it works. Based on my research this was supposed to be fixed in Symfony > 2.6 is this correct? If so why is this happening on our environment. Most of our Laravel projects are already using PHP7. This, however is an exception, is there something that I am missing?
When I run command php artisan schedule:run, it runs fine. However, when I put the command into the scheduler as follows:
* * * * * php /PATH/TO/PROJECT/artisan schedule:run >> /PATH/TO/PROJECT/storage/logs/scheduler.log 2>&1
I get the following error every time it runs.
PHP Fatal error: Uncaught ReflectionException: Class log does not exist ...
This question shows that it's because of an error that happens early in the bootstrap process and tries to log an error before the logger is registered.
I can't seem to figure out where to start debugging since this is the only call stack I get:
PHP Fatal error: Uncaught ReflectionException: Class log does not exist in /PATH/TO/PROJECT/vendor/laravel/framework/src/Illuminate/Container/Container.php:734
Stack trace:
#0 /PATH/TO/PROJECT/vendor/laravel/framework/src/Illuminate/Container/Container.php(734): ReflectionClass->__construct('log')
#1 /PATH/TO/PROJECT/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\Container\Container->build('log', Array)
#2 /PATH/TO/PROJECT/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(697): Illuminate\Container\Container->make('log', Array)
#3 /PATH/TO/PROJECT/vendor/laravel/framework/src/Illuminate/Container/Container.php(849): Illuminate\Foundation\Application->make('log')
#4 /PATH/TO/PROJECT/vendor/laravel/framework/src/Illuminate/Container/Container.php(804): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter))
#5 /PATH/TO/PROJECT/vendor/laravel/framework/src/Illuminate/Container/Container.php(773): Illuminate\Container\Container in /PATH/TO/PROJECT/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 734
I suspect it may have something to do with permissions, however the crontab is tied to the same user who runs it manually. What is causing the original error?
I've found what was causing my error in particular (it may/may not vary for others). After debugging through many source files I've found the source of the issue was that the PDO extension wasn't being loaded. I don't know why this was the case only when running as a cron-job.
As a note: it seems that other having the same issue often have it during the configuration phase so it might be a good idea to start debugging from there if you are getting the same exception.
I'm trying to get PHPUnit working; I haven't used it before. If I run phpunit in the CLI, it works, but via a script (which is what I'm looking for) it doesn't. This is the output I get (note the two spaces after Class):
Fatal error: Uncaught exception 'ReflectionException' with message 'Class does not exist' in /usr/local/share/pear/PHPUnit/Util/Test.php:295
Stack trace:
#0 /usr/local/share/pear/PHPUnit/Util/Test.php(295): ReflectionClass->__construct('')
#1 /usr/local/share/pear/PHPUnit/Util/Test.php(576): PHPUnit_Util_Test::parseTestMethodAnnotations(false, false)
#2 /usr/local/share/pear/PHPUnit/Util/Test.php(350): PHPUnit_Util_Test::getBooleanAnnotationSetting(false, false, 'backupGlobals')
#3 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(458): PHPUnit_Util_Test::getBackupSettings(false, false)
#4 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(834): PHPUnit_Framework_TestSuite::createTest(Object(ReflectionClass), false)
#5 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(212): PHPUnit_Framework_TestSuite->addTestMethod(Object(ReflectionClass), Object(ReflectionMethod))
#6 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(315): PHPUnit_Framework_TestSuite->__construct(Object(ReflectionClass))
#7 /var/www/www.s in /usr/local/share/pear/PHPUnit/Util/Test.php on line 295
It appears that the class name is being lost in addTestMethod. This is my code:
<?php
require_once 'PHPUnit/Autoload.php';
class MyTestCase extends PHPUnit_Framework_TestCase {
public function testSubtraction() {
$this->assertEquals(2 - 2, 0);
}
public function testAddition() {
$this->assertEquals(2 + 2, 4);
}
}
$c = new MyTestCase();
$suite = new PHPUnit_Framework_TestSuite();
$suite->addTestSuite('MyTestCase');
PHPUnit_TextUI_TestRunner::run($suite);
Am I setting things up wrong?
Here are the versions:
Installed packages, channel pear.phpunit.de:
============================================
Package Version State
File_Iterator 1.3.3 stable
PHPUnit 3.7.21 stable
PHPUnit_MockObject 1.2.3 stable
PHP_CodeCoverage 1.2.11 stable
PHP_Timer 1.0.4 stable
PHP_TokenStream 1.1.5 stable
Text_Template 1.1.4 stable
PHP 5.4
Reflection does work, I use that in my own scripts. Hopefully it's something with my setup, I really don't want to have to modify PHPUnit...surely it works elsewhere.
Something is wrong:
'Class does not exist'
^
`- normally the classname is given here
As you can see your error message is either missing something or PHPUnit tried to create a testcase out of nothing. Check you don't have some superfluous / incomplete files in your tests folder. Or something like that along the line. It's just a guess based on the error message, if I were you I would tackle this down in a debug session to actually find out what happens. This includes remove-debugging the testing session and stepping through and probably adding debug code into the phpunit install. Take care and backups.