yiisoft / yii2-queue

Yii2 Queue Extension. Supports DB, Redis, RabbitMQ, Beanstalk and Gearman
BSD 3-Clause "New" or "Revised" License
1.07k stars 295 forks source link

Running functional Test fails with: [Exception] Serialization of 'Closure' is not allowed #401

Closed EvilKarter closed 3 years ago

EvilKarter commented 3 years ago

Hi, I am not sure if this is a Problem of yii2-queue or codeseption. I am sending all my mails with the help of your queue-extension. For example my sendForgotPasswordMail function:

public function sendForgotPasswordMail(User $user): bool { $message = Yii::$app->mailer->compose([ html' => 'passwordResetToken-html', text' => 'passwordResetToken-text', ], ['user' => $user]) ->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->name . ' robot']) ->setTo($this->email) ->setSubject(Yii::$app->name . ' ' . Yii::t('app', 'forgot password')); $queueId = Yii::$app->queue->push(new MailJob([ 'message' => $message, ])); return 0 < $queueId; }

The application works quite fine this why. But if I am running my functional tests I always get this Error: [Exception] Serialization of 'Closure' is not allowed

1 D:\wamp64\www\jobquick\www\framework\vendor\yiisoft\yii2-queue\src\serializers\PhpSerializer.php:24

2 D:\wamp64\www\jobquick\www\framework\vendor\yiisoft\yii2-queue\src\Queue.php:195

3 D:\wamp64\www\jobquick\www\framework\backend\models\ForgotPasswordForm.php:47

Line 47 of ForgotPasswordForm is 'message' => $message, from example above

I know that PHP cannot serialize anonmyous functions. But why is this code working on all environments, but the tests are failing?

Additional info

Q A
Yii version 2.0.39.2
PHP version 7.3.5
Codeception PHP Testing Framework 4.1.12
PHPUnit 8.5.9
yii-bot commented 3 years ago

Thanks for posting in our issue tracker. In order to properly assist you, we need additional information:

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

rob006 commented 3 years ago

You probably have some closure in your app config for functional tests - Message holds reference to mailer, so I would check there first.

But in general you should avoid passing instance of Message to job, since it may contain references to other parts of app and you may end up with serializing more than you expect.

samdark commented 3 years ago

Likely it can't be solved on Yii side.

EvilKarter commented 3 years ago

ok i have solved my Problem: I have unset its public $mailer property and reattach it when it is unserialized.