将 PHPUnit 与 Phake 一起使用时如何修复 Symfony 5 中的弃用通知?
How to fix deprecation notices in Symfony 5 when using PHPUnit with Phake?
我使用 Symfony 5 开发我的应用程序并使用 PHPUnit 测试它,同时使用 Phake 来模拟我的 类。我刚刚将 Symfony 从 5.2 更新到 5.3,包括 symfony/phpunit-bridge 以及食谱。在symfony.lock
中,phpunit从4.7版本跳到了9.3(在composer.json
中版本是^9.5)。
现在,当我 运行 我的测试(我以前没有)时,我收到以下消息:
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
在我看来,弃用是因为 Phake 正在模拟 类 并因此从 类 扩展而没有在应该的时候实现方法,并在应该的时候实现它们't.
所以我猜实际上不是我的代码被弃用了。我说的对吗?
如果是这样,我如何在不禁用它们的情况下修复这些弃用?
那是 symfony/phpunit-bridge 问题、PHPUnit 问题还是 Phake 问题?
编辑: 因为我得到了一个 -1 而没有评论告诉我我的 post 有什么问题,我会尝试添加一些精确度。
我在 post 提出这个问题之前做了一些研究:我没有发现任何与此类弃用通知和 Phake 相关的内容。
我知道我的代码不包含弃用,因为除了这些我没有任何弃用通知。
这是我来自 ContactMailerTest 的代码,如果可以的话:
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);
}
}
来自 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());
}
}
编辑 2: 我安装了 symfony/phpunit-bridge 包。
编辑 3: 我已经 post 在 phake/phake 存储库上编辑了一个问题:https://github.com/phake/phake/issues/300
这个问题实际上是一个 Symfony 问题,已在 this PR 中解决。在 Symfony 发布时将其更新到最新的次要版本将解决此问题。
我使用 Symfony 5 开发我的应用程序并使用 PHPUnit 测试它,同时使用 Phake 来模拟我的 类。我刚刚将 Symfony 从 5.2 更新到 5.3,包括 symfony/phpunit-bridge 以及食谱。在symfony.lock
中,phpunit从4.7版本跳到了9.3(在composer.json
中版本是^9.5)。
现在,当我 运行 我的测试(我以前没有)时,我收到以下消息:
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
在我看来,弃用是因为 Phake 正在模拟 类 并因此从 类 扩展而没有在应该的时候实现方法,并在应该的时候实现它们't.
所以我猜实际上不是我的代码被弃用了。我说的对吗?
如果是这样,我如何在不禁用它们的情况下修复这些弃用? 那是 symfony/phpunit-bridge 问题、PHPUnit 问题还是 Phake 问题?
编辑: 因为我得到了一个 -1 而没有评论告诉我我的 post 有什么问题,我会尝试添加一些精确度。
我在 post 提出这个问题之前做了一些研究:我没有发现任何与此类弃用通知和 Phake 相关的内容。
我知道我的代码不包含弃用,因为除了这些我没有任何弃用通知。
这是我来自 ContactMailerTest 的代码,如果可以的话:
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);
}
}
来自 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());
}
}
编辑 2: 我安装了 symfony/phpunit-bridge 包。
编辑 3: 我已经 post 在 phake/phake 存储库上编辑了一个问题:https://github.com/phake/phake/issues/300
这个问题实际上是一个 Symfony 问题,已在 this PR 中解决。在 Symfony 发布时将其更新到最新的次要版本将解决此问题。