Skip to content

[Messenger][FrameworkBundle] Move collector, command into the component & minor tweaks #26816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use Symfony\Component\Lock\Store\StoreFactory;
use Symfony\Component\Lock\StoreInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Transport\ReceiverInterface;
use Symfony\Component\Messenger\Transport\SenderInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
Expand Down Expand Up @@ -1436,6 +1437,10 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont

private function registerMessengerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!interface_exists(MessageBusInterface::class)) {
throw new LogicException('Messenger support cannot be enabled as the Messenger component is not installed.');
}

$loader->load('messenger.xml');

$senderLocatorMapping = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$container->loadFromExtension('framework', array(
'validation' => array('enabled' => true),
'messenger' => array(
'middlewares' => array(
'validation' => array(
'enabled' => true,
),
),
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:validation enabled="true"/>
<framework:messenger>
<framework:middlewares>
<framework:validation enabled="true"/>
</framework:middlewares>
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
framework:
validation:
enabled: true
messenger:
middlewares:
validation:
enabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,15 @@ public function testMessenger()
$this->assertFalse($container->hasDefinition('messenger.middleware.doctrine_transaction'));
}

public function testMessengerValidationDisabled()
public function testMessengerValidationEnabled()
{
if (!class_exists(Validation::class)) {
self::markTestSkipped('Skipping tests since Validator component is not installed');
}
$container = $this->createContainerFromFile('messenger_validation_enabled');
$this->assertTrue($definition = $container->hasDefinition('messenger.middleware.validator'));
}

$container = $this->createContainerFromFile('messenger_validation');
public function testMessengerValidationDisabled()
{
$container = $this->createContainerFromFile('messenger_validation_disabled');
$this->assertFalse($container->hasDefinition('messenger.middleware.validator'));
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"symfony/security": "~3.4|~4.0",
"symfony/form": "^4.1",
"symfony/expression-language": "~3.4|~4.0",
"symfony/messenger": "^4.1",
"symfony/process": "~3.4|~4.0",
"symfony/security-core": "~3.4|~4.0",
"symfony/security-csrf": "~3.4|~4.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,8 +24,10 @@

/**
* @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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +18,8 @@

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*
* @experimental in 4.1
*/
class MessengerDataCollector extends DataCollector implements MiddlewareInterface
{
Expand All @@ -26,7 +28,7 @@ class MessengerDataCollector extends DataCollector implements MiddlewareInterfac
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
return $this->data;
// noop
}

/**
Expand Down Expand Up @@ -61,25 +63,25 @@ public function handle($message, callable $next)
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(),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
}
4 changes: 3 additions & 1 deletion src/Symfony/Component/Messenger/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down