artisan test command is not working, laravel 7.x - php

i hope you are doing well, so my problem is that i cannot run the artisan test command
whenever i run the command php artisan test i get the following error
RuntimeException
Running Collision ^5.0 artisan test command requires Laravel ^8.0.
at D:\php-files\behzad-thingi\vendor\nunomaduro\collision\src\Adapters\Laravel\Commands\TestCommand.php:71
67▕ }
68▕
69▕ // #phpstan-ignore-next-line
70▕ if ((int) \Illuminate\Foundation\Application::VERSION[0] < 8) {
➜ 71▕ throw new RuntimeException('Running Collision ^5.0 artisan test command requires Laravel ^8.0.');
72▕ }
73▕
74▕ $options = array_slice($_SERVER['argv'], $this->option('without-tty') ? 3 : 2);
75▕
honestly i didint test that the command worked before, since i upgraded the dependencies to use pestphp and followed their installation steps here. thanks for your answer.

this was an issue with pestphp versioning, as explained in this here. i just had to replace the version of pestphp and collision from
"nunomaduro/collision": "v5.0.0-BETA3",
"pestphp/pest": "^0.2.3",
to
"nunomaduro/collision": "5.0.0-BETA2",
"pestphp/pest": "0.2.1",

Related

Laravel/Passport - After remigration keys are not usable

Settings
Passport Version: "laravel/passport": "^9.3",
Laravel Version: "laravel/framework": "^7.0",
PHP Version: "php": "^7.2.5",
Database Driver & Version: MYSQL 8.0.15
Description:
I have used this article to create fresh API with laravel-passport. After installation I have successfully registered some users. Then after some coding I have needed to add some columns to existing tables. So after adding these columns I have used fresh command to remigrate all database.
php artisan migrate:fresh --seed
The operation was successfully and all tables(oauth tables also) have been created. But after this migration process existing passport grant tokens are not usable. When I register new User by sending normal AJAX request from web app interface I get this error.
Undefined property: stdClass::$refresh_token
I have tried many ways and researched about this issue but only after renewing old client and password keys process worked.
php artisan passport:install --force
Steps To Reproduce:
After installation passport just install and creating keys and migrate database. So everything will work. Then after remigrate database with command php artisan migrate:fresh . Then old keys will not be usable.
Even though this question is quite old. But we can use Event Listeners to achieve repeated steps.
Under app\Providers\EventServiceProvider.php add the following code in boot() method.
Laravel 9
public function boot()
{
parent::boot();
Event::listen(CommandFinished::class, function(CommandFinished $event) {
if($event->command === 'migrate:fresh') {
Artisan::call('passport:install', [ '--force' => true]);
echo Artisan::output();
}
});
}
I found the Illuminate\Database\Events\MigrationsEnded not working for my case. But other one, Illuminate\Console\Events\CommandFinished trigger on all command executions.
Do this to overwrite the existing keys and it will work
php artisan passport:install --force
Or you may publish Passport's configuration file using
php artisan vendor:publish --tag=passport-config
which will then provide the option to load the encryption keys from your environment variables:
Refer this for detailed instruction https://laravel.com/docs/7.x/passport

Symfony 4 - cannot find all migration versions

I am trying to run migrations on a Symfony 4 application with a total of 271 migrations. However, when doing so it skips the first 41 migrations (the first is Version20180921083101).
>>> php bin/console doctrine:migrations:status
== Configuration
>> Name: Application Migrations
>> Database Driver: pdo_mysql
>> Database Host: db
>> Database Name: test
>> Configuration Source: manually configured
>> Version Table Name: migration_versions
>> Version Column Name: version
>> Migrations Namespace: DoctrineMigrations
>> Migrations Directory: /var/www/src/Migrations
>> Previous Version: Already at first version
>> Current Version: 0
>> Next Version: 2018-11-14 06:38:03 (20181114063803)
>> Latest Version: 2020-01-27 05:06:49 (20200127050649)
>> Executed Migrations: 0
>> Executed Unavailable Migrations: 0
>> Available Migrations: 230
>> New Migrations: 230
I am been trying to update the schema, clear cache, and dropped/recreated the database, but without success. I have also tried to execute only the first one by running the following command:
>>> php bin/console doctrine:migrations:migrate Version20180921083101
Application Migrations
Unknown version: Version20180921083101
It seems that those versions prior to Version20181114063803 cannot be recognized.
I have been struggling with this issue for a while now and running out of things to try so any help or pointers would be highly appreciated.
Thanks!
For me the problem was that i was running
php bin/console doctrine:migrations:migrate 'DoctrineMigrations\Version20210403042222'
Instead of
php bin/console doctrine:migrations:migrate DoctrineMigrations\Version20210403042222
No single quotes, although the example in the docs uses single quotes.
So for the initial post, i think you just need to add the namespace without any quotes:
php bin/console doctrine:migrations:migrate DoctrineMigrations/Version20180921083101
If you use Symfony v.4 and want to execute one migration, you should use the next command
php bin/console doctrine:migrations:execute --up 20180921083101
You should simply pass the timestamp as argument, as example:
>>> php bin/console doctrine:migrations:migrate 20180921083101
Check also the doc here
I got the same problem after an update of the doctrine_migration bundle. In my case the Error wasn't really accurate, in fact it was more a problem of "it wasn't recognized as a valid migration" than a real not found issue so I put it there in case someone encounter the same situation.
Adding the following missing function solved my issue:
public function getDescription() : string
{
return 'Your description';
}
Problem might come from migrations executed from different branches before everything was merged together. If my memory is correct Symfony saves the last migration executed in your current database, if migrations comes from other branches with previous timestamp, they will not be executed with the doctrine:migrations:migrate command.

Script #php artisan package:discover handling the post-autoload-dump event returned with error code 255

I moved my project from desk to another.
When I run php artisan it does not work.
I tried to run composer update, but it returns the error
Script #php artisan package:discover handling the post-autoload-dump event returned with error code 255
This is how I solved this after an upgrade from laravel version 6.x - 7.x:
In App\Exceptions\Handler changed
//Use Exception;
Use Throwable;
Then methods to accept instances of Throwable instead of Exceptions as follows:
//public function report(Exception$exception);
public function report(Throwable $exception);
//public function render($request, Exception $exception);
public function render($request, Throwable $exception);
In config\session.php:
//'secure' => env('SESSION_SECURE_COOKIE', false),
'secure' => env('SESSION_SECURE_COOKIE', null),
Then run composer update
I solved the problem this way:
cd bootstrap/cache/
rm -rf *.php
The bootstrap directory contains the app.php file that initializes the structure. This directory also houses a cache directory that contains structure-generated files for performance optimization, such as files and route cache services. Laravel stores configuration files, provider, and cached services to optimize the fetching of this information. The problem with me was when the other developer ran the 'php artisan config: cache' command on your machine and since the cache folder contains files that can be deleted, I deleted them and solved the problem.
If this happened after Laravel update from 6.x to 7.x, then this could be due to the update of Symfony. See the upgrade guide of this part:
https://laravel.com/docs/7.x/upgrade#symfony-5-related-upgrades
I was upgrading my Laravel from 5.8 to 8.0 and I got this error.
So my fixes were
As #nobuhiroharada mentioned that I had missed .env file in my project
Second is that Laravel removed Exception and replaced it with Throwable. So we need to fix that in our app\Exceptions\Handler.php. One can refer Medium.com for the error fix.
In the upgrade guide of Laravel 8.x you need to update the dependencies like this
Next, in your composer.json file, remove classmap block from the autoload section and add the new namespaced class directory mappings:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Finally from bootstrap\cache delete the cache files and run composer update.
These 5 steps might help you remove the error you are facing in your Laravel Project.
This happens because you have upgraded to Laravel 7.
To fix it, update app/Exceptions/Handler.php like so:
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable; // <-- ADD THIS
class Handler extends ExceptionHandler
{
public function report(Throwable $exception) // <-- USE Throwable HERE
{
parent::report($exception);
}
public function render($request, Throwable $exception) // AND HERE
{
return parent::render($request, $exception);
}
}
This is documented in the official upgrade guide here:
https://laravel.com/docs/7.x/upgrade#symfony-5-related-upgrades
I got the same problem in Win 8 and solve it:
Here is the steps.
Step-1: Go to your project directory
Step-2: And type command cd bootstrap/cache/
Step-3: Again type command del -rf *.php
Step-4: Update your composer composer update
Step-5: Now you are done: php artisan serve
Thanks.
Do you have .env file in your new project?
I had same error message. When I add .env file, error is gone.
success message like this.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: ixudra/curl
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: socialiteproviders/manager
Package manifest generated successfully.
I hope this will help you.
maybe you have an error in the project code (for example, in routes or controller). This may be one of the reasons for this error.
In my project, the web.php file has a syntax error. I defined this when I started the php artisan command
C:\OSPanel\domains\lara.shop.loc>php artisan
In web.php line
syntax error, unexpected end of file
Same issue when I update laravel from 6.x to 7.x
I tried the most voted answer but it didn't work, then I used php artisan serve I noticed that:
RuntimeException
In order to use the Auth::routes() method, please install the laravel/ui package.
Try composer require laravel/ui maybe it will work.
I solve this error by deleting the vendor table then run composer update. I'm using Laravel 7. So, if you are not updating from the older Laravel version, maybe this is the solution.
I had this same problem when running composer update in a Laravel project. In the package.json it's configured to run artisan package:discover, which failed with:
Class 'Symfony\Component\Translation\Translator' not found in vendor/nesbot/carbon/src/Carbon/Translator.php on line 18
When I looked in the vendor/symfony/translation directory I found that it was completely empty, which explained the error.
The solution was to completely delete the vendor directory and then re-run composer update. This was the only way that I was able to make composer install the missing files.
I deleted composer.lock file and ran composer update.
That solved mine
I had the same issue, my problem was the PHP version of the server account did not match my Docker container. The SSH terminal was using the global php version for the server.
php -v
Confirm it's the version your project needs.
Composer did warn me that a higher php version was required but I rm -rf'd /vendor and ./composer.lock without paying too much attention to the warnings!
Got the same problem.
php artisan doesn't work.
composer install got error:
Script #php artisan package:discover handling the post-autoload-dump event returned with error code 255
And this works for me.
When I switch another linux user. It works.
some files are owned by another linux user.
So I use root account and change all the project file to the specific user,
chown -R www:www project/
and use that user to execute composer cmd
and then it works.
My case/solution, in case it helps anyone...
I copied my repo over from my old Windows computer to a new one, and installed the latest php.
composer install was returning:
Root composer.json requires php ^7.1.3 but your php version (8.1.10) does not satisfy that requirement
...which I thought was odd (assuming 8 satisfied ^7), so I continued on with composer install --ignore-platform-reqs, and ended up with this particular issue.
After trying a bunch of other possible solutions, what ended up working for me was simply downgrading to the same PHP version from my old machine (7.4.33).
This is not an actual error. If you look a bit above you'll see the actual error.
In my case, there was an error in my code:
PHP Fatal error: Declaration of
App\Exceptions\Handler::render($request, App\Exceptions\Exception $exception)
must be compatible with
Illuminate\Foundation\Exceptions\Handler::render($request, Throwable $e)
It is not possible to tell you what is actually a problem in your code, so you have to look real reason for this error in your stack trace.
In my case there is missing folder and its file Kernel.php in
app/Console
So I created app/Console/Kernel.php using code from previous project.
Now everything working fine.
Make sure your config\constants.php (and/or resources\lang\en\local.php) has no syntax errors. I get this error a lot by missing commas in constants.php file.
If you have this error the simplest way is you can try using composer install instead of composer update
I deleted my project I created a new folder and cloned the repository again and after that I gave composer install / update.
I got the same problem in Win 10 and solve it:
Here is the steps.
Step-1: Go to your project directory
Step-2: Update your composer
composer update
Step-3: Now you are done: php artisan serve
Nothing worked, so I installed a new project, and I read Handler.php in App\Exceptions, it was different, probably because I copied some solution and Internet and deleted the following:
protected $dontReport = [
//
];
protected $dontFlash = [
'password',
'password_confirmation',
];
I copy here all of Handler.php generated by laravel 7.5, may be useful for someone:
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* #var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* #var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* #param \Throwable $exception
* #return void
*
* #throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #param \Throwable $exception
* #return \Symfony\Component\HttpFoundation\Response
*
* #throws \Throwable
*/
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}
}
For me it was related to Kernel.php
I was adding new schedule task into the Kernel. I also updated some controllers, views, and installed twilio extension.
The error did not provide more information than
Script #php artisan package:discover handling the post-autoload-dump event returned with error code 255
#Suresh Pangeni refences the Kernel.php doc so I checked by doc that is in PROJECTFOLDER\app\Console\Kernel.php
protected $commands = [
Commands\Inspire::class,
Commands\Test::class
\App\Console\Commands\Message::class,
];
Missing Comma between Commands\Test::class and the next line didn't let me proceed. It provided no further warning or information when I ran composer dump-autoload.
Hope this can help someone else that has a similar issue!
I had the same error today, the causes are as follows:
cause 1: the env file contains space in one of the configuration.
cause 2: incorrect configuration of the Handler file belonging to the App\Exceptions namespace;
cause 3: incorrect configuration of a file inheriting ExceptionHandler
I was using Laravel 9.x and got the same error after trying to install this package maatwebsite/excel!
thanks to #samuel-terra and #dqureshiumar there is the solution worked for me:
clear bootstrap/cache:
cd bootstrap/cache/
rm -rf *.php
then run composer update:
composer update
My problem was __construct method.
composer.json
"php": "^8.1",
"laravel/framework": "^9.19",
Handler.php
The problem originates from this code:
this->container->make(FlasherInterface::class);
My solution, I removed the construction directly and the problem is solved.
public function __construct(Container $container)
{
parent::__construct($container);
$this->flasher = $this->container->make(FlasherInterface::class);
//$this->request = $this->container->get(Request::class);
}
Getting this error when my composer version 2.x then i rollback this
composer self-update --1
Now its perfectly working

Laravel running migrations on "app/database/migrations" folder recursively

So my migrations folder looks like this since I have dozens of tables it keeps things organized and clean:
migrations/
create_user_table.php
relations/
translations/
I'm trying to do a refresh all migrations and seed but it seems like I've run into a slight hiccup where I don't know the artisan command to run migrations recursively (i.e. run migrations in the relations and translations folders as well).
I've tried adding --path="app/database/migrations/*" however it spat out an error. Does anyone know the solution to this?
The only way to do it right now is to manually go through all the migrations. That is, you have to run the migration command on each of your subfolders:
php artisan migrate --path=/app/database/migrations/relations
php artisan migrate --path=/app/database/migrations/translations
However, what you can do is easily extend the artisan system to write your own migrate command that will iterate through all folders under the migrations folder, create these commands for you and run them.
You can also simply write a shell script if you don't want to get into doing this via artisan
Edit: for Laravel >= 5.0, the correct commands to migrate migration files in sub directories would be:
php artisan migrate --path=/database/migrations/relations
php artisan migrate --path=/database/migrations/translations
This add to boot method in AppServiceProvider
$mainPath = database_path('migrations');
$directories = glob($mainPath . '/*' , GLOB_ONLYDIR);
$paths = array_merge([$mainPath], $directories);
$this->loadMigrationsFrom($paths);
Now you use can php artisan migrate and also php artisan migrate:back
You can also use a wildcard, like so:
php artisan migrate --path=/database/migrations/*
You can use the following command to do this recursively:
php artisan migrate --path=/database/migrations/**/*
**/* is also known as the globstar
Before this works you must check if your bash supports the globstar.
You can do this by executing shopt and checking for globstar.
Globstar is supported by default by most server distributions but might not work on MAC.
For more on globstar see: https://www.linuxjournal.com/content/globstar-new-bash-globbing-option
In Laravel 5 the database folder sits alongside the app folder by default. So you could run this to migrate a single folders migrations:
php artisan migrate --path=/database/migrations/users
You can try this package nscreed/laravel-migration-paths. By default all sub directories inside the migrations folder will be autoloaded. Even you can add any directories very easily.
'paths' => [
database_path('migrations'),
'path/to/custom_migrations', // Your Custom Migration Directory
],
Don't need any special command just the generic: php artisan migrate will do your tasks.
I rewrote the MigrationServiceProvider:
- registerResetCommand()
- registerStatusCommand()
- registerMigrateCommand()
There you can register your own commands:
class MigrateCommand extends Illuminate\Database\Console\Migrations\MigrateCommand
After that you just need to extend youd directories:
protected function getMigrationPaths()
Or you just register the paths on application boot.
I've already done my solution before I knwewd about '$this->loadMigrationsFrom'.
A Simple solution is to create an Artisan Command for example (migrate:all),
then inside handle function define migrate command for each sub directories as mentioned bellow.
Artisan::call('migrate', [
'--path' => '/database/migrations/employee'
]);
Only relative path works for me (in Laravel 5.7):
php artisan migrate --path=database/migrations/your-folder-with-migrations
This works at laravel 8
php artisan migrate --path=database/migrations/tenant
It is a not a "direct" solution but i suggest you to look at Modularity into your laravel project.
Modules can segment your application in several smaller "folders" and bring migration, seeds, classes, routes, controllers, commands together in easily maintainable folders.
This package is a good start : https://github.com/pingpong-labs/modules
No changes are necessary, whenever you need it, use this command:
php artisan migrate --path=database/migrations/*
If you want a shortcut, you can include that in your composer.json:
"scripts": {
"migrate": [
"#php artisan migrate --force --path=database/migrations/*"
]
}
Then just use composer migrate in the terminal.
add in app/Providers/AppServiceProvider.php the path of your migration folder, you can add a string or an array
public function register()
{
$this->registerMigrations();
}
protected function registerMigrations()
{
return $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations/**/*');
}
A Simple Laravel solution is to create a gulp task (say migrate-others).
var elixir = require('laravel-elixir');
var gulp = require('gulp');
var shell = require('gulp-shell')
/*
|--------------------------------------------------------------------------
| Elixir Asset Management
|--------------------------------------------------------------------------
|
| Elixir provides a clean, fluent API for defining some basic Gulp tasks
| for your Laravel application. By default, we are compiling the Sass
| file for our application, as well as publishing vendor resources.
|
*/
elixir(function(mix) {
mix.sass('app.scss');
});
// Our Task
gulp.task('migrate-others', shell.task([
'php artisan migrate --path=/app/database/migrations/relations',
'php artisan migrate --path=/app/database/migrations/translations',
]));
Now you can simply call
gulp migrate-others
Here you go!
function rei($folder)
{
$iterator = new DirectoryIterator($folder);
system("php artisan migrate --path=" . $folder);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
echo $fileinfo->getFilename() . "\n";
rei($folder . $fileinfo->getFilename() . '/');
}
}
}
rei('./database/');

phpunit with composer not working

Background: i am on a windows host and sshing into a vagrant LAMP setup (ubuntu 13.04).
I have installed phpunit with composer using the following line in my composer.json:
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
I have then run composer update which installed phpunit. I can now navigate to /vendor/bin and see phpunit binary.
However if i type phpunit from within that directory (or anywhere elese for that matter) I get the error "phpunit is not installed"
Any suggestions as to where I go next with this - there are so few steps involved in this setup I really cant see where I could have gone wrong
So I had this issue too, for me the fix was to change in my vagrant file:
config.vm.synced_folder "C:/dev/vm/share", "/var/www/", mount_options: ['dmode=777','fmode=666']
to
config.vm.synced_folder "C:/dev/vm/share", "/var/www/", mount_options: ['dmode=777','fmode=777']
There is a lot of advise saying 666 is permissive enough, but in my case it was not and as this is only a development machine the security implications are not too important.
./phpunit from the bin directory.
It's just not in your path.
I created the script above:
#!/bin/bash
binary=$1
shift
dir=$PWD
for i in $( seq 5 )
do
if [ -e "$dir/composer.json" ]
then
$dir/vendor/bin/$binary $#
else
echo "not found phpunit in $dir"
fi
dir="$dir/.."
done
If you name it 'composer.exec', and put it n your path, you can call it:
composer.exec phpunit [phpunit-options]
And there is it! Calling phpunit or any other bin contained in vendor/bin.

Categories