简体   繁体   English

如何在 Symfony 中扩展 WebTestCase

[英]How to extend WebTestCase in Symfony

I want to write functional tests in symfony but I am unable to over-ride createKernel method of WebTestCase.我想在 symfony 中编写功能测试,但我无法覆盖 WebTestCase 的createKernel方法。 Our application integrated symfony by using an HttpKernel like this :我们的应用程序通过使用 HttpKernel 像这样集成了 symfony:

$kernel = new HttpKernel($dispatcher, $controllerResolver, new 
RequestStack(), $argumentResolver);

$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();

$kernel->terminate($request, $response);

I tried returning this kernel from over-ridden createKernel method but it doesn't have some methods(like getContainer() ) which gives error later on.我尝试从createKernel方法返回这个内核,但它没有一些方法(如getContainer() )稍后会出错。 How can I create my own kernel for WebTestCase?如何为 WebTestCase 创建自己的内核?

please see documentation testing (symfony docs)请参阅文档测试(symfony 文档)

To run your functional tests, the WebTestCase class needs to know which is the application kernel to bootstrap it.要运行您的功能测试,WebTestCase 类需要知道哪个是应用程序内核来引导它。 The kernel class is usually defined in the KERNEL_CLASS environment variable (included in the default phpunit.xml.dist file provided by Symfony):内核类通常定义在 KERNEL_CLASS 环境变量中(包含在 Symfony 提供的默认 phpunit.xml.dist 文件中):

<?xml version="1.0" charset="utf-8" ?>
<phpunit>
    <php>
        <!-- the value is the FQCN of the application kernel -->
        <env name="KERNEL_CLASS" value="App\Kernel" />
    </php>
    <!-- ... -->
</phpunit>

Also, you have phpunit runnable in your bin directory and phpunit.xml.dist file in app directory.此外,您的 bin 目录中有 phpunit runnable,app 目录中有 phpunit.xml.dist 文件。 usually you can run tests by bin/phpunit -c app path_to_the_tests通常你可以通过bin/phpunit -c app path_to_the_tests运行测试

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

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