|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Messenger\Tests\DataCollector; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Messenger\DataCollector\MessengerDataCollector; |
| 16 | +use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; |
| 17 | +use Symfony\Component\VarDumper\Test\VarDumperTestTrait; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> |
| 21 | + */ |
| 22 | +class MessengerDataCollectorTest extends TestCase |
| 23 | +{ |
| 24 | + use VarDumperTestTrait; |
| 25 | + |
| 26 | + /** |
| 27 | + * @dataProvider getHandleTestData |
| 28 | + */ |
| 29 | + public function testHandle($returnedValue, $expected) |
| 30 | + { |
| 31 | + $collector = new MessengerDataCollector(); |
| 32 | + $message = new DummyMessage('dummy message'); |
| 33 | + |
| 34 | + $next = $this->createPartialMock(\stdClass::class, array('__invoke')); |
| 35 | + $next->expects($this->once())->method('__invoke')->with($message)->willReturn($returnedValue); |
| 36 | + |
| 37 | + $this->assertSame($returnedValue, $collector->handle($message, $next)); |
| 38 | + |
| 39 | + $messages = $collector->getMessages(); |
| 40 | + $this->assertCount(1, $messages); |
| 41 | + |
| 42 | + $this->assertDumpMatchesFormat($expected, $messages[0]); |
| 43 | + } |
| 44 | + |
| 45 | + public function getHandleTestData() |
| 46 | + { |
| 47 | + $messageDump = <<<DUMP |
| 48 | + "message" => array:2 [ |
| 49 | + "type" => "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" |
| 50 | + "object" => Symfony\Component\VarDumper\Cloner\Data {%A |
| 51 | + %A+class: "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"%A |
| 52 | + } |
| 53 | + ] |
| 54 | +DUMP; |
| 55 | + |
| 56 | + yield 'no returned value' => array( |
| 57 | + null, |
| 58 | + <<<DUMP |
| 59 | +array:2 [ |
| 60 | +$messageDump |
| 61 | + "result" => array:2 [ |
| 62 | + "type" => "NULL" |
| 63 | + "value" => null |
| 64 | + ] |
| 65 | +] |
| 66 | +DUMP |
| 67 | + ); |
| 68 | + |
| 69 | + yield 'scalar returned value' => array( |
| 70 | + 'returned value', |
| 71 | + <<<DUMP |
| 72 | +array:2 [ |
| 73 | +$messageDump |
| 74 | + "result" => array:2 [ |
| 75 | + "type" => "string" |
| 76 | + "value" => "returned value" |
| 77 | + ] |
| 78 | +] |
| 79 | +DUMP |
| 80 | + ); |
| 81 | + |
| 82 | + yield 'array returned value' => array( |
| 83 | + array('returned value'), |
| 84 | + <<<DUMP |
| 85 | +array:2 [ |
| 86 | +$messageDump |
| 87 | + "result" => array:2 [ |
| 88 | + "type" => "array" |
| 89 | + "object" => Symfony\Component\VarDumper\Cloner\Data {%A |
| 90 | + ] |
| 91 | +] |
| 92 | +DUMP |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + public function testHandleWithException() |
| 97 | + { |
| 98 | + $collector = new MessengerDataCollector(); |
| 99 | + $message = new DummyMessage('dummy message'); |
| 100 | + |
| 101 | + $expectedException = new \RuntimeException('foo'); |
| 102 | + $next = $this->createPartialMock(\stdClass::class, array('__invoke')); |
| 103 | + $next->expects($this->once())->method('__invoke')->with($message)->willThrowException($expectedException); |
| 104 | + |
| 105 | + try { |
| 106 | + $collector->handle($message, $next); |
| 107 | + } catch (\Throwable $actualException) { |
| 108 | + $this->assertSame($expectedException, $actualException); |
| 109 | + } |
| 110 | + |
| 111 | + $messages = $collector->getMessages(); |
| 112 | + $this->assertCount(1, $messages); |
| 113 | + |
| 114 | + $this->assertDumpMatchesFormat(<<<DUMP |
| 115 | +array:2 [ |
| 116 | + "message" => array:2 [ |
| 117 | + "type" => "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" |
| 118 | + "object" => Symfony\Component\VarDumper\Cloner\Data {%A |
| 119 | + %A+class: "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"%A |
| 120 | + } |
| 121 | + ] |
| 122 | + "exception" => array:2 [ |
| 123 | + "type" => "RuntimeException" |
| 124 | + "message" => "foo" |
| 125 | + ] |
| 126 | +] |
| 127 | +DUMP |
| 128 | + , $messages[0]); |
| 129 | + } |
| 130 | +} |
0 commit comments