From f9c9ca0514a91cde76435df492a0244d81c912d8 Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Thu, 5 Apr 2018 16:42:36 +0200 Subject: [PATCH] [Messenger] Move data collector & command into the component --- .../Resources/config/console.xml | 2 +- .../Resources/config/messenger.xml | 2 +- .../Command/ConsumeMessagesCommand.php} | 6 +- .../DataCollector/MessengerDataCollector.php | 16 ++- .../MessengerDataCollectorTest.php | 130 ++++++++++++++++++ src/Symfony/Component/Messenger/composer.json | 4 +- 6 files changed, 148 insertions(+), 12 deletions(-) rename src/Symfony/{Bundle/FrameworkBundle/Command/MessengerConsumeMessagesCommand.php => Component/Messenger/Command/ConsumeMessagesCommand.php} (95%) rename src/Symfony/{Bundle/FrameworkBundle => Component/Messenger}/DataCollector/MessengerDataCollector.php (87%) create mode 100644 src/Symfony/Component/Messenger/Tests/DataCollector/MessengerDataCollectorTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml index 9c1ed0e163..989da40e00 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml @@ -69,7 +69,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml index 240e2d2c48..1bf97d028d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml @@ -58,7 +58,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/MessengerConsumeMessagesCommand.php b/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php similarity index 95% rename from src/Symfony/Bundle/FrameworkBundle/Command/MessengerConsumeMessagesCommand.php rename to src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php index 73032c88af..1ef2e843ce 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/MessengerConsumeMessagesCommand.php +++ b/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Bundle\FrameworkBundle\Command; +namespace Symfony\Component\Messenger\Command; use Psr\Container\ContainerInterface; use Symfony\Component\Console\Command\Command; @@ -24,8 +24,10 @@ use Symfony\Component\Messenger\Worker; /** * @author Samuel Roze + * + * @experimental in 4.1 */ -class MessengerConsumeMessagesCommand extends Command +class ConsumeMessagesCommand extends Command { protected static $defaultName = 'messenger:consume-messages'; diff --git a/src/Symfony/Bundle/FrameworkBundle/DataCollector/MessengerDataCollector.php b/src/Symfony/Component/Messenger/DataCollector/MessengerDataCollector.php similarity index 87% rename from src/Symfony/Bundle/FrameworkBundle/DataCollector/MessengerDataCollector.php rename to src/Symfony/Component/Messenger/DataCollector/MessengerDataCollector.php index 3430bed848..a9b51b5d37 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DataCollector/MessengerDataCollector.php +++ b/src/Symfony/Component/Messenger/DataCollector/MessengerDataCollector.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Bundle\FrameworkBundle\DataCollector; +namespace Symfony\Component\Messenger\DataCollector; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -18,6 +18,8 @@ use Symfony\Component\Messenger\MiddlewareInterface; /** * @author Samuel Roze + * + * @experimental in 4.1 */ class MessengerDataCollector extends DataCollector implements MiddlewareInterface { @@ -26,7 +28,7 @@ class MessengerDataCollector extends DataCollector implements MiddlewareInterfac */ public function collect(Request $request, Response $response, \Exception $exception = null) { - return $this->data; + // noop } /** @@ -61,25 +63,25 @@ class MessengerDataCollector extends DataCollector implements MiddlewareInterfac try { $result = $next($message); - if (is_object($result)) { + if (\is_object($result)) { $debugRepresentation['result'] = array( - 'type' => get_class($result), + 'type' => \get_class($result), 'object' => $this->cloneVar($result), ); - } elseif (is_array($result)) { + } elseif (\is_array($result)) { $debugRepresentation['result'] = array( 'type' => 'array', 'object' => $this->cloneVar($result), ); } else { $debugRepresentation['result'] = array( - 'type' => gettype($result), + 'type' => \gettype($result), 'value' => $result, ); } } catch (\Throwable $exception) { $debugRepresentation['exception'] = array( - 'type' => get_class($exception), + 'type' => \get_class($exception), 'message' => $exception->getMessage(), ); } diff --git a/src/Symfony/Component/Messenger/Tests/DataCollector/MessengerDataCollectorTest.php b/src/Symfony/Component/Messenger/Tests/DataCollector/MessengerDataCollectorTest.php new file mode 100644 index 0000000000..789b834a97 --- /dev/null +++ b/src/Symfony/Component/Messenger/Tests/DataCollector/MessengerDataCollectorTest.php @@ -0,0 +1,130 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Messenger\Tests\DataCollector; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Messenger\DataCollector\MessengerDataCollector; +use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; +use Symfony\Component\VarDumper\Test\VarDumperTestTrait; + +/** + * @author Maxime Steinhausser + */ +class MessengerDataCollectorTest extends TestCase +{ + use VarDumperTestTrait; + + /** + * @dataProvider getHandleTestData + */ + public function testHandle($returnedValue, $expected) + { + $collector = new MessengerDataCollector(); + $message = new DummyMessage('dummy message'); + + $next = $this->createPartialMock(\stdClass::class, array('__invoke')); + $next->expects($this->once())->method('__invoke')->with($message)->willReturn($returnedValue); + + $this->assertSame($returnedValue, $collector->handle($message, $next)); + + $messages = $collector->getMessages(); + $this->assertCount(1, $messages); + + $this->assertDumpMatchesFormat($expected, $messages[0]); + } + + public function getHandleTestData() + { + $messageDump = << array:2 [ + "type" => "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" + "object" => Symfony\Component\VarDumper\Cloner\Data {%A + %A+class: "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"%A + } + ] +DUMP; + + yield 'no returned value' => array( + null, + << array:2 [ + "type" => "NULL" + "value" => null + ] +] +DUMP + ); + + yield 'scalar returned value' => array( + 'returned value', + << array:2 [ + "type" => "string" + "value" => "returned value" + ] +] +DUMP + ); + + yield 'array returned value' => array( + array('returned value'), + << array:2 [ + "type" => "array" + "object" => Symfony\Component\VarDumper\Cloner\Data {%A + ] +] +DUMP + ); + } + + public function testHandleWithException() + { + $collector = new MessengerDataCollector(); + $message = new DummyMessage('dummy message'); + + $expectedException = new \RuntimeException('foo'); + $next = $this->createPartialMock(\stdClass::class, array('__invoke')); + $next->expects($this->once())->method('__invoke')->with($message)->willThrowException($expectedException); + + try { + $collector->handle($message, $next); + } catch (\Throwable $actualException) { + $this->assertSame($expectedException, $actualException); + } + + $messages = $collector->getMessages(); + $this->assertCount(1, $messages); + + $this->assertDumpMatchesFormat(<< array:2 [ + "type" => "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" + "object" => Symfony\Component\VarDumper\Cloner\Data {%A + %A+class: "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"%A + } + ] + "exception" => array:2 [ + "type" => "RuntimeException" + "message" => "foo" + ] +] +DUMP + , $messages[0]); + } +} diff --git a/src/Symfony/Component/Messenger/composer.json b/src/Symfony/Component/Messenger/composer.json index c4602ed9e9..fb4d8928ce 100644 --- a/src/Symfony/Component/Messenger/composer.json +++ b/src/Symfony/Component/Messenger/composer.json @@ -21,7 +21,9 @@ "require-dev": { "symfony/serializer": "~3.4|~4.0", "symfony/dependency-injection": "~3.4.6|~4.0", - "symfony/property-access": "~3.4|~4.0" + "symfony/http-kernel": "~3.4|~4.0", + "symfony/property-access": "~3.4|~4.0", + "symfony/var-dumper": "~3.4|~4.0" }, "suggest": { "sroze/enqueue-bridge": "For using the php-enqueue library as an adapter."