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

Restructure the package #24

Merged
merged 6 commits into from
Sep 2, 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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## v0.13.0
### Changes:
- Deprecated `Neusta\Pimcore\TestingFramework\Kernel\TestKernel`
in favor of `Neusta\Pimcore\TestingFramework\TestKernel`
- Deprecated `Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore`
in favor of `Neusta\Pimcore\TestingFramework\BootstrapPimcore`
- Deprecated `Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase`
in favor of `Neusta\Pimcore\TestingFramework\KernelTestCase`
- Deprecated `Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration`
in favor of `\Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel`
- Deprecated `Neusta\Pimcore\TestingFramework\Test\Attribute\{ConfigureContainer,ConfigureExtension,RegisterBundle,RegisterCompilerPass}`
in favor of `\Neusta\Pimcore\TestingFramework\Attribute\Kernel\{ConfigureContainer,ConfigureExtension,RegisterBundle,RegisterCompilerPass}`

## v0.12.4
### Bugfixes:
- Fix type definition for `ConfgureExtension`
Expand Down
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ Just call `BootstrapPimcore::bootstrap()` in your `tests/bootstrap.php` as seen

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

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

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

```php
# tests/bootstrap.php
Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore::bootstrap(
\Neusta\Pimcore\TestingFramework\BootstrapPimcore::bootstrap(
APP_ENV: 'custom',
SOMETHING: 'else',
);
Expand All @@ -52,8 +52,8 @@ 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;
use Neusta\Pimcore\TestingFramework\BootstrapPimcore;
use Neusta\Pimcore\TestingFramework\TestKernel;

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

Expand Down Expand Up @@ -100,13 +100,13 @@ To enable it again, you can use the `WithAdminMode` trait.

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`:
To do this, your test class must inherit from `KernelTestCase`:

```php
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\TestKernel;

class SomeTest extends ConfigurableKernelTestCase
class SomeTest extends KernelTestCase
{
public function test_bundle_with_different_configuration(): void
{
Expand All @@ -130,18 +130,18 @@ class SomeTest extends ConfigurableKernelTestCase

#### Attributes

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

```php
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;
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 ConfigurableKernelTestCase
class SomeTest extends KernelTestCase
{
#[ConfigureContainer(__DIR__ . '/Fixtures/some_config.yaml')]
#[ConfigureExtension('some_extension', ['config' => 'values'])]
Expand All @@ -164,10 +164,10 @@ You can also use the `RegisterBundle`, `ConfigureContainer`, `ConfigureExtension
to configure the kernel in a data provider.

```php
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\Attribute\Kernel\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\KernelTestCase;

class SomeTest extends ConfigurableKernelTestCase
class SomeTest extends KernelTestCase
{
public function provideTestData(): iterable
{
Expand Down Expand Up @@ -199,11 +199,11 @@ class SomeTest extends ConfigurableKernelTestCase
You can create your own kernel configuration attributes by implementing the `KernelConfiguration` interface:

```php
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;
use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel;
use Neusta\Pimcore\TestingFramework\TestKernel;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class ConfigureSomeBundle implements KernelConfiguration
class ConfigureSomeBundle implements ConfigureKernel
{
public function __construct(
private readonly array $config,
Expand Down
3 changes: 3 additions & 0 deletions composer-dependency-analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// Exclude test app
->addPathToExclude(__DIR__ . '/tests/app')

// Ignore packages that the analyzer does not recognize
->ignoreErrorsOnPackage('symfony/deprecation-contracts', [ErrorType::UNUSED_DEPENDENCY])

// Ignore optional dependency
->ignoreErrorsOnPackageAndPath('dama/doctrine-test-bundle', __DIR__ . '/src/Database/ResetDatabase.php', [ErrorType::DEV_DEPENDENCY_IN_PROD])
->ignoreErrorsOnPackageAndPath('dama/doctrine-test-bundle', __DIR__ . '/src/Database/DatabaseResetter.php', [ErrorType::DEV_DEPENDENCY_IN_PROD])
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"symfony/config": "^5.4 || ^6.4",
"symfony/console": "^5.4 || ^6.4",
"symfony/dependency-injection": "^5.4 || ^6.4",
"symfony/deprecation-contracts": "^2.1 || ^3.0",
"symfony/event-dispatcher": "^5.4 || ^6.4",
"symfony/filesystem": "^5.4 || ^6.4",
"symfony/framework-bundle": "^5.4 || ^6.4",
Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ parameters:
path: src/Database/RunCommand.php

-
message: "#^Parameter \\#1 \\$name of static method Neusta\\\\Pimcore\\\\TestingFramework\\\\Pimcore\\\\BootstrapPimcore\\:\\:setEnv\\(\\) expects string, int\\|string given\\.$#"
message: "#^Parameter \\#1 \\$name of static method Neusta\\\\Pimcore\\\\TestingFramework\\\\BootstrapPimcore\\:\\:setEnv\\(\\) expects string, int\\|string given\\.$#"
count: 1
path: src/Pimcore/BootstrapPimcore.php
path: src/BootstrapPimcore.php
6 changes: 5 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ parameters:

excludePaths:
# There are two errors, that were not able to be added to the baseline
- src/Kernel/CompatibilityKernel.php
- src/Internal/CompatibilityTestKernel.php
# Deprecated aliases
- src/Kernel/TestKernel.php
- src/Pimcore/BootstrapPimcore.php
- src/Test/*

bootstrapFiles:
- vendor/pimcore/pimcore/stubs/dynamic-constants.php
Expand Down
11 changes: 11 additions & 0 deletions src/Attribute/ConfigureKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Attribute;

use Neusta\Pimcore\TestingFramework\TestKernel;

interface ConfigureKernel
{
public function configure(TestKernel $kernel): void;
}
25 changes: 25 additions & 0 deletions src/Attribute/Kernel/ConfigureContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Attribute\Kernel;

use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Symfony\Component\DependencyInjection\ContainerBuilder;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class ConfigureContainer implements ConfigureKernel
{
/**
* @param string|\Closure(ContainerBuilder):void $config path to a config file or a closure which gets the {@see ContainerBuilder} as its first argument
*/
public function __construct(
private readonly string|\Closure $config,
) {
}

public function configure(TestKernel $kernel): void
{
$kernel->addTestConfig($this->config);
}
}
25 changes: 25 additions & 0 deletions src/Attribute/Kernel/ConfigureExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Attribute\Kernel;

use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel;
use Neusta\Pimcore\TestingFramework\TestKernel;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class ConfigureExtension implements ConfigureKernel
{
/**
* @param array<string, mixed> $extensionConfig
*/
public function __construct(
private readonly string $namespace,
private readonly array $extensionConfig,
) {
}

public function configure(TestKernel $kernel): void
{
$kernel->addTestExtensionConfig($this->namespace, $this->extensionConfig);
}
}
25 changes: 25 additions & 0 deletions src/Attribute/Kernel/RegisterBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Attribute\Kernel;

use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class RegisterBundle implements ConfigureKernel
{
/**
* @param class-string<BundleInterface> $bundle
*/
public function __construct(
private readonly string $bundle,
) {
}

public function configure(TestKernel $kernel): void
{
$kernel->addTestBundle($this->bundle);
}
}
28 changes: 28 additions & 0 deletions src/Attribute/Kernel/RegisterCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Attribute\Kernel;

use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class RegisterCompilerPass implements ConfigureKernel
{
/**
* @param PassConfig::TYPE_* $type
*/
public function __construct(
private readonly CompilerPassInterface $compilerPass,
private readonly string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION,
private readonly int $priority = 0,
) {
}

public function configure(TestKernel $kernel): void
{
$kernel->addTestCompilerPass($this->compilerPass, $this->type, $this->priority);
}
}
31 changes: 31 additions & 0 deletions src/BootstrapPimcore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework;

use Neusta\Pimcore\TestingFramework\Pimcore\AdminMode;
use Pimcore\Bootstrap;

final class BootstrapPimcore
{
private const DEFAULT_ENV_VARS = [
'APP_ENV' => 'test',
];

public static function bootstrap(string ...$envVars): void
{
foreach ($envVars + self::DEFAULT_ENV_VARS as $name => $value) {
self::setEnv($name, $value);
}

Bootstrap::setProjectRoot();
Bootstrap::bootstrap();
AdminMode::disable();
}

public static function setEnv(string $name, string $value): void
{
putenv("{$name}=" . $_ENV[$name] = $_SERVER[$name] = $value);
}
}
2 changes: 1 addition & 1 deletion src/Database/ResetDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
use Neusta\Pimcore\TestingFramework\Exception\DoesNotExtendKernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Pimcore\Test\KernelTestCase;

/**
* @mixin KernelTestCase
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/DoesNotExtendKernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Neusta\Pimcore\TestingFramework\Exception;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Pimcore\Test\KernelTestCase;

final class DoesNotExtendKernelTestCase extends \RuntimeException
{
Expand All @@ -13,7 +13,7 @@ public static function forTrait(string $trait): self
return new self(\sprintf(
'The trait "%s" can only be used on TestCases that extend "%s".',
$trait,
KernelTestCase::class
KernelTestCase::class,
));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Kernel;
namespace Neusta\Pimcore\TestingFramework\Internal;

use Pimcore\Bundle\AdminBundle\PimcoreAdminBundle;
use Pimcore\HttpKernel\BundleCollection\BundleCollection;
Expand All @@ -13,7 +13,7 @@

if (!method_exists(Version::class, 'getMajorVersion') || 10 === Version::getMajorVersion()) {
/** @internal */
abstract class CompatibilityKernel extends Kernel
abstract class CompatibilityTestKernel extends Kernel
{
/**
* @internal
Expand Down Expand Up @@ -55,7 +55,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
}
} else {
/** @internal */
abstract class CompatibilityKernel extends Kernel
abstract class CompatibilityTestKernel extends Kernel
{
/**
* @internal
Expand Down
Loading
Loading