Symfony捆绑包中的功能测试

时间:2020-09-25 13:20:52

标签: symfony bundle simple-phpunit

我创建了2个项目,一个是一个处理请求并包含代码的包,第二个只是一个包含该包的项目。

为什么我创建了捆绑包?因为这样,我可以根据与之合作的客户来修饰服务/视图。

我已经有功能测试,但是现在,我在主项目中创建了它们。我想将它们移入捆绑软件之一,因为控制器在此捆绑软件中。

当我要处理请求时,我假设我需要一个内核。

所以我创建了一个:

namespace MyProject\MyBundle\Tests;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

class TestKernel extends BaseKernel
{
    use MicroKernelTrait;

    private const CONFIG_EXTS = '.{xml,yaml}';

    public function registerBundles(): iterable
    {
        $contents = require $this->getProjectDir().'/config/bundles.php';
        foreach ($contents as $class => $envs) {
            if ($envs[$this->environment] ?? $envs['all'] ?? false) {
                yield new $class();
            }
        }
    }

    public function getProjectDir(): string
    {
        return \dirname(__DIR__).'/tests';
    }

    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
    {
        $container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php'));
        $container->setParameter('container.dumper.inline_class_loader', \PHP_VERSION_ID < 70400 || $this->debug);
        $container->setParameter('container.dumper.inline_factories', true);
        $confDir = $this->getProjectDir().'/config';

        $loader->load($this->getProjectDir().'/../Resources/config/my_project.yaml');

        $loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
        $loader->load($confDir.'/*'.self::CONFIG_EXTS, 'glob');
    }

    protected function configureRoutes(RouteCollectionBuilder $routes): void
    {
        $confDir = $this->getProjectDir().'/../Resources/config';

        $routes->import($confDir.'/routes*.yaml', '/', 'glob');
    }
}

我在哪里加载配置文件。

在我的/ test文件夹(在捆绑包中)中,我创建了config /文件夹的某种副本(来自我的主项目),其中包含测试的值。我不完全相信,但它现在可以正常工作。

我正在逐行挖掘错误,但是...我不确定这是正确的解决方案。

有没有更简单的测试包的解决方案?我使用SF4.4版本进行开发,我们计划在测试发布后立即进行升级。

0 个答案:

没有答案