为 Symfony 包创建功能测试

问题描述 投票:0回答:1

我创建了一个 Symfony 捆绑包,它允许我跨多个应用程序管理用户/权限/配置文件。该捆绑包提供了一组路线、表格、服务等。

我已经有验证应用程序功能的单元测试,但我想添加功能测试,通过直接测试我的路由来验证应用程序的整体行为。

我该怎么做?知道我没有内核,似乎不可能直接测试捆绑包。在 /tests 文件夹中,我创建了一个 /tests/TestApplication 文件夹,其中包含一个空白项目。我不确定我是否走在正确的道路上。

是否应该避免对捆绑包进行功能测试?您知道有关此主题的任何好的技术文档吗?我已经遵循了本教程,但不幸的是,它在这个主题上还不够深入:https://symfonycasts.com/screencast/symfony-bundle

祝你有美好的一天!

symfony testing bundle
1个回答
1
投票

当我为捆绑包创建集成或功能测试以测试它如何与其他组件配合使用时,我创建一个测试应用程序:

首先,您需要通过 Composer 安装所需的组件作为开发要求。

接下来,您可以创建一个测试内核:

<?php

declare(strict_types=1);

namespace Tests\VarLabIT\LexofficeBundle\Integration\TestApp;

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
use VarLabIT\LexofficeBundle\VarLabITLexofficeBundle;

class TestAppKernel extends Kernel
{
    public function registerBundles(): iterable
    {
        $bundles = [];

        if ('test' === $this->getEnvironment()) {
            $bundles[] = new FrameworkBundle();
            $bundles[] = new VarLabITLexofficeBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader): void
    {
        $loader->load(__DIR__ . '/config/config_test.yml');
    }

    public function getCacheDir(): string
    {
        return \sys_get_temp_dir() . '/VarLabITLexofficeBundle/cache';
    }

    public function getLogDir(): string
    {
        return \sys_get_temp_dir() . '/VarLabITLexofficeBundle/logs';
    }
}

(来自 https://github.com/var-lab-it/lexoffice-bundle/blob/main/tests/Integration/TestApp/TestAppKernel.php

然后,您可以在测试用例中使用 TestKernel:

<?php

declare(strict_types=1);

namespace Tests\VarLabIT\LexofficeBundle\Integration\Tests;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Tests\VarLabIT\LexofficeBundle\Integration\TestApp\TestAppKernel;
use VarLabIT\LexofficeBundle\LexofficeClient;

class ConfigurationTest extends KernelTestCase
{
    protected ContainerInterface $container;

    protected function setUp(): void
    {
        parent::setUp();

        $kernel = new TestAppKernel('test', true);
        $kernel->boot();
        $this->container = $kernel->getContainer();
    }

    public function testConfiguration(): void
    {
        $apiKey      = $this->container->getParameter('var_lab_it_lexoffice.api_key');
        $apiEndpoint = $this->container->getParameter('var_lab_it_lexoffice.api_endpoint');
        $apiVersion  = $this->container->getParameter('var_lab_it_lexoffice.api_version');

        self::assertEquals('test-key', $apiKey);
        self::assertEquals('https://api.lexoffice.io', $apiEndpoint);
        self::assertEquals('v1', $apiVersion);
    }

    public function testIfClientIsRegistered(): void
    {
        $client = $this->container->get(LexofficeClient::class);

        self::assertInstanceOf(LexofficeClient::class, $client);
    }
}

完整代码可以在这里找到:https://github.com/var-lab-it/lexoffice-bundle/tree/main/tests/Integration

© www.soinside.com 2019 - 2024. All rights reserved.