How to fix deprecation notices in Symfony 5 when using PHPUnit with Phake?

Viewed 1591

I develop my app with Symfony 5 and test it with PHPUnit, along with Phake to mock my classes. I've just updated Symfony from 5.2 to 5.3, including symfony/phpunit-bridge as well as the recipes. In symfony.lock, phpunit jumped from version 4.7 to 9.3 (in composer.json version is ^9.5).

Now, I have the following messages when I run my tests (I didn't have them before):

Remaining indirect deprecation notices (3)

  1x: The "Symfony\Bridge\Twig\Mime\TemplatedEmail::__serialize()" method is considered internal. It may change without further notice. You should not extend it from "TemplatedEmail_PHAKEcbeb592e8143b41".
    1x in ContactMailerTest::testSendEmail from App\Tests\Mailer

  1x: The "Symfony\Bridge\Twig\Mime\TemplatedEmail::__unserialize()" method is considered internal. It may change without further notice. You should not extend it from "TemplatedEmail_PHAKEcbeb592e8143b41".
    1x in ContactMailerTest::testSendEmail from App\Tests\Mailer

  1x: Class "TranslatorInterface_PHAKEe9b1bfdc40995c1" should implement method "Symfony\Contracts\Translation\TranslatorInterface::getLocale()": Returns the default locale.
    1x in EmailFactoryTest::testCreateEmail from App\Tests\Mailer

It seems to me that the deprecation comes from the fact that Phake is mocking the classes and thus extending from the classes without implementing a method when it should, and implementing them when it shouldn't.

So I'm guessing that it's not actually my code that is deprecated. Am I right?

If so, how could I fix these deprecations without disabling them? Is that a symfony/phpunit-bridge problem, a PHPUnit problem, or a Phake problem?


Edit: Since I got a -1 without a comment to tell me what is wrong with my post, I'll try to add some precisions.

I have made some research prior to posting this question: I haven't found anything related to this type of deprecation notices and Phake.

I know that my code doesn't contain deprecations because I don't have any deprecation notices other than these.

Here's my code from ContactMailerTest if that can help:

final class ContactMailerTest extends TestCase
{
    private MailerInterface $mailer;
    private EmailFactory $factory;
    private LoggerInterface $logger;
    private ContactMailer $contactMailer;
    private Contact $contact;
    private TemplatedEmail $email;

    protected function setUp(): void
    {
        $this->mailer = Phake::mock(MailerInterface::class);
        $this->factory = Phake::mock(EmailFactory::class);
        $this->logger = Phake::mock(LoggerInterface::class);
        $this->contact = Phake::mock(Contact::class);
        $this->contact->name = 'Jane';
        $this->contact->emailAddress = 'jane@example.com';
        $this->contact->message = 'Cat ipsum dolor sit amet !';
        $this->email = Phake::mock(TemplatedEmail::class);

        Phake::when($this->factory)->createEmail($this->contact->name, $this->contact->emailAddress, $this->contact->message)->thenReturn($this->email);

        $this->contactMailer = new ContactMailer($this->mailer, $this->factory, $this->logger);
    }

    public function testSendEmail(): void
    {
        $this->contactMailer->sendEmail($this->contact);

        Phake::verify($this->factory)->createEmail('Jane', 'jane@example.com', 'Cat ipsum dolor sit amet !');
        Phake::verify($this->mailer)->send($this->email);
        $this->assertTrue($this->contactMailer->hadSuccess);
    }
}

and from EmailFactoryTest:

final class EmailFactoryTest extends TestCase
{
    public function testCreateEmail(): void
    {
        $translator = Phake::mock(TranslatorInterface::class);
        $name = 'Jane';
        $emailAddress = 'jane@example.com';
        $message = 'Cat ipsum dolor sit amet !';

        Phake::when($translator)->trans('email.contact.address')->thenReturn('contact@example.com');
        Phake::when($translator)->trans('email.contact.name')->thenReturn('Contact');
        Phake::when($translator)->trans('email.chloe.address')->thenReturn('chloe@example.com');
        Phake::when($translator)->trans('email.chloe.name')->thenReturn('Chloé');
        Phake::when($translator)->trans('email.contact.subject', ['name' => $name])->thenReturn('Message de la part de '.$name);

        $factory = new EmailFactory($translator);
        $email = $factory->createEmail($name, $emailAddress, $message);

        $from = $email->getFrom()[0];
        $this->assertEquals('contact@example.com', $from->getAddress());
        $this->assertEquals('Contact', $from->getName());

        $to = $email->getTo()[0];
        $this->assertEquals('chloe@example.com', $to->getAddress());
        $this->assertEquals('Chloé', $to->getName());

        $replyTo = $email->getReplyTo()[0];
        $this->assertEquals('jane@example.com', $replyTo->getAddress());
        $this->assertEquals('Jane', $replyTo->getName());

        $this->assertEquals('Message de la part de Jane', $email->getSubject());
        $this->assertEquals(['message' => 'Cat ipsum dolor sit amet !'], $email->getContext());
        $this->assertNotNull($email->getHtmlTemplate());
    }
}

Edit 2: I have symfony/phpunit-bridge package installed.


Edit 3: I've posted an issue on phake/phake repository right there: https://github.com/phake/phake/issues/300

1 Answers

This problem was actually a Symfony problem and has been resolved in this PR. Updating Symfony to the latest minor version when it is released will fix the issue.

Related