微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony 4测试

如何解决Symfony 4测试

我想将ParameterBagInterface和EntityManagerInterface注入我的单元测试(WebTestCase和KernelTestCase)中,但是我找不到能正确返回其名称空间和名称方法Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface)。有办法吗?

我尝试过的是:

$this->parameterBag = self::$container->get(ParameterBagInterface::class);

返回Symfony\Component\DependencyInjection\ParameterBag\ContainerBag

$this->parameterBag = $this->prophesize(ParameterBagInterface::class)->reveal();

返回Double\ParameterBagInterface\P1

$this->parameterBag = $this->createMock(ParameterBagInterface::class);

返回Mock_ParameterBagInterface_fccf09f9

我所有的类都使用ParameterBagInterface并按类型进行提示

这是示例测试类:

/**
 *
 * @package App\Tests\Entity
 */
class LogCollectTest extends WebTestCase
{
    use CronManagerCron;

    /**
     * @var EntityManager
     */
    private $em;

    /**
     * {@inheritDoc}
     */
    protected function setUp()
    {
        self::bootKernel();
        $this->parameterBag = self::$container->get(ParameterBagInterface::class);
    }

    /**
     * Test saving click
     */
    public function testSavingClick()
    {
        // truncate the log collect table to be sure to get the right click
        $this->truncateLogCollectTable();

        $userAgents = [...];

        foreach ($userAgents as $agent => $expectedResult) {
            // we make fake client requests and record them in database (test enviropment)
            $clientStatus = $this->sendClientData($agent);

            // the controller is resulting properly
            $this->assertEquals(200,$clientStatus);

            /**
             * @var LogCollect $logCollectEntry
             */
            $logCollectEntry = $this->em->getRepository(LogCollect::class)->getLast(); <-- 
        ...
        // later we process this client requests with cron and later assert the data
        $logCollectorCron = new LogCollectorCron(
            $this->container,$this->em,$this->parameterBag,'test'
        );
        $logCollectorCron->run();
        ...
   }

有什么建议吗?

解决方法

您将不会获得任何接口,因为从本质上说,无法实例化接口,因此请注意:ParameterBagInterface对象不存在。 当您要求容器为您提供ParameterBagInterface时,容器将为您提供实现此接口的服务。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。