|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Codeception\Module\Symfony; |
| 6 | + |
| 7 | +use Symfony\Component\Messenger\DataCollector\MessengerDataCollector; |
| 8 | +use Symfony\Component\VarDumper\Cloner\Data; |
| 9 | + |
| 10 | +use function class_exists; |
| 11 | +use function count; |
| 12 | +use function is_string; |
| 13 | +use function sprintf; |
| 14 | + |
| 15 | +trait MessengerAssertionsTrait |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Asserts no message of the given class was dispatched (optionally on a single bus). |
| 19 | + * |
| 20 | + * ```php |
| 21 | + * <?php |
| 22 | + * $I->dontSeeMessageDispatched(SendWelcomeEmail::class); |
| 23 | + * $I->dontSeeMessageDispatched(SendWelcomeEmail::class, 'messenger.bus.default'); |
| 24 | + * ``` |
| 25 | + * |
| 26 | + * @param class-string $messageClass |
| 27 | + */ |
| 28 | + public function dontSeeMessageDispatched(string $messageClass, ?string $bus = null): void |
| 29 | + { |
| 30 | + $this->assertNotContains( |
| 31 | + $messageClass, |
| 32 | + $this->getDispatchedMessageClasses(__FUNCTION__, $bus), |
| 33 | + sprintf("The '%s' message was dispatched%s.", $messageClass, $this->busSuffix($bus)), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the dispatched message class names, in dispatch order (optionally for a single bus). |
| 39 | + * |
| 40 | + * The profiler stores cloned snapshots, so this yields class names, not the message objects. |
| 41 | + * |
| 42 | + * ```php |
| 43 | + * <?php |
| 44 | + * $classes = $I->grabDispatchedMessageClasses(); |
| 45 | + * $classes = $I->grabDispatchedMessageClasses('messenger.bus.default'); |
| 46 | + * ``` |
| 47 | + * |
| 48 | + * @return list<class-string> |
| 49 | + */ |
| 50 | + public function grabDispatchedMessageClasses(?string $bus = null): array |
| 51 | + { |
| 52 | + return $this->getDispatchedMessageClasses(__FUNCTION__, $bus); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Asserts how many messages were dispatched (optionally on a single bus). |
| 57 | + * |
| 58 | + * ```php |
| 59 | + * <?php |
| 60 | + * $I->seeDispatchedMessageCount(1); |
| 61 | + * $I->seeDispatchedMessageCount(2, 'messenger.bus.default'); |
| 62 | + * ``` |
| 63 | + */ |
| 64 | + public function seeDispatchedMessageCount(int $expectedCount, ?string $bus = null): void |
| 65 | + { |
| 66 | + $messages = $this->grabMessengerCollector(__FUNCTION__)->getMessages($bus); |
| 67 | + |
| 68 | + $this->assertCount( |
| 69 | + $expectedCount, |
| 70 | + $messages, |
| 71 | + sprintf( |
| 72 | + 'Expected %d message(s) to be dispatched%s, but %d were.', |
| 73 | + $expectedCount, |
| 74 | + $this->busSuffix($bus), |
| 75 | + count($messages), |
| 76 | + ), |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Asserts at least one message of the given class was dispatched (optionally on a single bus). |
| 82 | + * |
| 83 | + * ```php |
| 84 | + * <?php |
| 85 | + * $I->seeMessageDispatched(SendWelcomeEmail::class); |
| 86 | + * $I->seeMessageDispatched(SendWelcomeEmail::class, 'messenger.bus.default'); |
| 87 | + * ``` |
| 88 | + * |
| 89 | + * @param class-string $messageClass |
| 90 | + */ |
| 91 | + public function seeMessageDispatched(string $messageClass, ?string $bus = null): void |
| 92 | + { |
| 93 | + $this->assertContains( |
| 94 | + $messageClass, |
| 95 | + $this->getDispatchedMessageClasses(__FUNCTION__, $bus), |
| 96 | + sprintf("The '%s' message was not dispatched%s.", $messageClass, $this->busSuffix($bus)), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return list<class-string> |
| 102 | + */ |
| 103 | + private function getDispatchedMessageClasses(string $callingFunction, ?string $bus): array |
| 104 | + { |
| 105 | + $classes = []; |
| 106 | + foreach ($this->grabMessengerCollector($callingFunction)->getMessages($bus) as $entry) { |
| 107 | + if (!$entry instanceof Data) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + $message = $entry['message']; |
| 112 | + $type = $message instanceof Data ? ($message['type'] ?? null) : null; |
| 113 | + if ($type instanceof Data) { |
| 114 | + $type = $type->getValue(); |
| 115 | + } |
| 116 | + |
| 117 | + if (is_string($type) && class_exists($type)) { |
| 118 | + $classes[] = $type; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $classes; |
| 123 | + } |
| 124 | + |
| 125 | + private function busSuffix(?string $bus): string |
| 126 | + { |
| 127 | + return $bus !== null ? sprintf(" on bus '%s'", $bus) : ''; |
| 128 | + } |
| 129 | + |
| 130 | + protected function grabMessengerCollector(string $callingFunction): MessengerDataCollector |
| 131 | + { |
| 132 | + return $this->grabCollector(DataCollectorName::MESSENGER, $callingFunction); |
| 133 | + } |
| 134 | +} |
0 commit comments