Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable Kernel for integration tests #8

Merged
merged 16 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ jobs:
dependencies: "lowest"
- php-version: "8.1"
dependencies: "highest"
phpunit-flags: "--fail-on-risky"
- php-version: "8.2"
dependencies: "highest"
phpunit-flags: "--fail-on-risky"

steps:
- name: Git Checkout
Expand All @@ -39,5 +41,9 @@ jobs:
with:
dependency-versions: ${{ matrix.dependencies }}

- name: Add Pimcore Admin UI
run: composer require --dev pimcore/admin-ui-classic-bundle --no-interaction
if: matrix.dependencies == 'highest'

- name: Execute tests
run: composer tests
run: composer tests -- ${{ matrix.phpunit-flags }}
11 changes: 5 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Changelog

## v0.11.1

## v0.12.0
### Features
- Dynamically configurable test kernel with which you can register bundles, load configurations,
configure extensions, and register compiler passes for each test.

## v0.11.1
### Features
- Support for Pimcore 11.

## v0.11.0

### Breaking Changes:

- `Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore::bootstrap()` now expects named arguments.<br>
If you pass the application environment as a parameter, you now have to prefix it with: `APP_ENV:`:
```diff
Expand All @@ -22,9 +23,7 @@
- The `WithoutAdminMode` trait was removed.

### Bugfixes:

- Reset admin mode to previous state after each test class when using `WithAdminMode` trait.

### Changes:

- Drop support for Pimcore `<10.5` and PHP `<8.1`.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,38 @@ To enable it again, you can use the `WithAdminMode` trait.
- `WithInheritedValues`
- `WithoutInheritedValues`

### Integration tests with 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`:

```php
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());
}]);
}
}
```

### Integration Tests With a Database

If you write integration tests that use the database, we've got you covered too.
Expand Down
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
"require-dev": {
"dama/doctrine-test-bundle": "^6.0 || ^7.0",
"doctrine/orm": "^2.7",
"ergebnis/composer-normalize": "^2.6.1",
"friendsofphp/php-cs-fixer": "^3.0",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-phpunit": "^1.1",
"phpstan/phpstan-symfony": "^1.2",
"phpunit/phpunit": "^9.3"
"ergebnis/composer-normalize": "^2.42.0",
"friendsofphp/php-cs-fixer": "^3.51",
"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",
"phpunit/phpunit": "^9.6.17"
},
"suggest": {
"pimcore/admin-ui-classic-bundle": "Required when used with Pimcore 11"
Expand All @@ -50,7 +51,7 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\Unit\\Neusta\\Pimcore\\TestingFramework\\": "tests/Unit"
"Neusta\\Pimcore\\TestingFramework\\Tests\\": "tests/"
}
},
"config": {
Expand Down
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ parameters:
count: 1
path: src/Database/RunCommand.php

-
message: "#^Method Pimcore\\\\Kernel\\:\\:configureContainer\\(\\) invoked with 1 parameter, 3 required\\.$#"
count: 1
path: src/Kernel/TestKernel.php

-
message: "#^Parameter \\#1 \\$bundle of method Pimcore\\\\HttpKernel\\\\BundleCollection\\\\BundleCollection\\:\\:addBundle\\(\\) expects string\\|Symfony\\\\Component\\\\HttpKernel\\\\Bundle\\\\BundleInterface, Pimcore\\\\Bundle\\\\AdminBundle\\\\PimcoreAdminBundle given\\.$#"
count: 1
path: src/Kernel/TestKernel.php

-
message: "#^Parameter \\#1 \\$name of static method Neusta\\\\Pimcore\\\\TestingFramework\\\\Pimcore\\\\BootstrapPimcore\\:\\:setEnv\\(\\) expects string, int\\|string given\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ parameters:
- src

excludePaths:
# There are two errors, that were not able to add to the baseline
- src/Kernel/TestKernel.php
# There are two errors, that were not able to be added to the baseline
- src/Kernel/CompatibilityKernel.php

bootstrapFiles:
- vendor/pimcore/pimcore/stubs/dynamic-constants.php
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
colors="true"
failOnRisky="true"
failOnWarning="true"
>
<php>
Expand Down
107 changes: 107 additions & 0 deletions src/Kernel/CompatibilityKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Kernel;

use Pimcore\Bundle\AdminBundle\PimcoreAdminBundle;
use Pimcore\HttpKernel\BundleCollection\BundleCollection;
use Pimcore\Kernel;
use Pimcore\Version;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

if (!method_exists(Version::class, 'getMajorVersion') || 10 === Version::getMajorVersion()) {
/** @internal */
abstract class CompatibilityKernel extends Kernel
{
/**
* @internal
*
* @var list<string|callable(ContainerBuilder):void>
*/
protected array $testConfigs = [];
/**
* @internal
*
* @var array<string, array<mixed>>
*/
protected array $testExtensionConfigs = [];

protected function configureContainer(ContainerConfigurator $container): void
{
$container->import(__DIR__ . '/../../dist/config/*.yaml');
$container->import(__DIR__ . '/../../dist/pimcore10/config/*.yaml');

parent::configureContainer($container);

if (file_exists($pimcore10Config = $this->getProjectDir() . '/config/pimcore10')) {
$container->import($pimcore10Config . '/*.{php,yaml}');
}

foreach ($this->testExtensionConfigs as $namespace => $config) {
$container->extension($namespace, $config);
}
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

foreach ($this->testConfigs as $config) {
$loader->load($config);
}
}
}
} else {
/** @internal */
abstract class CompatibilityKernel extends Kernel
{
/**
* @internal
*
* @var list<string|callable(ContainerBuilder):void>
*/
protected array $testConfigs = [];
/**
* @internal
*
* @var array<string, array<mixed>>
*/
protected array $testExtensionConfigs = [];

protected function configureContainer(
ContainerConfigurator $container,
LoaderInterface $loader,
ContainerBuilder $builder,
): void {
$container->import(__DIR__ . '/../../dist/config/*.yaml');
$container->import(__DIR__ . '/../../dist/pimcore11/config/*.yaml');

parent::configureContainer($container, $loader, $builder);

if (file_exists($pimcore11Config = $this->getProjectDir() . '/config/pimcore11')) {
$container->import($pimcore11Config . '/*.{php,yaml}');
}

foreach ($this->testConfigs as $config) {
$loader->load($config);
}

foreach ($this->testExtensionConfigs as $namespace => $config) {
$container->extension($namespace, $config);
}
}

protected function registerCoreBundlesToCollection(BundleCollection $collection): void
{
if (!class_exists(PimcoreAdminBundle::class)) {
throw new \LogicException('Pimcore 11 requires the "pimcore/admin-ui-classic-bundle" dependency.');
}

parent::registerCoreBundlesToCollection($collection);

$collection->addBundle(new PimcoreAdminBundle(), 60);
}
}
}
Loading
Loading