teamneusta / pimcore-testing-framework
The Pimcore testing framework provides base classes for unit, integration and functional testing
Installs: 6 615
Dependents: 6
Suggesters: 0
Security: 0
Stars: 6
Watchers: 11
Forks: 0
Open Issues: 2
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.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 || ~11.3.0 || ~11.4.0
- 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: 2025-01-28 09:40:52 UTC
README
Provides tools for Pimcore unit/integration testing with PHPUnit.
Installation
-
Require the bundle
composer require --dev teamneusta/pimcore-testing-framework
Usage
Bootstrapping Pimcore
This Bundle provides a convenience method to bootstrap Pimcore for running tests:
Call BootstrapPimcore::bootstrap()
in your tests/bootstrap.php
as seen below, and you’re done.
# tests/bootstrap.php require dirname(__DIR__).'/vendor/autoload.php'; \Neusta\Pimcore\TestingFramework\BootstrapPimcore::bootstrap();
You can also pass any environment variable via named arguments to this method:
# tests/bootstrap.php \Neusta\Pimcore\TestingFramework\BootstrapPimcore::bootstrap( APP_ENV: 'custom', SOMETHING: 'else', );
Note
Make sure this file is configured as the bootstrap file in your phpunit.xml.dist
file:
<!-- phpunit.xml.dist --> <?xml version="1.0" encoding="UTF-8" ?> <phpunit bootstrap="tests/bootstrap.php" > <!-- ... --> </phpunit>
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\TestKernel
as a base,
which already provides all necessary configurations with default values
(see: dist/config
and dist/pimcore10/config
or dist/pimcore11/config
, depending on your Pimcore version).
For a basic setup, you can use the TestKernel
directly:
# tests/bootstrap.php use Neusta\Pimcore\TestingFramework\BootstrapPimcore; use Neusta\Pimcore\TestingFramework\TestKernel; require dirname(__DIR__).'/vendor/autoload.php'; BootstrapPimcore::bootstrap( PIMCORE_PROJECT_ROOT: __DIR__.'/app', KERNEL_CLASS: TestKernel::class, );
Important
Remember to create the tests/app
directory!
mkdir -p tests/app echo '/var' > tests/app/.gitignore
Note
Since the kernels of Pimcore 10 and 11 are not compatible (the signature of the method configureContainer()
differs),
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/pimcore10/
or config/pimcore11/
folder and will be loaded last.
Switch Common Behavior On/Off in Test Cases
This bundle provides 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 use the ConfigurableKernel
trait:
use Neusta\Pimcore\TestingFramework\ConfigurableKernel; use Neusta\Pimcore\TestingFramework\TestKernel; use Pimcore\Test\KernelTestCase; class SomeTest extends KernelTestCase { use ConfigurableKernel; 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 KernelTestCase::bootKernel()
is to use attributes for the kernel configuration.
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\ConfigurableKernel; use Pimcore\Test\KernelTestCase; #[RegisterBundle(SomeBundle::class)] class SomeTest extends KernelTestCase { use ConfigurableKernel; #[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 ConfigureContainer
, ConfigureExtension
, RegisterBundle
, or RegisterCompilerPass
classes
to configure the kernel in a data provider.
use Neusta\Pimcore\TestingFramework\Attribute\Kernel\ConfigureExtension; use Neusta\Pimcore\TestingFramework\ConfigurableKernel; use Pimcore\Test\KernelTestCase; class SomeTest extends KernelTestCase { use ConfigurableKernel; 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 ConfigureKernel
interface:
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, )); } }
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.
This bundle provides the ResetDatabase
trait, which does the heavy lifting:
Use it in one of your test case classes,
and it will install a fresh Pimcore into the configured database before the first test is run.
It will 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 will 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.
Install the bundle according to its readme,
and it will 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 a local machine, the vendor dependencies are required.
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 bin/composer tests