Laravel RefreshDatabase doesn't work in testing - php

In order to move my current Laravel 5.5 project to the latest version, I was trying to setup some tests (I've never done that before).
To do so, I added <env name="DB_CONNECTION" value="mysql_testing" /> in the phpunit.xml, under the <php> section, and created a copy of the current mysql configuration under config/database.php naming it mysql_testing, which has 'database' => env('DB_NAME_TESTING', 'forge') .
In the .env file I put DB_NAME_TESTING=testDB. The database exists, is empty and the user has all permissions on it.
I haven't created any Test, yet, just tried the Examples provided by Laravel. If I run the tests as they are, they all pass. If I add the RefreshDatabase trait on them, instead, I get
ErrorException : Trying to access array offset on value of type int
C:\[path to project]\vendor\symfony\console\Input\ArrayInput.php:135
which corresponds to } elseif ('-' === $key[0]) {.
After a bit of tracing, I found out that the $key is 'migrate:fresh', which is the Artisan command that RefreshDatabase is trying to run. In later versions this segment has been replaced with } elseif (str_starts_with($key, '-')) {, but this is only available since PHP 8, and I'm currently stuck with PHP 7.4.
My question now is: what am I doing wrong in setting up my testing database?

You cannot use Laravel 9 using PHP 7.4. Take a look at Support policy - max version for PHP 7.4 is Laravel 8 so either use newer PHP version or upgrade to Laravel 8 and not Laravel 9.

Related

Getting error for artisan commands ( In routes-v7.php Call to undefined method Closure::__set_state() )

I have upgraded Laravel version from 5.7 to 8.34. I'm getting this error for all artisan commands.
To others: removing the routes-v7 might help, but it's possibly caused due to a return in your routes file (web.php). Remove it and use a controller to return a specific view/back() etc.
Call to undefined method Closure::__set_state()
In my case, I delete the bootstrap/cache/config.php file and run the PHP artisan vendor: publish but the same error occurs in the terminal.
Solution:-
1 Delete the routes-v7.php under the bootstrap folder path(bootstrap/cache/routes-v7.php) .
2 Run the PHP artisan optimize Command. then they recreate the deleted file in the repository.
Laravel 8 && Laravel 9
Please Support !!!!
https://stackoverflow.com/users/16749364/hitesh-sharma
#Tjab answer is the correct one. Just a few more thoughts:
It's because you're using Closures (a.k.a. Lambda functions function () use() {}) in your configuration. I'm pretty sure it's because you're using HTTP redirect routes. DON'T USE THAT FEATURE, since you won't be able to cache your routes, which is actually a good idea because it should increase your application speed. Otherwise your config can't be serialized (what caching actually means) to disk.
Instead create controller and action for each redirect.
Also check your configuration files for Closures and remove them (same for all other PHP frameworks).

Laravel 5.7: Class setQueue does not exist

Getting this error when trying to send a simple email:
Class setQueue does not exist at vendor/laravel/framework/src/Illuminate/Container/Container.php:779)
Here is the code causing it:
Mail::raw($data['email_body'], function (Message $message) use ($email_or_url, $data) {
$message->to($email_or_url);
$message->subject($data['email_subject']);
});
Why would Laravel be trying to queue this and how do I make it stop?
bug case in laravel project for this issue. https://github.com/laravel/framework/issues/26819
It turned out to be a problem in php. Laravel has patched it so you have 2 options to fix. Use the patch version and hope there aren't other places php 7.3 breaks laravel, or downgrade your project to 7.2
I am also facing the same issue in MacBook Pro. For MacBook Users just use command
"valet restart"
In my case, it's working now. Try to restart your server that might be helpful.
Note: I am using PHP 7.3.*
Make sure you rename your QUEUE_DRIVER environment variable to QUEUE_CONNECTION in both config/queue.php and .env - it was renamed in 5.7
https://laracasts.com/discuss/channels/laravel/trying-to-send-email-class-setqueue-does-not-exist
Why would Laravel be trying to queue this and how do I make it stop?
It's not trying to queue this email, but MailServiceProvider still registers a queue driver (for when an email implements the ShouldQueue interface).
it happens to me because i was running php 7.3, when i downgrade to php 7.2.13 it works, and i also change the QUEUE_DRIVER variable in .env and config/queue.php
What I've found by now in my research for the solution is that in my case it is not about the php version (I even reinstalled everything and changed my servers providers because of the laracasts answers) but it was a code caused error, and I know because i reverted to previous commit and it worked fine.
In my case it was because I had a transaction before the email sending
$suma = $user->transactionsAsUser->filter(
function ($transaction) {
return $transaction->status == 'successful' || $transaction->kanjea_ammount < 0;
}
)->sum('kanjea_ammount');
$user->kanjea_balance = $suma;
$user->save();
\Mail::to($user->email)->send(new \App\Mail\JustRegistered($user));
When I removed that fragment before the last line, it just worked fine. Even using PHP 7.3.
By the way. this problem doesn't occur in my local environment w7+xampp, but only dokku
My solution was to valet install again as I got the error after running brew update/upgrade
related details:
PHP 7.3.0 (cli) (built: Dec 7 2018 11:00:11) ( NTS )

Logger does not work anymore after upgrade from Laravel 5.5

Since I've upgraded to Laravel version 5.6 from Laravel version 5.5 my Logger doesn't work properly anymore.
At first I got the following error stack :
laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (InvalidArgumentException(code: 0): Log [] is not defined. at /home/vagrant/Code/grotesmurf/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:181)
which was solved by simply adding the new config/logging.php file that is provided by Laravel 5.6.
But now I'm getting no output from the Logger! I'm simply running \Log::info('hello!') as a tinker command, but it doesn't generate any log output anymore (same for scripts calling the \Log() method).
I've tried different LOG_CHANNEL settings (daily, single, stack), but none of these work.
Hope anyone has ran into this error already and is able to provide me with some suggestions. I have followed the upgrade guide and it doesn't help unfortunately.
Thanks in advance.
P.S. I'm running php version 7.1 & am on ubuntu.
P.P.S. I have cleared all cached config using artisan.
Well I have found the actual problem, we use an adjusted storage_path() method in our application and the new Logger is now using the storage_path() method to generate its path - this caused the log file to be created in a different directory than storage/logs.
i had the same issue, deleting the files in bootstrap/cache solved it.

laravel artisan app:name resulted in syntax errors

I am using Laravel 5 on a shared host. I have installed it ok. However on running:
/usr/bin/php55-cli artisan app:name 247
the laravel install broke. I tracked it down to app.php and the new name caused syntax errors. I got it working again by using Linux to do a global replace changing it back from 247 to App.
Does anyone know why? And how can I get artisan to work properly?
Thanks
This is not Laravel nor artisan issue, but plain PHP syntax error.
PHP does not allow variable or class name to start with an integer. Same goes to namespaces. So your error comes from you app name being 247. First letter can not be an integer.

CakePHP 3.0 not running on other machines

I've developed a small project on a machine, using CakePHP 3.0, and I need it to run on another machine. I've tried to install it on several other machines.
If I run the composer to install the CakePHP 3.0, then I copy my stuff to overwrite it, the project works. I've tried this on two machines and had no problem so far. If I don't run the composer, and just copy the stuff to the target machine, it gives me the following error. I've tried this on 3 machines, and every machine gives me this:
Fatal error: Class 'Locale' not found in /home/u113681897/public_html/vendor/cakephp/cakephp/src/I18n/I18n.php on line 229
Fatal error: Class 'Locale' not found in /home/u113681897/public_html/vendor/cakephp/cakephp/src/I18n/I18n.php on line 229
I've copied the whole project to this server to test.
I told you this because I thought it has something to do with my problem. The point is that I have to run this on a machine that is not mine, and I can't install composer on it. The /public_html/vendor/cakephp/cakephp/src/I18n/ has files related to internationalization and localization, but my project will never be translated, so a workaround to make the project ignore those files would be enough to solve my problem.
The following code is an excerpt from the (...)/I18n/I18n.php that might be relevant:
<?php
namespace Cake\I18n;
use Aura\Intl\FormatterLocator;
use Aura\Intl\PackageLocator;
use Aura\Intl\TranslatorFactory;
use Cake\I18n\Formatter\IcuFormatter;
use Cake\I18n\Formatter\SprintfFormatter;
use Locale;
class I18n {
// lots of code here
public static function defaultLocale() {
if (static::$_defaultLocale === null) {
static::$_defaultLocale = Locale::getDefault() ?: 'en_US';
// the line above is the Line 229
}
return static::$_defaultLocale;
}
// many code here too
}
I've checked that another file also tries to access this Locale class, but I don't know if there are other files trying to access it as well. Many files from everywhere inside the project tries to access methods from I18n.php. I need it running but I can't figure out how to make it run.
Any help will be greatly appreciated.
As I just found out, prior to CakePHP 3.0, the installation must be done by composer, as stated in the 3.0 migration guide:
CakePHP should be installed with Composer
Since CakePHP can no longer easily be installed via PEAR, or in a shared
directory, those options are no longer supported. Instead you should use
Composer to install CakePHP into your application.
So it won't run on regular free web hosting services.

Categories