首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony 4测试

Symfony 4测试
EN

Stack Overflow用户
提问于 2020-09-13 02:13:44
回答 1查看 127关注 0票数 0

我想在我的单元测试中注入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,并且都是类型提示的。

下面是测试类示例:

代码语言:javascript
复制
/**
 *
 * @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();
        ...
   }

有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2020-09-13 04:03:56

你不会得到任何接口,因为接口本质上是永远不能实例化的,所以需要说明的是: ParameterBagInterface对象不能存在。当您请求容器为您提供ParameterBagInterface时,容器将为您提供一个实现此接口的服务。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63863485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档