微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony捆绑包中的功能测试

如何解决Symfony捆绑包中的功能测试

我创建了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版本进行开发,我们计划在测试发布后立即进行升级

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。