Important

You are browsing the documentation for version 3.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

Testing

Unit and Functional Tests

To test that a message was sent in unit and functional tests, you can use MessageQueueExtension trait. There are two implementation of this trait, one for unit tests, another for functional tests:

Also, in case if you need custom logic for manage sent messages, you can use Oro\Bundle\MessageQueueBundle\Test\Unit\MessageQueueAssertTrait or Oro\Bundle\MessageQueueBundle\Test\Functional\MessageQueueAssertTrait traits.

Before you start to use traits in functional tests, you need to register oro_message_queue.test.message_collector service for test environment.

1# config/config_test.yml
2
3services:
4    oro_message_queue.test.message_collector:
5        class: Oro\Bundle\MessageQueueBundle\Test\Functional\MessageCollector
6        decorates: oro_message_queue.client.message_producer
7        arguments:
8            - '@oro_message_queue.test.message_collector.inner'

The following example shows how to test whether a message was sent.

<?php
namespace Acme\Bundle\AcmeBundle\Tests\Functional;

use Oro\Bundle\MessageQueueBundle\Test\Functional\MessageQueueExtension;
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;

class SomeTest extends WebTestCase
{
    use MessageQueueExtension;

    public function testSingleMessage()
    {
        // assert that a message was sent to a topic
        self::assertMessageSent('aFooTopic', 'Something has happened');

        // assert that at least one message was sent to a topic
        // can be used if a message is not matter
        self::assertMessageSent('aFooTopic');
    }

    public function testSeveralMessages()
    {
        // assert that exactly given messages were sent to a topic
        self::assertMessagesSent(
            'aFooTopic',
            [
                'Something has happened',
                'Something else has happened',
            ]
        );
        // assert that the exactly given number of messages were sent to a topic
        // can be used if messages are not matter
        self::assertMessagesCount('aFooTopic', 2);
        // also assertCountMessages alias can be used to do the same assertion
        self::assertCountMessages('aFooTopic');
    }

    public function testNoMessages()
    {
        // assert that no any message was sent to a topic
        self::assertMessagesEmpty('aFooTopic');
        // also assertEmptyMessages alias can be used to do the same assertion
        self::assertEmptyMessages('aFooTopic');
    }

    public function testAllMessages()
    {
        // assert that exactly given messages were sent
        // NOTE: use this assertion with caution because it is possible
        // that messages not related to a testing functionality were sent as well
        self::assertAllMessagesSent(
            [
                ['topic' => 'aFooTopic', 'message' => 'Something has happened'],
                ['topic' => 'aFooTopic', 'message' => 'Something else has happened'],
            ]
        );
    }
}

In unit tests you are usually need to pass the message producer to a service you test. To fetch correct instance of message producer in the unit tests use self::getMessageProducer(), e.g.:

<?php
namespace Acme\Bundle\AcmeBundle\Tests\Unit;

use Acme\Bundle\AcmeBundle\SomeClass;
use Oro\Bundle\MessageQueueBundle\Test\Unit\MessageQueueExtension;

class SomeTest extends \PHPUnit_Framework_TestCase
{
    use MessageQueueExtension;

    public function testSingleMessage()
    {
        $instance = new SomeClass(self::getMessageProducer());

        $instance->doSomethind();

        self::assertMessageSent('aFooTopic', 'Something has happened');
    }
}

Create and Run Unit and Functional Tests to Test the Message Queue

To test that a message was sent in unit and functional tests, you can use the AbstractMessageQueueAssertTrait trait. There are two implementations of this trait, one for unit tests and the other one for functional tests:

If you need a custom logic to manage sent messages, you can use the Oro\Bundle\MessageQueueBundle\Test\Unit\MessageQueueAssertTrait or Oro\Bundle\MessageQueueBundle\Test\Functional\MessageQueueAssertTrait traits.

Before you start to use traits in functional tests, register the oro_message_queue.test.message_collector service for the test environment.

1# config/config_test.yml
2
3services:
4    oro_message_queue.test.message_collector:
5        class: Oro\Bundle\MessageQueueBundle\Test\Functional\MessageCollector
6        decorates: oro_message_queue.client.message_producer
7        arguments:
8            - '@oro_message_queue.test.message_collector.inner'

The following example shows how to test whether a message was sent.

 1<?php
 2namespace Acme\Bundle\AcmeBundle\Tests\Functional;
 3
 4use Oro\Bundle\MessageQueueBundle\Test\Functional\MessageQueueExtension;
 5use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
 6
 7class SomeTest extends WebTestCase
 8{
 9    use MessageQueueExtension;
10
11    public function testSingleMessage()
12    {
13        // assert that a message was sent to a topic
14        self::assertMessageSent('aFooTopic', 'Something has happened');
15
16        // assert that at least one message was sent to a topic
17        // can be used if a message is not matter
18        self::assertMessageSent('aFooTopic');
19    }
20
21    public function testSeveralMessages()
22    {
23        // assert that exactly given messages were sent to a topic
24        self::assertMessagesSent(
25            'aFooTopic',
26            [
27                'Something has happened',
28                'Something else has happened',
29            ]
30        );
31        // assert that the exactly given number of messages were sent to a topic
32        // can be used if messages are not matter
33        self::assertMessagesCount('aFooTopic', 2);
34        // also assertCountMessages alias can be used to do the same assertion
35        self::assertCountMessages('aFooTopic');
36    }
37
38    public function testNoMessages()
39    {
40        // assert that no any message was sent to a topic
41        self::assertMessagesEmpty('aFooTopic');
42        // also assertEmptyMessages alias can be used to do the same assertion
43        self::assertEmptyMessages('aFooTopic');
44    }
45
46    public function testAllMessages()
47    {
48        // assert that exactly given messages were sent
49        // NOTE: use this assertion with caution because it is possible
50        // that messages not related to a testing functionality were sent as well
51        self::assertAllMessagesSent(
52            [
53                ['topic' => 'aFooTopic', 'message' => 'Something has happened'],
54                ['topic' => 'aFooTopic', 'message' => 'Something else has happened'],
55            ]
56        );
57    }
58}

In unit tests, you are usually need to pass the message producer to a service you are testong. To fetch the correct instance of the message producer in the unit tests, use self::getMessageProducer().

For example:

 1<?php
 2namespace Acme\Bundle\AcmeBundle\Tests\Unit;
 3
 4use Acme\Bundle\AcmeBundle\SomeClass;
 5use Oro\Bundle\MessageQueueBundle\Test\Unit\MessageQueueExtension;
 6
 7class SomeTest extends \PHPUnit\Framework\TestCase
 8{
 9    use MessageQueueExtension;
10
11    public function testSingleMessage()
12    {
13        $instance = new SomeClass(self::getMessageProducer());
14
15        $instance->doSomethind();
16
17        self::assertMessageSent('aFooTopic', 'Something has happened');
18    }
19}