teamneusta/pimcore-testing-framework 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

teamneusta/pimcore-testing-framework

Composer 安装命令:

composer require --dev teamneusta/pimcore-testing-framework

包简介

The Pimcore testing framework provides base classes for unit, integration and functional testing

README 文档

README

Provides tools for Pimcore unit/integration testing with PHPUnit.

Installation

  1. Require the bundle

    composer require --dev teamneusta/pimcore-testing-framework

Usage

Bootstrapping Pimcore

We provide a convenience method to bootstrap Pimcore for running tests. Just call BootstrapPimcore::bootstrap() in your tests/bootstrap.php as seen below, and you're done.

# tests/bootstrap.php
<?php

include dirname(__DIR__).'/vendor/autoload.php';

Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore::bootstrap();

You can also pass any environment variable via named arguments to this method:

# tests/bootstrap.php
Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore::bootstrap(
    APP_ENV: 'custom',
    SOMETHING: 'else',
);

Integration Tests For a Bundle

If you want to add integration tests for a Bundle, you need to set up an application with a kernel. Pimcore also expects some configuration (e.g., for the security) to be present.

You can use the \Neusta\Pimcore\TestingFramework\Kernel\TestKernel as a base, which already provides all necessary configurations with default values (see: dist/config and dist/pimcore11/config or dist/pimcore12/config, depending on your Pimcore version).

For a basic setup, you can use the TestKernel directly:

# tests/bootstrap.php
<?php

use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore;

include dirname(__DIR__).'/vendor/autoload.php';

BootstrapPimcore::bootstrap(
    PIMCORE_PROJECT_ROOT: __DIR__.'/app',
    KERNEL_CLASS: TestKernel::class,
);

Important

Don't forget to create the tests/app directory!

mkdir -p tests/app
echo '/var' > tests/app/.gitignore

Note

Since Pimcore 11 and 12 configuration someteimes differ, we have extended our TestKernel with the ability to load separate configuration files depending on the version. Configuration that is compatible with both Pimcore versions belongs to the config/ folder of the test app as before. Version specific configuration can be placed inside the config/pimcore11/ or config/pimcore12/ folder and will be loaded last.

Switch Common Behavior on/off in Test Cases

We provide traits to switch common behavior on/off in whole test case classes.

Admin Mode

The admin mode is disabled by default when calling BootstrapPimcore::bootstrap().

To enable it again, you can use the WithAdminMode trait.

Cache

  • WithoutCache

Inherited Values of DataObjects

  • WithInheritedValues
  • WithoutInheritedValues

Integration Tests With a Configurable Kernel

The TestKernel can be configured dynamically for each test. This is useful if different configurations or dependent bundles are to be tested. To do this, your test class must inherit from ConfigurableKernelTestCase:

use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;

class SomeTest extends ConfigurableKernelTestCase
{
    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());
        }]);
    }
}

Attributes

An alternative to passing a config closure in the options array to ConfigurableKernelTestCase::bootKernel() is to use attributes for the kernel configuration.

use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureContainer;
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterBundle;
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterCompilerPass;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;

#[RegisterBundle(SomeBundle::class)]
class SomeTest extends ConfigurableKernelTestCase 
{
    #[ConfigureContainer(__DIR__ . '/Fixtures/some_config.yaml')]
    #[ConfigureExtension('some_extension', ['config' => 'values'])]
    #[RegisterCompilerPass(new SomeCompilerPass())]
    public function test_something(): void
    {
        self::bootKernel();

        // test something
    }
}

Tip

All attributes can be used on class and test method level.

Data Provider

You can also use the RegisterBundle, ConfigureContainer, ConfigureExtension, or RegisterCompilerPass classes to configure the kernel in a data provider.

use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;

class SomeTest extends ConfigurableKernelTestCase 
{
    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'));
    }
}

Tip

The kernel configuration objects are not passed as arguments to the test method, which means you can use them anywhere between your provided real test data.

Custom Attributes

You can create your own kernel configuration attributes by implementing the KernelConfiguration interface:

use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class ConfigureSomeBundle implements KernelConfiguration
{
    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,
        ));
    }
}

Then you can use the new class as an attribute or inside a data provider.

Integration Tests With a Database

If you write integration tests that use the database, we've got you covered too.

We provide the ResetDatabase trait, which does the heavy lifting: Just use it in one of your test case classes, and it'll install a fresh Pimcore into the configured database before the first test is run. It'll also reset the database between each test, so you don't have to worry about leftovers from previous tests.

Using a Dump

If you already have a database dump that you want to use instead of a fresh Pimcore installation, there's the DATABASE_DUMP_LOCATION environment variable. Point it to the location of your dump, and it'll be used instead.

Faster Database Reset

By default, resetting the database between the tests works by dropping the database, recreating it and reinstalling Pimcore (or reimporting the dump).

This is rather slow, but there are some tricks that can speed it up:

Storing the Database in the RAM

Normally, the database is stored on the disk, so that the data is persisted. But we don't really need this for testing, so if you're using Docker, you can configure it to store it in RAM instead:

# compose.yaml
services:
  db:
    image: 'mariadb:10.10' # or 'mysql:8.0'
    tmpfs:
      - /tmp
      - /var/lib/mysql
Wrapping Each Test in a Transaction

We support the dama/doctrine-test-bundle, which isolates database tests by wrapping them into a transaction. You just have to install the bundle according to its readme, and it'll automatically be used.

Contribution

Feel free to open issues for any bug, feature request, or other ideas.

Please remember to create an issue before creating large pull requests.

Local Development

To develop on your local machine, instance identification for Pimcore 12 is needed.

Copy the compose.override.yaml.dist file to compose.override.yaml:

cp -n compose.override.yaml.dist compose.override.yaml

And replace all replace_with_secret values with your data.

Then install the dependencies:

bin/composer install

We use composer scripts for our main quality tools. They can be executed via the bin/composer file as well.

bin/composer cs:fix
bin/composer phpstan

For the tests there is a different script that includes a database setup.

bin/run-tests

teamneusta/pimcore-testing-framework 适用场景与选型建议

teamneusta/pimcore-testing-framework 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.41k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「pimcore」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 teamneusta/pimcore-testing-framework 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 teamneusta/pimcore-testing-framework 我们能提供哪些服务?
定制开发 / 二次开发

基于 teamneusta/pimcore-testing-framework 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 10
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 10
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-09