Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Add RabbitMQContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Mar 19, 2024
1 parent 79b96e1 commit 5d4d4ae
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Module/RabbitMQ/RabbitMQContainer
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Testcontainers\Module\RabbitMQ;

use AMQPConnection;
use Testcontainers\GenericContainer;

final class RabbitMQContainer extends GenericContainer
{
public function __construct(
string $image = 'rabbitmq:3.8-management',
public readonly string $user = 'guest',
public readonly string $pass = 'guest',
public readonly int $port = 5672
) {
parent::__construct($image);

$this
->withExposedPorts("{$port}/tcp")
->withEnv([
'RABBITMQ_DEFAULT_USER' => $this->user,
'RABBITMQ_DEFAULT_PASS' => $this->pass,
]);
}

public function getDsn(): string
{
return "amqp://{$this->user}:{$this->pass}@{$this->getHost()}:{$this->getFirstMappedPort()}";
}

public function createAmqp(): AMQPConnection
{
$amqp = new AMQPConnection();
$amqp->setUser($this->user);
$amqp->setPassword($this->pass);
$amqp->setHost($this->getHost());
$amqp->setPort($this->getFirstMappedPort());

return $amqp;
}

public function start(int $wait = 15): self
{
return parent::start($wait); // TODO: Properly wait for RabbitMQ to be ready
}
}
85 changes: 85 additions & 0 deletions tests/Module/RabbitMQContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Test\Testcontainers\Module;

use AMQPChannel;
use AMQPEnvelope;
use AMQPExchange;
use AMQPQueue;
use PHPUnit\Framework\TestCase;
use Testcontainers\Module\RabbitMQ\RabbitMQContainer;

/**
* @internal
*/
final class RabbitMQContainerTest extends TestCase
{
private static RabbitMQContainer $container;

private AMQPExchange $exchange;

private AMQPQueue $queue;

public static function setUpBeforeClass(): void
{
if (extension_loaded('amqp') === false) {
self::markTestSkipped('The amqp extension is not installed/enabled.');
}

self::$container = new RabbitMQContainer();
self::$container->start();
}

protected function tearDown(): void
{
self::$container->stop();
}

public function testCreateExchange(): void
{
$this->exchange = new AMQPExchange($this->getChannel());
$this->exchange->setName('testExchange');
$this->exchange->setFlags(AMQP_DURABLE);
$this->exchange->setType(AMQP_EX_TYPE_DIRECT);

self::assertTrue($this->exchange->declareExchange());
}

/**
* @depends testCreateExchange
*/
public function testCreateAndBindQueue(): void
{
$this->queue = new AMQPQueue($channel);
$this->queue->setName('testQueue');
$this->queue->setFlags(AMQP_DURABLE);

self::assertSame(0, $this->queue->declareQueue());
}

/**
* @depends testCreateExchange
* @depends testCreateAndBindQueue
*/
public function testPublishAndConsume(): void
{
self::assertTrue($this->exchange->publish('testMessage', 'routingKey'));

$message = $this->queue->get();
self::assertInstanceOf(AMQPEnvelope::class, $message);
self::assertSame('testMessage', $message->getBody());
self::assertSame('routingKey', $message->getRoutingKey());
}

private function getChannel(): AMQPChannel
{
$amqp = self::$container->createAmqp();
if ($amqp->isConnected() === false) {
$amqp->connect();
}

return new AMQPChannel($amqp);
}
}

0 comments on commit 5d4d4ae

Please sign in to comment.