-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TesterBundle] phpunit test injection
- Loading branch information
Showing
10 changed files
with
201 additions
and
18 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
packages/tester-bundle/PHPStan/Rules/Properties/AutowireReadWritePropertiesExtension.php
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,32 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\PHPStan\Rules\Properties; | ||
|
||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\Rules\Properties\ReadWritePropertiesExtension; | ||
|
||
class AutowireReadWritePropertiesExtension implements ReadWritePropertiesExtension | ||
{ | ||
public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool | ||
{ | ||
return $this->hasProperAttribute($property, $propertyName, AutowireService::class); | ||
} | ||
|
||
public function isInitialized(PropertyReflection $property, string $propertyName): bool | ||
{ | ||
return $this->hasProperAttribute($property, $propertyName, AutowireService::class); | ||
} | ||
|
||
private function hasProperAttribute(PropertyReflection $property, string $propertyName, string $attribute): bool | ||
{ | ||
$properReflection = $property->getDeclaringClass()->getNativeProperty($propertyName)->getNativeReflection(); | ||
|
||
return 0 !== \count($properReflection->getAttributes($attribute, \ReflectionAttribute::IS_INSTANCEOF)); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/AutowireInterface.php
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,7 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire; | ||
|
||
interface AutowireInterface | ||
{ | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/AutowireService.php
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,16 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire; | ||
|
||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
class AutowireService | ||
{ | ||
public function __construct(private ?string $serviceId = null) | ||
{ | ||
} | ||
|
||
public function getServiceId(): ?string | ||
{ | ||
return $this->serviceId; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/AutowireTransportTester.php
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,12 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire; | ||
|
||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
class AutowireTransportTester extends AutowireService | ||
{ | ||
public function __construct(string $tranportName) | ||
{ | ||
parent::__construct(sprintf('messenger.transport.%s.draw.tester', $tranportName)); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/SetUpAutowireExtension.php
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,108 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire; | ||
|
||
use Draw\Component\Core\Reflection\ReflectionAccessor; | ||
use Draw\Component\Core\Reflection\ReflectionExtractor; | ||
use PHPUnit\Event\Code\TestMethod; | ||
use PHPUnit\Event\Test\Prepared as TestPrepared; | ||
use PHPUnit\Event\Test\PreparedSubscriber as TestPreparedSubscriber; | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Runner\Extension\Extension; | ||
use PHPUnit\Runner\Extension\Facade; | ||
use PHPUnit\Runner\Extension\ParameterCollection; | ||
use PHPUnit\TextUI\Configuration\Configuration; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
class SetUpAutowireExtension implements Extension | ||
{ | ||
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void | ||
{ | ||
$facade->registerSubscribers( | ||
new class() implements TestPreparedSubscriber { | ||
/** | ||
* @var array<string, array<int, array{\ReflectionProperty, string}>> | ||
*/ | ||
private array $propertyAttributes = []; | ||
|
||
public function notify(TestPrepared $event): void | ||
{ | ||
$test = $event->test(); | ||
|
||
\assert($test instanceof TestMethod); | ||
|
||
if (!is_a($test->className(), AutowireInterface::class, true)) { | ||
return; | ||
} | ||
|
||
$testCase = null; | ||
|
||
foreach (debug_backtrace() as $frame) { | ||
if (isset($frame['object']) && $frame['object'] instanceof TestCase) { | ||
$testCase = $frame['object']; | ||
break; | ||
} | ||
} | ||
|
||
if (!$testCase instanceof AutowireInterface) { | ||
return; | ||
} | ||
|
||
if (!$testCase instanceof KernelTestCase) { | ||
return; | ||
} | ||
|
||
$container = null; | ||
|
||
foreach ($this->getPropertyAttributes($testCase) as [$property, $serviceId]) { | ||
$container ??= ReflectionAccessor::callMethod($testCase, 'getContainer'); | ||
|
||
$property->setValue( | ||
$testCase, | ||
$container->get($serviceId) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @return iterable<array{0:\ReflectionProperty, 1: string}> | ||
*/ | ||
private function getPropertyAttributes(TestCase $testCase): iterable | ||
{ | ||
$className = $testCase::class; | ||
|
||
if (!\array_key_exists($className, $this->propertyAttributes)) { | ||
$this->propertyAttributes[$className] = []; | ||
|
||
foreach ((new \ReflectionObject($testCase))->getProperties() as $property) { | ||
$attribute = $property->getAttributes(AutowireService::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null; | ||
|
||
if (!$attribute) { | ||
continue; | ||
} | ||
|
||
$autoWireService = $attribute->newInstance(); | ||
|
||
$serviceId = $autoWireService->getServiceId(); | ||
|
||
if (!$serviceId) { | ||
$classes = ReflectionExtractor::getClasses($property->getType()); | ||
if (1 !== \count($classes)) { | ||
throw new \RuntimeException('Property '.$property->getName().' of class '.$testCase::class.' must have a type hint.'); | ||
} | ||
|
||
$serviceId = $classes[0]; | ||
} | ||
|
||
$this->propertyAttributes[$className][] = [$property, $serviceId]; | ||
} | ||
} | ||
|
||
foreach ($this->propertyAttributes[$className] as $property) { | ||
yield $property; | ||
} | ||
} | ||
}, | ||
); | ||
} | ||
} |
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,5 @@ | ||
services: | ||
draw.tester_bundle.autowire_read_write_properties_extension: | ||
class: Draw\Bundle\TesterBundle\PHPStan\Rules\Properties\AutowireReadWritePropertiesExtension | ||
tags: | ||
- phpstan.properties.readWriteExtension |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
includes: | ||
- phpstan-baseline.neon | ||
- packages/tester-bundle/extension.neon | ||
|
||
parameters: | ||
level: 5 | ||
|
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