teamneusta / pimcore-testing-framework
Pimcore 测试框架提供了单元、集成和功能测试的基础类
Requires
- php: ~8.1.0 || ~8.2.0
- doctrine/persistence: ^2.2 || ^3.0
- phpunit/phpunit: ^9.6.0
- pimcore/pimcore: ^10.5 || ~11.0.0 || ~11.1.0 || ~11.2.2
- psr/log: ^1.1.3 || ^2.0 || ^3.0
- symfony/config: ^5.4 || ^6.4
- symfony/console: ^5.4 || ^6.4
- symfony/dependency-injection: ^5.4 || ^6.4
- symfony/event-dispatcher: ^5.4 || ^6.4
- symfony/filesystem: ^5.4 || ^6.4
- symfony/framework-bundle: ^5.4 || ^6.4
- symfony/http-kernel: ^5.4 || ^6.4
Requires (Dev)
- dama/doctrine-test-bundle: ^6.0 || ^7.0
- doctrine/orm: ^2.7
- ergebnis/composer-normalize: ^2.42.0
- friendsofphp/php-cs-fixer: ^3.60
- laminas/laminas-zendframework-bridge: ^1.8
- phpspec/prophecy-phpunit: ^2.2
- phpstan/extension-installer: ^1.3.1
- phpstan/phpstan: ^1.10.60
- phpstan/phpstan-phpunit: ^1.3.16
- phpstan/phpstan-symfony: ^1.3.8
- shipmonk/composer-dependency-analyser: ^1.7
Suggests
- dama/doctrine-test-bundle: To isolate database tests in transactions and improve test performance
- pimcore/admin-ui-classic-bundle: Required when used with Pimcore 11
This package is auto-updated.
Last update: 2024-09-11 07:03:44 UTC
README
提供用于 Pimcore 单元/集成测试的 PHPUnit 工具。
安装
-
需要此包
composer require --dev teamneusta/pimcore-testing-framework
用法
引导 Pimcore
我们提供了一个方便的方法来引导 Pimcore 以运行测试。只需在下面的 tests/bootstrap.php
中调用 BootstrapPimcore::bootstrap()
即可,完成。
# tests/bootstrap.php <?php include dirname(__DIR__).'/vendor/autoload.php'; \Neusta\Pimcore\TestingFramework\BootstrapPimcore::bootstrap();
您还可以通过命名参数传递任何环境变量到这个方法
# tests/bootstrap.php \Neusta\Pimcore\TestingFramework\BootstrapPimcore::bootstrap( APP_ENV: 'custom', SOMETHING: 'else', );
为 Bundle 执行集成测试
如果您想为 Bundle 添加集成测试,您需要设置一个带有内核的应用程序。Pimcore 也期望存在一些配置(例如,对于 security
)。
您可以使用 \Neusta\Pimcore\TestingFramework\Kernel\TestKernel
作为基础,它已经提供了所有必要的默认配置(请参阅:dist/config
和 dist/pimcore10/config
或 dist/pimcore11/config
,具体取决于您的 Pimcore 版本)。
对于基本设置,您可以直接使用 TestKernel
# tests/bootstrap.php <?php use Neusta\Pimcore\TestingFramework\BootstrapPimcore; use Neusta\Pimcore\TestingFramework\TestKernel; include dirname(__DIR__).'/vendor/autoload.php'; BootstrapPimcore::bootstrap( PIMCORE_PROJECT_ROOT: __DIR__.'/app', KERNEL_CLASS: TestKernel::class, );
重要
别忘了创建 tests/app
目录!
mkdir -p tests/app echo '/var' > tests/app/.gitignore
注意
由于 Pimcore 10 和 11 的内核不兼容(configureContainer()
方法的签名不同),我们已经扩展了我们的 TestKernel
,使其能够根据版本加载不同的配置文件。与两个版本都兼容的配置属于测试应用的 config/
文件夹,如之前一样。特定版本的配置可以放置在 config/pimcore10/
或 config/pimcore11/
文件夹中,并将最后加载。
在测试用例中切换常见行为的开/关
我们提供了特质来在整测试用例类中切换常见行为的开/关。
管理模式
调用 BootstrapPimcore::bootstrap()
时,默认禁用管理模式。
要再次启用它,您可以使用 WithAdminMode
特质。
缓存
WithoutCache
数据对象继承值
WithInheritedValues
WithoutInheritedValues
具有可配置内核的集成测试
TestKernel
可以针对每个测试动态配置。如果需要测试不同的配置或依赖项包,这很有用。为此,您的测试类必须继承自 KernelTestCase
。
use Neusta\Pimcore\TestingFramework\KernelTestCase; use Neusta\Pimcore\TestingFramework\TestKernel; class SomeTest extends KernelTestCase { public function test_bundle_with_different_configuration(): void { // Boot the kernel with a config closure $kernel = self::bootKernel(['config' => static function (TestKernel $kernel) { // Add some other bundles we depend on $kernel->addTestBundle(OtherBundle::class); // Add some configuration $kernel->addTestConfig(__DIR__.'/config.yaml'); // Configure some extension $kernel->addTestExtensionConfig('my_bundle', ['some_config' => true]); // Add some compiler pass $kernel->addTestCompilerPass(new MyBundleCompilerPass()); }]); } }
属性
在 options
数组中传递 config
闭包到 KernelTestCase::bootKernel()
的另一种方法是使用内核配置属性。
use Neusta\Pimcore\TestingFramework\Attribute\Kernel\ConfigureContainer; use Neusta\Pimcore\TestingFramework\Attribute\Kernel\ConfigureExtension; use Neusta\Pimcore\TestingFramework\Attribute\Kernel\RegisterBundle; use Neusta\Pimcore\TestingFramework\Attribute\Kernel\RegisterCompilerPass; use Neusta\Pimcore\TestingFramework\KernelTestCase; #[RegisterBundle(SomeBundle::class)] class SomeTest extends KernelTestCase { #[ConfigureContainer(__DIR__ . '/Fixtures/some_config.yaml')] #[ConfigureExtension('some_extension', ['config' => 'values'])] #[RegisterCompilerPass(new SomeCompilerPass())] public function test_something(): void { self::bootKernel(); // test something } }
提示
所有属性都可以在类 和 测试方法级别上使用。
数据提供者
您还可以使用 RegisterBundle
、ConfigureContainer
、ConfigureExtension
或 RegisterCompilerPass
类来配置数据提供者中的内核。
use Neusta\Pimcore\TestingFramework\Attribute\Kernel\ConfigureExtension; use Neusta\Pimcore\TestingFramework\KernelTestCase; class SomeTest extends KernelTestCase { public function provideTestData(): iterable { yield [ 'some value', new ConfigureExtension('some_extension', ['config' => 'some value']), ]; yield [ new ConfigureExtension('some_extension', ['config' => 'other value']), 'other value', ]; } /** @dataProvider provideTestData */ public function test_something(string $expected): void { self::assertSame($expected, self::getContainer()->getParameter('config')); } }
提示
内核配置对象不会作为参数传递给测试方法,这意味着您可以在提供真实测试数据之间任何地方使用它们。
自定义属性
您可以通过实现 KernelConfiguration
接口来创建自己的内核配置属性。
use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel; use Neusta\Pimcore\TestingFramework\TestKernel; #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)] class ConfigureSomeBundle implements ConfigureKernel { public function __construct( private readonly array $config, ) { } public function configure(TestKernel $kernel): void { $kernel->addTestBundle(SomeBundle::class); $kernel->addTestExtensionConfig('some', array_merge( ['default' => 'config'], $this->config, )); } }
然后您可以将新类用作属性或数据提供者内部。
具有数据库的集成测试
如果您编写了使用数据库的集成测试,我们也为您提供了解决方案。
我们提供了ResetDatabase
特性,它负责重头戏:只需在您的测试用例类中使用它,它就会在运行第一个测试之前,在配置的数据库中安装一个新的Pimcore。它还会在每个测试之间重置数据库,这样您就不必担心之前的测试遗留问题。
使用数据库转储
如果您已经有了想要使用的数据库转储文件,而不是全新的Pimcore安装,可以使用DATABASE_DUMP_LOCATION
环境变量。将其指向转储文件的位置,它将替代使用。
更快的数据库重置
默认情况下,在测试之间重置数据库是通过删除数据库、重新创建它并重新安装Pimcore(或重新导入转储)来实现的。
这相当慢,但有一些技巧可以加快速度
将数据库存储在RAM中
通常,数据库存储在磁盘上,以便数据持久化。但在测试中我们并不真的需要这样做,所以如果您使用Docker,可以配置它将其存储在RAM中。
# compose.yaml services: db: image: 'mariadb:10.10' # or 'mysql:8.0' tmpfs: - /tmp - /var/lib/mysql
将每个测试封装在事务中
我们支持dama/doctrine-test-bundle
,它通过将测试封装在事务中来隔离数据库测试。您只需按照其readme文件安装此包即可,它将自动使用。
贡献
请随时为任何错误、功能请求或其他想法提交问题。
请在创建大型pull请求之前记得创建一个问题。
本地开发
要在本地机器上进行开发,需要安装供应商依赖项。
bin/composer install
我们使用composer脚本来执行我们的主要质量工具。它们可以通过bin/composer
文件执行。
bin/composer cs:fix bin/composer phpstan bin/composer tests