|
| 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_array; |
| 13 | +use function is_string; |
| 14 | +use function sprintf; |
| 15 | + |
| 16 | +trait MessengerAssertionsTrait |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Asserts that the given number of messages were dispatched through the Messenger buses. |
| 20 | + * Optionally pass a bus name (e.g. `messenger.bus.default`) to only count messages dispatched on that bus. |
| 21 | + * |
| 22 | + * ```php |
| 23 | + * <?php |
| 24 | + * $I->assertMessageCount(1); |
| 25 | + * $I->assertMessageCount(2, 'messenger.bus.default'); |
| 26 | + * ``` |
| 27 | + */ |
| 28 | + public function assertMessageCount(int $expectedCount, ?string $bus = null, string $message = ''): void |
| 29 | + { |
| 30 | + $messages = $this->grabMessengerCollector(__FUNCTION__)->getMessages($bus); |
| 31 | + |
| 32 | + $this->assertCount( |
| 33 | + $expectedCount, |
| 34 | + $messages, |
| 35 | + $message !== '' ? $message : sprintf( |
| 36 | + 'Expected %d message(s) to be dispatched%s, but %d were.', |
| 37 | + $expectedCount, |
| 38 | + $this->busSuffix($bus), |
| 39 | + count($messages), |
| 40 | + ), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Asserts that **no** message of the given class was dispatched through the Messenger buses. |
| 46 | + * Optionally restrict the check to a single bus. |
| 47 | + * |
| 48 | + * ```php |
| 49 | + * <?php |
| 50 | + * $I->dontSeeMessageDispatched(App\Message\SendWelcomeEmail::class); |
| 51 | + * $I->dontSeeMessageDispatched(App\Message\SendWelcomeEmail::class, 'messenger.bus.default'); |
| 52 | + * ``` |
| 53 | + * |
| 54 | + * @param class-string $messageClass |
| 55 | + */ |
| 56 | + public function dontSeeMessageDispatched(string $messageClass, ?string $bus = null): void |
| 57 | + { |
| 58 | + $this->assertNotContains( |
| 59 | + $messageClass, |
| 60 | + $this->getDispatchedMessageClasses(__FUNCTION__, $bus), |
| 61 | + sprintf("The '%s' message was dispatched%s.", $messageClass, $this->busSuffix($bus)), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Grabs the fully-qualified **class names** of the messages dispatched through the Messenger buses, |
| 67 | + * in dispatch order. Optionally restrict the result to a single bus. |
| 68 | + * |
| 69 | + * Note: the profiler's messenger collector only stores cloned snapshots, so this returns the message |
| 70 | + * **class names**, not the message objects themselves. |
| 71 | + * |
| 72 | + * ```php |
| 73 | + * <?php |
| 74 | + * $classes = $I->grabDispatchedMessageClasses(); |
| 75 | + * $classes = $I->grabDispatchedMessageClasses('messenger.bus.default'); |
| 76 | + * ``` |
| 77 | + * |
| 78 | + * @return list<class-string> |
| 79 | + */ |
| 80 | + public function grabDispatchedMessageClasses(?string $bus = null): array |
| 81 | + { |
| 82 | + return $this->getDispatchedMessageClasses(__FUNCTION__, $bus); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Asserts that at least one message of the given class was dispatched through the Messenger buses. |
| 87 | + * Optionally restrict the check to a single bus. |
| 88 | + * |
| 89 | + * ```php |
| 90 | + * <?php |
| 91 | + * $I->seeMessageDispatched(App\Message\SendWelcomeEmail::class); |
| 92 | + * $I->seeMessageDispatched(App\Message\SendWelcomeEmail::class, 'messenger.bus.default'); |
| 93 | + * ``` |
| 94 | + * |
| 95 | + * @param class-string $messageClass |
| 96 | + */ |
| 97 | + public function seeMessageDispatched(string $messageClass, ?string $bus = null): void |
| 98 | + { |
| 99 | + $this->assertContains( |
| 100 | + $messageClass, |
| 101 | + $this->getDispatchedMessageClasses(__FUNCTION__, $bus), |
| 102 | + sprintf("The '%s' message was not dispatched%s.", $messageClass, $this->busSuffix($bus)), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return list<class-string> |
| 108 | + */ |
| 109 | + private function getDispatchedMessageClasses(string $callingFunction, ?string $bus): array |
| 110 | + { |
| 111 | + $messages = $this->grabMessengerCollector($callingFunction)->getMessages($bus); |
| 112 | + |
| 113 | + $classes = []; |
| 114 | + foreach ($messages as $entry) { |
| 115 | + // The collector clones each entry into a VarDumper Data tree; normalize it back to a plain |
| 116 | + // array so we can read the message class name (a ClassStub that getValue() unwraps to a string). |
| 117 | + if ($entry instanceof Data) { |
| 118 | + $entry = $entry->getValue(true); |
| 119 | + } |
| 120 | + if (!is_array($entry)) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + $message = $entry['message'] ?? null; |
| 125 | + $type = is_array($message) ? ($message['type'] ?? null) : null; |
| 126 | + if (is_string($type) && class_exists($type)) { |
| 127 | + $classes[] = $type; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return $classes; |
| 132 | + } |
| 133 | + |
| 134 | + private function busSuffix(?string $bus): string |
| 135 | + { |
| 136 | + return $bus !== null ? sprintf(" on bus '%s'", $bus) : ''; |
| 137 | + } |
| 138 | + |
| 139 | + protected function grabMessengerCollector(string $callingFunction): MessengerDataCollector |
| 140 | + { |
| 141 | + return $this->grabCollector(DataCollectorName::MESSENGER, $callingFunction); |
| 142 | + } |
| 143 | +} |
0 commit comments