Skip to content

Commit

Permalink
Composer > Add handy scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
simplebus-bot committed Jul 8, 2021
1 parent cac2265 commit 744e6e3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/DependencyInjection/Compiler/AutoRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionUnionType;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand Down Expand Up @@ -63,17 +64,30 @@ public function process(ContainerBuilder $container): void
}

$type = $parameters[0]->getType();
if (null === $type || !$type instanceof ReflectionNamedType) {
if (null === $type) {
continue;
}

// get the class name
$handles = $type->getName();
if (!$type instanceof ReflectionNamedType && !$type instanceof ReflectionUnionType) {
continue;
}

if ($type instanceof ReflectionUnionType) {
$types = $type->getTypes();
} else {
$types = [$type];
}

foreach ($types as $type) {
if (!$type instanceof ReflectionNamedType) {
continue;
}

$tagAttributes[] = [
$this->tagAttribute => $handles,
'method' => $method->getName(),
];
$tagAttributes[] = [
$this->tagAttribute => $type->getName(),
'method' => $method->getName(),
];
}
}

if (0 !== count($tags)) {
Expand Down
55 changes: 55 additions & 0 deletions tests/Functional/Php8SmokeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace SimpleBus\SymfonyBridge\Tests\Functional;

use SimpleBus\Message\Bus\MessageBus;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent2;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent3;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEventSubscriberUsingPublicMethodAndUnion;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestKernel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* @internal
* @coversNothing
* @requires PHP 8.0
*/
class Php8SmokeTest extends KernelTestCase
{
protected function tearDown(): void
{
parent::tearDown();

static::$class = null;
}

/**
* @test
*/
public function itCanAutoRegisterEventSubscribersUsingPublicMethodAndUnion(): void
{
self::bootKernel(['environment' => 'config2_php8']);
$container = self::$kernel->getContainer();

$event2 = new AutoEvent2();
$event3 = new AutoEvent3();

$this->assertFalse($event2->isHandledBy(AutoEventSubscriberUsingPublicMethodAndUnion::class));
$this->assertFalse($event3->isHandledBy(AutoEventSubscriberUsingPublicMethodAndUnion::class));

$eventBus = $container->get('event_bus');

$this->assertInstanceOf(MessageBus::class, $eventBus);

$eventBus->handle($event2);
$eventBus->handle($event3);

$this->assertTrue($event2->isHandledBy(AutoEventSubscriberUsingPublicMethodAndUnion::class));
$this->assertTrue($event3->isHandledBy(AutoEventSubscriberUsingPublicMethodAndUnion::class));
}

protected static function getKernelClass(): string
{
return TestKernel::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;

final class AutoEventSubscriberUsingPublicMethodAndUnion
{
public function __invoke(AutoEvent2 | AutoEvent3 $event): void
{
$event->setHandledBy($this);
}
}
9 changes: 9 additions & 0 deletions tests/Functional/SmokeTest/config2_php8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
imports:
- { resource: config.yml }

services:
auto_event_subscriber_using_public_method_and_union:
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEventSubscriberUsingPublicMethodAndUnion
tags:
- { name: event_subscriber, register_public_methods: true }
public: false

0 comments on commit 744e6e3

Please sign in to comment.