我最近在学习 Symfony 3 框架和依赖注入(inject)。
我希望你能帮我解决我对在 Symfony 3 中使用 PHPUnit 测试 Services 的方法的疑惑。我有些担心如何以正确的方式做到这一点。
让我们做一个Service类的例子:
// src/AppBundle/Services/MathService.php
namespace AppBundle\Services;
class MathService
{
public function subtract($a, $b)
{
return $a - $b;
}
}
我看到通常 Symfony 中的 UnitTest 类会测试 Controllers。
但是,除了Controllers,我还能测试什么独立类,比如Services(例如包含业务逻辑)?
我知道至少有两种方法可以做到:
1. 创建一个扩展 PHPUnit_Framework_TestCase
的测试类并创建服务对象在此测试类中的一些方法或构造函数中(与 Symfony 文档中关于 testing 完全一样)
// tests/AppBundle/Services/MathTest.php
namespace Tests\AppBundle\Services;
use AppBundle\Services\MathService;
class MathTest extends \PHPUnit_Framework_TestCase
{
protected $math;
public function __construct() {
$this->math = new MathService();
}
public function testSubtract()
{
$result = $this->math->subtract(5, 3);
$this->assertEquals(2, $result);
}
}
2. 使用依赖注入(inject)将我们的服务类作为服务容器。然后创建一个扩展 KernelTestCase
的测试类以访问内核。它将使我们能够从内核使用容器注入(inject)我们的服务(基于关于 testing Doctrine 的 Symfony 文档)。
服务容器的配置:
# app/config/services.yml
services:
app.math:
class: AppBundle\Services\MathService
现在我们的测试类将如下所示:
// tests/AppBundle/Services/MathTest.php
namespace Tests\AppBundle\Services;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class MathTest extends KernelTestCase
{
private $math;
protected function setUp()
{
self::bootKernel();
$this->math = static::$kernel
->getContainer()
->get('app.math');
}
public function testSubtract()
{
$result = $this->math->subtract(5, 3);
$this->assertEquals(2, $result);
}
}
我们选择这种方式是有好处的。
首先,我们可以通过依赖注入(inject)在 Controller 和测试中访问我们的服务容器。
其次 - 如果将来我们想要更改服务类的位置或更改类的名称 - 与1. 案例 - 我们可以避免更改许多文件,因为我们至少会在 services.yml
文件中更改路径/名称。
我的问题:
还有其他方法可以在 Symfony 3 中测试服务类吗?应该采用哪种方式更好?
最佳答案
2018 年更新了棘手的 Symfony 3.4/4.0 解决方案。
这种方法with all its pros/cons is described in this post with code examples .
访问私有(private)服务的最佳解决方案是添加一个使所有服务公开以供测试的编译器 channel 。
1。更新内核
use Symfony\Component\HttpKernel\Kernel;
+use Symplify\PackageBuilder\DependencyInjection\CompilerPass\PublicForTestsCompilerPass;
final class AppKernel extends Kernel
{
protected function build(ContainerBuilder $containerBuilder): void
{
$containerBuilder->addCompilerPass('...');
+ $containerBuilder->addCompilerPass(new PublicForTestsCompilerPass());
}
}
2。需要或创建自己的编译器通行证
PublicForTestsCompilerPass
看起来像:
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
final class PublicForTestsCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $containerBuilder): void
{
if (! $this->isPHPUnit()) {
return;
}
foreach ($containerBuilder->getDefinitions() as $definition) {
$definition->setPublic(true);
}
foreach ($containerBuilder->getAliases() as $definition) {
$definition->setPublic(true);
}
}
private function isPHPUnit(): bool
{
// defined by PHPUnit
return defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__');
}
}
要使用这个类,只需通过以下方式添加包:
composer require symplify/package-builder
当然,更好的方法是使用自己的类,以满足您的需求(您将 Behat 用于测试等)。
然后您所有的测试将继续按预期工作!
关于php - 使用 PHPUnit 在 Symfony3 中测试服务/服务容器的优雅方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40687198/