Skip to content

Commit 8a44be8

Browse files
Bartłomiej Nowakbnowak
Bartłomiej Nowak
authored andcommitted
covered more test-cases
1 parent 9438e5e commit 8a44be8

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

src/Symfony/MessageMapFactory.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,13 @@ public function create(): MessageMap
5353
$reflectionClass = $this->reflectionProvider->getClass($serviceClass);
5454

5555
if (isset($tagAttributes['handles'])) {
56-
// todo cover by test case
5756
$handles = [$tagAttributes['handles'] => ['method' => $tagAttributes['method'] ?? self::DEFAULT_HANDLER_METHOD]];
5857
} else {
5958
$handles = $this->guessHandledMessages($reflectionClass);
6059
}
6160

6261
foreach ($handles as $messageClassName => $options) {
63-
$methodReflection = $reflectionClass->getNativeMethod($options['method']);
62+
$methodReflection = $reflectionClass->getNativeMethod($options['method'] ?? self::DEFAULT_HANDLER_METHOD);
6463
$variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
6564

6665
$returnTypesMap[$messageClassName][] = $variant->getReturnType();
@@ -84,7 +83,6 @@ public function create(): MessageMap
8483
private function guessHandledMessages(ClassReflection $reflectionClass): iterable
8584
{
8685
if ($reflectionClass->implementsInterface(MessageSubscriberInterface::class)) {
87-
// todo handle different return formats
8886
foreach ($reflectionClass->getName()::getHandledMessages() as $index => $value) {
8987
if (is_int($index) && is_string($value)) {
9088
yield $value => ['method' => self::DEFAULT_HANDLER_METHOD];

tests/Type/Symfony/container.xml

+12
Original file line numberDiff line numberDiff line change
@@ -360,5 +360,17 @@
360360
<service id="multi_query_handler" class="MessengerHandleTrait\MultiQueryHandler">
361361
<tag name="messenger.message_handler"/>
362362
</service>
363+
<service id="tagged_handler" class="MessengerHandleTrait\TaggedHandler">
364+
<tag name="messenger.message_handler" handles="MessengerHandleTrait\TaggedQuery" method="handle"/>
365+
</service>
366+
<service id="multi_handles_for_in_the_same_handler" class="MessengerHandleTrait\MultiHandlesForInTheSameHandler">
367+
<tag name="messenger.message_handler"/>
368+
</service>
369+
<service id="multi_handlers_for_the_same_message_handler_1" class="MessengerHandleTrait\MultiHandlersForTheSameMessageHandler1">
370+
<tag name="messenger.message_handler"/>
371+
</service>
372+
<service id="multi_handlers_for_the_same_message_handler_2" class="MessengerHandleTrait\MultiHandlersForTheSameMessageHandler2">
373+
<tag name="messenger.message_handler"/>
374+
</service>
363375
</services>
364376
</container>

tests/Type/Symfony/data/messenger_handle_trait.php

+53-5
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@ public function __invoke(RegularQuery $query): RegularQueryResult
1616
}
1717
}
1818

19+
class BooleanQuery {}
1920
class StringQuery {}
2021
class IntQuery {}
2122
class FloatQuery {}
2223
class MultiQueryHandler implements MessageSubscriberInterface
2324
{
2425
public static function getHandledMessages(): iterable
2526
{
26-
yield StringQuery::class;
27+
yield BooleanQuery::class;
2728
yield IntQuery::class => ['method' => 'handleInt'];
2829
yield FloatQuery::class => ['method' => 'handleFloat'];
2930
yield StringQuery::class => ['method' => 'handleString'];
3031
}
3132

32-
public function __invoke(StringQuery $query): string
33+
public function __invoke(BooleanQuery $query): bool
3334
{
34-
return 'string result';
35+
return true;
3536
}
3637

3738
public function handleInt(IntQuery $query): int
@@ -52,16 +53,63 @@ public function handleString(StringQuery $query): string
5253
// todo add handle method with union return type?
5354
}
5455

56+
class TaggedQuery {}
57+
class TaggedResult {}
58+
class TaggedHandler
59+
{
60+
public function handle(TaggedQuery $query): TaggedResult
61+
{
62+
return new TaggedResult();
63+
}
64+
}
65+
66+
class MultiHandlesForInTheSameHandlerQuery {}
67+
class MultiHandlesForInTheSameHandler implements MessageSubscriberInterface
68+
{
69+
public static function getHandledMessages(): iterable
70+
{
71+
yield MultiHandlesForInTheSameHandlerQuery::class;
72+
yield MultiHandlesForInTheSameHandlerQuery::class => ['priority' => '0'];
73+
}
74+
75+
public function __invoke(MultiHandlesForInTheSameHandlerQuery $query): bool
76+
{
77+
return true;
78+
}
79+
}
80+
81+
class MultiHandlersForTheSameMessageQuery {}
82+
class MultiHandlersForTheSameMessageHandler1
83+
{
84+
public function __invoke(MultiHandlersForTheSameMessageQuery $query): bool
85+
{
86+
return true;
87+
}
88+
}
89+
class MultiHandlersForTheSameMessageHandler2
90+
{
91+
public function __invoke(MultiHandlersForTheSameMessageQuery $query): bool
92+
{
93+
return false;
94+
}
95+
}
96+
5597
class HandleTraitClass {
5698
use HandleTrait;
5799

58100
public function __invoke()
59101
{
60102
assertType(RegularQueryResult::class, $this->handle(new RegularQuery()));
103+
104+
assertType('bool', $this->handle(new BooleanQuery()));
61105
assertType('int', $this->handle(new IntQuery()));
62106
assertType('float', $this->handle(new FloatQuery()));
107+
assertType('string', $this->handle(new StringQuery()));
108+
109+
assertType(TaggedResult::class, $this->handle(new TaggedQuery()));
63110

64-
// HandleTrait will throw exception in fact due to multiple handlers per single query
65-
assertType('mixed', $this->handle(new StringQuery()));
111+
// HandleTrait will throw exception in fact due to multiple handle methods/handlers per single query
112+
assertType('mixed', $this->handle(new MultiHandlesForInTheSameHandlerQuery()));
113+
assertType('mixed', $this->handle(new MultiHandlersForTheSameMessageQuery()));
66114
}
67115
}

0 commit comments

Comments
 (0)