[英]How to test Symfony standalone bundle
I have developped some bundles ie https://github.com/975L/EmailBundle and I want to add tests for them. 我已经开发了一些捆绑软件,例如https://github.com/975L/EmailBundle ,我想为它们添加测试。 So, I have created the file Tests/Service/EmailServiceTest.php
that contains one test. 因此,我创建了包含一个测试的文件Tests/Service/EmailServiceTest.php
。
I have installed phpunit using sudo apt install phpunit
on my Ubuntu computer, but when I cd
to my bundle's folder and run phpunit
I have the following error: 我已经在我的Ubuntu计算机上使用sudo apt install phpunit
,但是当我cd
到捆绑包的文件夹并运行phpunit
,出现以下错误:
1) c975L\\EmailBundle\\Tests\\Service\\EmailServiceTest::testCreateEmailObject Error: Class 'c975L\\EmailBundle\\Service\\EmailService' not found 1)c975L \\ EmailBundle \\ Tests \\ Service \\ EmailServiceTest :: testCreateEmailObject错误:未找到类'c975L \\ EmailBundle \\ Service \\ EmailService'
It looks like the autoload can't find the class, even if declared at the top with use
. 即使use
在顶部声明,自动加载看起来也找不到该类。
I saw some solutions over the web that requires to create a bootstrap.php
with autoload but even with this, it doesn't work... 我在网上看到了一些解决方案,这些解决方案要求使用自动加载功能创建bootstrap.php
,但是即使这样做,它也无法正常工作...
So my question is how do we test a standalone Symfony Bundle with phpunit? 所以我的问题是我们如何使用phpunit测试独立的Symfony捆绑包?
It is a good idea to install phpunit on a per project basis, as version differences could come up if you have multiple projects. 最好在每个项目的基础上安装phpunit,因为如果您有多个项目,则版本差异可能会出现。
(1) composer require phpunit/phpunit --dev
(1) composer require phpunit/phpunit --dev
I saw some solutions over the web that requires to create a bootstrap.php with autoload but even with this, it doesn't work... 我在网上看到了一些解决方案,这些解决方案要求使用自动加载功能创建bootstrap.php,但是即使这样做,它也无法正常工作...
You can use composer's autoloader as a bootstrap. 您可以使用作曲家的自动加载器作为引导程序。 A custom bootstrap might come in handy in some cases, but not necessary. 自定义引导程序在某些情况下可能会派上用场,但不是必需的。
(2) Create a phpunit.xml.dist
file, something like this (your current one does not specify a bootstrap): (2)创建一个phpunit.xml.dist
文件,如下所示(您当前的文件未指定引导程序):
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="MyBundle test suite">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Also check out vendor/bin/phpunit --generate-configuration
which is nice. 还请检查vendor/bin/phpunit --generate-configuration
这很好。
(3) Run vendor/bin/phpunit
(3)运行vendor/bin/phpunit
It should find the config file automatically, but you can also specify it with the --configuration
option 它应该会自动找到配置文件,但是您也可以使用--configuration
选项指定它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.