-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow kernel configuration via attributes (#11)
- Loading branch information
Showing
12 changed files
with
334 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\TestingFramework\Test\Attribute; | ||
|
||
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class ConfigureContainer implements KernelConfiguration | ||
{ | ||
/** | ||
* @param string $config path to a config file | ||
*/ | ||
public function __construct( | ||
private readonly string $config, | ||
) { | ||
} | ||
|
||
public function configure(TestKernel $kernel): void | ||
{ | ||
$kernel->addTestConfig($this->config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\TestingFramework\Test\Attribute; | ||
|
||
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class ConfigureExtension implements KernelConfiguration | ||
{ | ||
/** | ||
* @param array<string, array<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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\TestingFramework\Test\Attribute; | ||
|
||
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel; | ||
|
||
interface KernelConfiguration | ||
{ | ||
public function configure(TestKernel $kernel): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\TestingFramework\Test\Attribute; | ||
|
||
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel; | ||
use Symfony\Component\HttpKernel\Bundle\BundleInterface; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class RegisterBundle implements KernelConfiguration | ||
{ | ||
/** | ||
* @param class-string<BundleInterface> $bundle | ||
*/ | ||
public function __construct( | ||
private readonly string $bundle, | ||
) { | ||
} | ||
|
||
public function configure(TestKernel $kernel): void | ||
{ | ||
$kernel->addTestBundle($this->bundle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\TestingFramework\Test\Attribute; | ||
|
||
use Neusta\Pimcore\TestingFramework\Kernel\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 KernelConfiguration | ||
{ | ||
/** | ||
* @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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\TestingFramework\Test\Reflection; | ||
|
||
use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TestAttributeProvider | ||
{ | ||
private \ReflectionMethod $test; | ||
|
||
public function __construct(TestCase $test) | ||
{ | ||
$this->test = new \ReflectionMethod($test, $test->getName(false)); | ||
} | ||
|
||
/** | ||
* @return list<KernelConfiguration> | ||
*/ | ||
public function getKernelConfigurationAttributes(): array | ||
{ | ||
$attributes = []; | ||
foreach ($this->getAttributes($this->test->getDeclaringClass(), KernelConfiguration::class) as $attribute) { | ||
$attributes[] = $attribute->newInstance(); | ||
} | ||
|
||
foreach ($this->getAttributes($this->test, KernelConfiguration::class) as $attribute) { | ||
$attributes[] = $attribute->newInstance(); | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* | ||
* @param class-string<T> $attribute | ||
* | ||
* @return iterable<\ReflectionAttribute<T>> | ||
*/ | ||
private function getAttributes(\ReflectionClass|\ReflectionMethod $source, string $attribute): iterable | ||
{ | ||
yield from $source->getAttributes($attribute, \ReflectionAttribute::IS_INSTANCEOF); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.