[Messenger] Move data collector & command into the component

This commit is contained in:
Maxime Steinhausser 2018-04-05 16:42:36 +02:00
parent fb172cae86
commit f9c9ca0514
6 changed files with 148 additions and 12 deletions

View File

@ -69,7 +69,7 @@
<tag name="console.command" command="debug:event-dispatcher" />
</service>
<service id="console.command.messenger_consume_messages" class="Symfony\Bundle\FrameworkBundle\Command\MessengerConsumeMessagesCommand">
<service id="console.command.messenger_consume_messages" class="Symfony\Component\Messenger\Command\ConsumeMessagesCommand">
<argument type="service" id="message_bus" />
<argument type="service" id="messenger.receiver_locator" />

View File

@ -58,7 +58,7 @@
<tag name="monolog.logger" channel="messenger" />
</service>
<service id="data_collector.messenger" class="Symfony\Bundle\FrameworkBundle\DataCollector\MessengerDataCollector">
<service id="data_collector.messenger" class="Symfony\Component\Messenger\DataCollector\MessengerDataCollector">
<tag name="data_collector" template="@WebProfiler/Collector/messenger.html.twig" id="messenger" priority="100" />
<tag name="message_bus_middleware" />
</service>

View File

@ -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 <samuel.roze@gmail.com>
*
* @experimental in 4.1
*/
class MessengerConsumeMessagesCommand extends Command
class ConsumeMessagesCommand extends Command
{
protected static $defaultName = 'messenger:consume-messages';

View File

@ -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 <samuel.roze@gmail.com>
*
* @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(),
);
}

View File

@ -0,0 +1,130 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <maxime.steinhausser@gmail.com>
*/
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 = <<<DUMP
"message" => 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,
<<<DUMP
array:2 [
$messageDump
"result" => array:2 [
"type" => "NULL"
"value" => null
]
]
DUMP
);
yield 'scalar returned value' => array(
'returned value',
<<<DUMP
array:2 [
$messageDump
"result" => array:2 [
"type" => "string"
"value" => "returned value"
]
]
DUMP
);
yield 'array returned value' => array(
array('returned value'),
<<<DUMP
array:2 [
$messageDump
"result" => 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(<<<DUMP
array:2 [
"message" => 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]);
}
}

View File

@ -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."