Skip to content

Commit

Permalink
refactor SetUpAutowireExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed May 21, 2024
1 parent 9708aaa commit 9b89b71
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SymfonyWebTestCase;

class SetUpAutowireExtension implements Extension
{
Expand Down Expand Up @@ -102,21 +102,23 @@ public function notify(TestPrepared $event): void

private function initializeClients(TestCase $testCase): void
{
if (!$testCase instanceof WebTestCase && !$testCase instanceof DrawWebTestCase) {
if (!empty(iterator_to_array($this->getClientAttributes($testCase), false))) {
throw new \RuntimeException(
sprintf(
'AutowireClient attribute can only be used in %s or %s.',
WebTestCase::class,
DrawWebTestCase::class
)
);
}
$clientAttributes = iterator_to_array($this->getClientAttributes($testCase));

if (empty($clientAttributes)) {
return;
}

foreach ($this->getClientAttributes($testCase) as [$property, $attribute]) {
if (!$testCase instanceof SymfonyWebTestCase && !$testCase instanceof DrawWebTestCase) {
throw new \RuntimeException(
sprintf(
'AutowireClient attribute can only be used in %s or %s.',
SymfonyWebTestCase::class,
DrawWebTestCase::class
)
);
}

foreach ($clientAttributes as [$property, $attribute]) {
\assert($property instanceof \ReflectionProperty);
\assert($attribute instanceof \ReflectionAttribute);

Expand All @@ -135,7 +137,7 @@ private function initializeClients(TestCase $testCase): void
}
}

private function getClientAttributes(TestCase $testCase): \Generator
private function getClientAttributes(TestCase $testCase): iterable
{
foreach ((new \ReflectionObject($testCase))->getProperties() as $property) {
$attribute = $property->getAttributes(
Expand Down

0 comments on commit 9b89b71

Please sign in to comment.