ReflectionException: Laravel 5.4 - php

Im getting this exception when running phpunit. I'm running the latest Laravel 5.4 with PHPUnit 5.7.23
ReflectionException: Class config does not exist
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Container/Container.php:729
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Container/Container.php:608
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Container/Container.php:575
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:728
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Container/Container.php:1172
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Support/ServiceProvider.php:57
/home/vagrant/Code/ProcessingHub-App/vendor/myvendor/core/src/app/Providers/CoreServiceProvider.php:50
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:574
/home/vagrant/Code/ProcessingHub-App/tests/CreatesApplication.php:18
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:89
/home/vagrant/Code/ProcessingHub-App/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:66
/home/vagrant/Code/ProcessingHub-App/tests/TestCase.php:27
/home/vagrant/Code/ProcessingHub-App/tests/Unit/PassportTest.php:30
As you can see the exception comes from CoreServiceProvider this is one of my own ServiceProviders Nothing to weird happening there as you can see here
/**
* Register any package services.
*
* #return void
*/
public function register()
{
$this->registerEloquentFactoriesFrom(__DIR__.'/../../database/factories');
$this->mergeConfigFrom(
__DIR__.'/../../config/fields.php', 'fields'
);
}
contents of fields.php:
return [];
If i outcommend $this->mergeConfigFrom() it works like a charm, but the weird thing is i do this in multiple ServiceProviders and in these Classes it is not an issue.
I literally tried everything.
Running: composer dump-autoload
Running: php artisan optimize
Removing vendor and reinstalling everything
Debugging everywhere but no usefull info
Replacing test with basic testExample from laravel didn't work either
I already replaced my .env with an new simple .env didn't help.
My question is:
Does annyone know how i could fix this.

After I looked more into the \tests directory with a fresh Laravel installation i saw the thing i was staring at for like hours and hours.
The application was trying to register the ServiceProvider before the app was bootstraped. Sorry for the troubles (It was not my code).
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
trait CreatesApplication
{
/**
* Creates the application.
*
* #return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$app->register(\StepOrange\Core\Providers\CoreServiceProvider::class);
return $app;
}
}

Related

Deprecation notices in every console command following Symfony 5.4 upgrade

I am quite confused by the following deprecation notice in my Symfony 5.4 application.
The "Symfony\Component\Console\Command\Command::$defaultName" property
is considered final. You should not override it in
"CRMPiccoBundle\Console\Command\Aws\Cognito\CreateUser".
In my code I have the following:
class CreateUser extends Command
{
protected static $defaultName = 'crmpiccobundle:aws:cognito:createuser';
...which aligns with the documentation.
The parent Command class looks like this:
/**
* #var string|null The default command name
*/
protected static $defaultName;
All my commands are now outputting a deprecation notice, which is not ideal.
composer show | grep console
symfony/console v5.4.19 Eases the creation of beautiful and testable command line interfaces
What am I misunderstanding here? I'm on PHP 8.1.14.
First: Deprecations are not errors You are not required to fix anything until you upgrade to Symfony 6. Then you must fix the error.
Second, like many documentations, the Symfony docs sometimes lag behind the development. The new method is documented here
So you can switch to using this
#[AsCommand(
name: 'crmpiccobundle:aws:cognito:createuser',
description: 'Creates a new user.',
)]
class CreateUser extends Command
{
...

Why Travis CI fails at testing my Laravel web route?

On my local machine all my phpunit tests passed successfully. But when the tests run on Travis CI, the one testing my Laravel web route fails.
Here is my route definition:
Route::get('/{any}', 'SinglePageController#index')->where('any', '.*')->name('landing');
My controller:
/**
* return the main view
* #return view
*/
public function index()
{
return view('landing');
}
(My Laravel view is named landing.blade.php)
And here is my test:
/**
* test return main web view
*
* #test
*/
public function testLandingViewIsReturned()
{
$response = $this->get(route('landing', ['any' => '']));
$response->assertSuccessful()
->assertViewIs('landing');
}
The Travis CI log:
1) Tests\Unit\RouteTest::testLandingViewIsReturned
Response status code [500] is not a successful status code.
Failed asserting that false is true.
Again, this test passes on my local dev env.
What's wrong with Travis?
Problem solved: my mix-manifest.json was missing during Travis build because I forgot to npm run dev in my .travis.yml configuration file.
The exception wasn't throw on my local env because I build assets every day.

Class 'Vendor\PackageName\ClassName' not found for newly created Laravel package

I've a problem running my newly created Laravel package which please check out https://github.com/Younesi/laravel-aparat
I can download it via Composer with no problem and it's auto-discovered via Laravel but when I try to use it, It gives me the following error of not finding class.
Class 'Younesi\LaravelAparat\Aparat' not found
My service Provider code is like:
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->app->bind('aparat', function ($app) {
return new Aparat;
});
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return array('aparat');
}
Any help would be appreciated.
Looking at the package it's working fine, in composer.json of that package there is:
"autoload": {
"psr-4": {
"Younesi\\laravelAparat\\": "src"
}
},
Notice that laravel is not with capital letter here, so in your code you should import rather this way:
use Younesi\laravelAparat\Aparat;
instead of:
use Younesi\LaravelAparat\Aparat;
I also see that you are author of this package, so I would recommend using standard conversion (namespace starting with capital letter) instead of current namespace.
Looking further at package code, I also see that in service provider there is:
namespace Younesi\LaravelAparat;
namespace so it's nothing weird it won't work if you autoload it with lower-case letter and have namespace with upper-case letter
There were some cases with registration problem, cache issues, etc. Try one of these solutions:
register your provider (in main composer.json, then in config/app.php [provider & alias] ), then run composer dump-autoload
make sure you have initiated your package : go to the folder, then composer init
try php artisan config:cache or delete everything in bootstrap/cache/

PHP artisan suddenly doesn't work

I have started to learn Laravel. Until now, everything worked perfectly. I'm following this tutorial and I'm stuck with episode 7.
The problem is that I cannot start artisan anymore. I have tried to install tinker, and I've probably updated artisan so I ended up without artisan and tinker. I am using Linux Ubuntu 12.04 LTS. I have installed everything via command line. After that I tried to run:
php artisan --version
The following problem occurs:
[ErrorException]
Declaration of App\Providers\EventServiceProvider::boot() should be
compati ble with
Illuminate\Foundation\Support\Providers\EventServiceProvider::boot
()
This is how my file app/Providers/EventServiceProvider.php looks like:
<?php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
];
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
//
}
}
I'm using Laravel 5.2 and my composer.json it looks like this:
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"doctrine/dbal": "^2.6#dev",
"vluzrmos/tinker": "dev-master",
"moon/artisan": "dev-master"
I've seen similar problems here for example:
https://laracasts.com/discuss/channels/general-discussion/l5-composer-update-ends-with-an-error-suddenly
https://laracasts.com/discuss/channels/laravel/event-service-provider-in-package
but never the answer was given directly and actually I do not understand how to solve this problem? I would need direct answer because I'm newbie in Laravel. Can artisan be updated somehow easy with Linux command line so it can work again?
Apparently, the new boot() method doesn't take any argument. You'll have to apply some changes to the three providers.
/**
* Register any other events for your application.
*
- * #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
- public function boot(DispatcherContract $events)
+ public function boot()
{
- parent::boot($events);
+ parent::boot();
//
}
Check out this commit for the full list of changes.
https://github.com/laravel/laravel/commit/2b05ce3b054593f7622c1be6c4c6aadc1c5a54ae
Similar to #greut answer, but if it is caused by upgrading laravel (which may be triggered if you are installing other package through composer update and your version for laravel is dev-master), there are 2 places that you need to change the parameter.
App\Providers\RouteServiceProvider
App\Providers\EventServiceProvider
In both controller, there is a method named boot(). Change the parameter to empty. i.e.
public function boot(/*original something here. empty it*/)
{
parent::boot(/*original something here. empty it*/);
}
Reference: https://laracasts.com/discuss/channels/forge/laravel-53-update-causing-error-on-forge-only/replies/189654
I encountered the same problem in forge while performing the upgrade to 5.3, you need to get rid of bootstrap/cache and as you mentioned artisan won’t launch because of that error so you need to do it the old way: rm -R bootstrap/cache and then mkdir bootstrap/cache. Don’t forget to apply the correct permissions of bootstrap/cache after you’re done.
Speaking strictly from a PHP point of view, when artisan tries to start up its CLI application, and you get this error
Declaration of App\Providers\EventServiceProvider::boot() should be compatible with Illuminate\Foundation\Support\Providers\EventServiceProvider::boot
You've defined a class App\Providers\EventServiceProvider. This class has Illuminate\Foundation\Support\Providers\EventServiceProvider as an parent/ancestor (aliased as ServiceProvider in your class).
The boot method in your Illuminate\Foundation\Support\Providers\EventServiceProvider has a set of arguments. You have defined boot in App\Providers\EventServiceProvider, and changed those arguments somehow (fewer arguments, different type hints, different/no defaults, etc.).
You can't do that.
Make you boot compatible with the parent class, and you'l fix your problem.
(This, however, might not fix all your problems, as the comments make it sound like you're using an unreleased version of Laravel that may differ from what's in a tutorial)

Adding a custom Artisan command with Behat

I've registered a custom Artisan command:
Artisan::add(new MigrateAll);
The class resides in app/commands (default location)
However when I run Behat I get the error:
Class 'MigrateAll' not found
Artisan is called in Behat for setting up the DB:
/**
* #static
* #beforeSuite
*/
public static function setUpDb()
{
Artisan::call('migrate:install');
//...
}
Do I need to give it a namespace? (I could not find the correct way to call the Artisan::add command with a namespaced class)
This is somewhat related to your earlier question. Your Behat test suite runs in a separate process independently of your app and knows nothing about the configuration. This also applies to the autoloading in your bootstrap and the autoloading would be the most likely reason why classes don't get found. This should be easily fixed by using Composer to autoload your own sources and vendor packages (both in your app and in your test suite).
# composer.json
{
"require": {
"…": "…"
},
"autoload": {
"psr-0": {
"": "../src"
}
}
}
// Include composer's autoloader in your `setUp()` / bootstrap / index.php.
include __DIR__ . '../vendor/autoload.php';
Take that process separation as a rule and keep in mind that Laravel like any other framework requires a whole bunch of other configuration. Since you are trying to use the database component, your next issue will be with that, because it won't be configured in your test suite.
The best approach is to create separate bootstrap file for Behat, which would inherit most lines from your normal bootstrap, where you need to pass the necessary configuration and do this:
/**
* #static
* #beforeSuite
*/
public static function setUp()
{
include_once('bootstrap.php');
}
If you configured your behat environment with this tut (Laravel, BDD And You: Let’s Get Started), after you added a new command, you need to $ composer dump-autoload to make behat to know the command.

Categories