<commit_before><?php
/**
 * This file is part of the prooph/service-bus.
 * (c) 2014-2016 prooph software GmbH <<EMAIL>>
 * (c) 2015-2016 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace ProophTest\ServiceBus\Plugin;

use PHPUnit\Framework\TestCase;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\DefaultActionEvent;
use Prooph\Common\Messaging\MessageFactory;
use Prooph\ServiceBus\CommandBus;
use Prooph\ServiceBus\MessageBus;
use Prooph\ServiceBus\Plugin\MessageFactoryPlugin;
use ProophTest\ServiceBus\Mock\DoSomething;
use Prophecy\Argument;

class MessageFactoryPluginTest extends TestCase
{
    /**
     * @test
     */
    public function it_turns_a_message_given_as_array_into_a_message_object_using_a_factory(): void
    {
        $messageFactory = $this->prophesize(MessageFactory::class);

        $messageFactory->createMessageFromArray('custom-message', Argument::any())->will(function ($args): DoSomething {
            list($messageName, $messageArr) = $args;

            return new DoSomething($messageArr['payload']);
        });

        $factoryPlugin = new MessageFactoryPlugin($messageFactory->reveal());

        $actionEvent = new DefaultActionEvent(
            MessageBus::EVENT_DISPATCH,
            new CommandBus(),
            [
                //We provide message as array containing a "message_name" key because only in this case the factory plugin
                //gets active
                MessageBus::EVENT_PARAM_MESSAGE => [
                    'message_name' => 'custom-message',
                    'payload' => ['some data'],
                ],
            ]
        );

        $factoryPlugin($actionEvent);

        $message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE);

        $this->assertInstanceOf(DoSomething::class, $message);
        $this->assertEquals(['some data'], $message->payload());
    }

    /**
     * @test
     */
    public function it_will_return_eary_if_message_name_not_present_in_message(): void
    {
        $messageFactoryMock = $this->getMockForAbstractClass(MessageFactory::class);
        $messageFactoryMock
            ->expects($this->never())
            ->method('createMessageFromArray');

        $actionEventMock = $this->getMockForAbstractClass(ActionEvent::class);
        $actionEventMock
            ->expects($this->once())
            ->method('getParam')
            ->with(MessageBus::EVENT_PARAM_MESSAGE)
            ->will($this->returnValue([]));

        $messagePlugin = new MessageFactoryPlugin($messageFactoryMock);
        $messagePlugin($actionEventMock);
    }
}
<commit_msg>update message factory plugin test<commit_after><?php
/**
 * This file is part of the prooph/service-bus.
 * (c) 2014-2016 prooph software GmbH <<EMAIL>>
 * (c) 2015-2016 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace ProophTest\ServiceBus\Plugin;

use PHPUnit\Framework\TestCase;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\DefaultActionEvent;
use Prooph\Common\Messaging\MessageFactory;
use Prooph\ServiceBus\CommandBus;
use Prooph\ServiceBus\Exception\MessageDispatchException;
use Prooph\ServiceBus\MessageBus;
use Prooph\ServiceBus\Plugin\MessageFactoryPlugin;
use ProophTest\ServiceBus\Mock\DoSomething;
use Prophecy\Argument;

class MessageFactoryPluginTest extends TestCase
{
    /**
     * @test
     */
    public function it_turns_a_message_given_as_array_into_a_message_object_using_a_factory(): void
    {
        $commandBus = new CommandBus();
        $messageFactory = $this->prophesize(MessageFactory::class);

        $messageFactory->createMessageFromArray('custom-message', Argument::any())->will(function ($args): DoSomething {
            list($messageName, $messageArr) = $args;

            return new DoSomething($messageArr['payload']);
        });

        $factoryPlugin = new MessageFactoryPlugin($messageFactory->reveal());
        $factoryPlugin->attachToMessageBus($commandBus);


        $commandBus->attach(
            CommandBus::EVENT_FINALIZE,
            function (ActionEvent $actionEvent): void {
                $message = $actionEvent->getParam(CommandBus::EVENT_PARAM_MESSAGE);
                $this->assertInstanceOf(DoSomething::class, $message);
                $this->assertEquals(['some data'], $message->payload());
            }
        );

        try {
            $commandBus->dispatch([
                'message_name' => 'custom-message',
                'payload' => ['some data'],
            ]);
        } catch (MessageDispatchException $exception) {
            // ignore
        }
    }

    /**
     * @test
     */
    public function it_will_return_eary_if_message_name_not_present_in_message(): void
    {
        $commandBus = new CommandBus();
        $messageFactory = $this->prophesize(MessageFactory::class);

        $factoryPlugin = new MessageFactoryPlugin($messageFactory->reveal());
        $factoryPlugin->attachToMessageBus($commandBus);

        $commandBus->attach(
            CommandBus::EVENT_FINALIZE,
            function (ActionEvent $actionEvent): void {
                $message = $actionEvent->getParam(CommandBus::EVENT_PARAM_MESSAGE);
                $this->assertEquals(
                    [
                        'payload' => ['some data'],
                    ],
                    $message)
                ;
            }
        );

        try {
            $commandBus->dispatch([
                'payload' => ['some data'],
            ]);
        } catch (MessageDispatchException $exception) {
            // ignore
        }
    }
}

{'fraction_non_alphanumeric': 0.1182611672086257, 'fraction_numerical': 0.0058189286325517715, 'mean_word_length': 2.67316153362665, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6190089', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1529, 'n_words': 294}