> For the complete documentation index, see [llms.txt](https://docs.ecotone.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ecotone.tech/modelling/microservices-php/distributed-bus/distributed-bus-with-service-map/testing.md).

# Testing

Testing Distributed Bus and cross-service messaging

Distributed Bus with Service Map comes with ability to test using real or in memory Message Channels. We can test run the tests using [Ecotone Lite test support](/modelling/microservices-php/distributed-bus/distributed-bus-with-service-map/testing.md).

## Testing Flow between Services

Suppose we do have two different Services **User Service** and **Ticket Service**. From User Service we would like to send Command to create new ticket, whenever something interesting happens.

Therefore using Ecotone Lite we can roll out two different applications and share the channel to test the full flow. Let's start by defining Publishing Service (Application):

```php
# test file

$distributedTicketChannel = SimpleMessageChannelBuilder::createQueueChannel('distributed_ticket');

$userService = EcotoneLite::bootstrapFlowTesting(
            configuration: ServiceConfiguration::createWithDefaults()
                ->withServiceName('user_service')
                ->withExtensionObjects([
                    // distributed service map on the publisher side
                    DistributedServiceMap::initialize()
                        ->withServiceMapping(serviceName: 'ticket_service', channelName: 'distributed_ticket'),
                    // shared in memory channel
                    enableAsynchronousProcessing: [distributedTicketChannel]
                ]),
        )
```

When we do have Publishing Service, we can now set up Consumer side. Suppose we would like to test such Distributed Command Handler:

```php
# application file

class TicketService
{
    #[Distributed]
    #[CommandHandler('ticket.create')]
    public function createTicket(CreateTicket $command, TicketRepository $ticketRepository): void
    {
        $ticket = Ticket::create($command);
        $ticketRepository->save($ticket);
    }
}
```

So let's define the Distributed Consumer

```php
# test file
(...)

$ticketRepository = new InMemoryTicketRepository();
$ticketService = EcotoneLite::bootstrapFlowTesting(
            classesToResolve: [TicketService::class],
            containerOrAvailableServices: [
                TicketService::class => new TicketService(),
                TicketRepository::class => $ticketRepository,            
            ]
            configuration: ServiceConfiguration::createWithDefaults()
                ->withServiceName('ticket_service'),
            enableAsynchronousProcessing: [distributedTicketChannel]
        )
```

As we do have Publishing and Consuming side ready, we can now send Command from User Service.

```php
$userService->getDistributedBus()->convertAndSendCommand(
    'ticket_service',
    'ticket.create',
    new CreateTicket(//data),
);
```

This as a result will send this Command Message to Distributed Message Channel from which we can consume and verify, if our Ticket was stored correctly:

```php
$ticketService->run('ticket_service');
$this->assertSame(
    1,
    $ticketRepository->count(),
);
```

## Testing with real Message Channels and serialization

We may also test out using real production Message Channels, it's simple as switching to different provider:

```php
$distributedTicketChannel = SqsBackedMessageChannelBuilder::create("distributed_ticket")
```

We can also test serialization using [In Memory Message Channels](/modelling/testing-support/testing-asynchronous-messaging.md#testing-serialization).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ecotone.tech/modelling/microservices-php/distributed-bus/distributed-bus-with-service-map/testing.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
