简体   繁体   English

Symfony2在单元测试中获得配置参数

[英]Symfony2 getting config params in unit test

I have a Symfony2 service that is constructed using a parameter from config.yml using dependency injection. 我有一个Symfony2服务,使用依赖注入使用config.yml中的参数构造。 I'm now trying to unit test it and find that the unit test does not have access to the container and therefore the service. 我现在正在尝试对其进行单元测试,并发现单元测试无法访问容器,因此无法访问服务。 So I should built one myself using mock data. 所以我应该使用模拟数据自己构建一个。 It would make sense to me if I could now read the config parameter (going first to config_test.yml then config.yml etc etc) but it appears that isn't possible either. 如果我现在可以读取config参数(首先是config_test.yml然后是config.yml等等),这对我来说是有意义的,但似乎也不可能。 This appears to make unit testing a service cumbersome as I would need to code the initialisation parameters into the test instead of the config files. 这似乎使单元测试服务变得麻烦,因为我需要将初始化参数编码到测试而不是配置文件中。

If there really is no way to construct a service with parameters from config.yml during a unit test, does anyone know the logic as to why it is a Bad Thing™? 如果在单元测试期间确实无法使用config.yml中的参数构建服务,那么有没有人知道它为什么是Bad Thing™的逻辑?

This works for me: 这对我有用:

class MyServiceTest extends WebTestCase
{
    public function testCookies()
    {
        $client = static::createClient();

        $myParams = $client->getKernel()->getContainer()->getParameter('my_params');
    }
}

I found this post, because I needed config parameters in my tests myself. 我找到了这篇文章,因为我自己在测试中需要配置参数。 This was the first hit on Google. 这是谷歌的第一次热播。

However, this is a solution which works. 但是,这是一个有效的解决方案。 There might be better ones. 可能会有更好的。

<?php

...

require_once(__DIR__ . "/../../../../../app/AppKernel.php");

class MediaImageTest extends WebTestCase
{
    private $_application;
    private $storagePath;

    public function setUp() {  
         $kernel = new \AppKernel('test', true);
         $kernel->boot();
         $this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
         $this->_application->setAutoExit(false)

         $this->storagePath = $this->_application->getKernel()->getContainer()->getParameter('media_path');
    }

    ...
}

You might look in to this too: Access Symfony 2 container via Unit test? 你也可以看一下: 通过单元测试访问Symfony 2容器? Is a much cleaner solution accessing the kernel within unit tests. 在单元测试中访问内核是一个更清洁的解决方案。

Unit testing is about testing a class in isolation from other classes. 单元测试是关于与其他类隔离测试类。 To unit test your service you shouldn't need to read anything from the configuration. 要对您的服务进行单元测试,您不需要从配置中读取任何内容。 Just pass a value in your test. 只需在测试中传递一个值即可。 In the end, it could potentially work with other values, right? 最后,它可能与其他值一起工作,对吗?

Of course if there's some logic/validation around the accepted values, you should probably cover it by tests. 当然,如果在可接受的值周围有一些逻辑/验证,您应该通过测试来覆盖它。 Think how you'd do it if you were taking this value from the configuration. 如果从配置中获取此值,请考虑如何执行此操作。 You simply wouldn't be able to test the behaviour with different values. 您根本无法使用不同的值测试行为。

If you want to verify if your application is working correctly (your classes collaborate the way you expect), use functional tests or an acceptance testing tool (like Behat). 如果要验证应用程序是否正常工作(您的类以您期望的方式协作),请使用功能测试或验收测试工具(如Behat)。

I'm using Symfony 3.2.2, but I think that this could work also for you. 我正在使用Symfony 3.2.2,但我认为这对你也有用。

It's simply a line: 这只是一条线:

$export_dir = static::$kernel->getContainer()->getParameter('export_dir');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM