This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79b96e1
commit 5d4d4ae
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |