Skip to content

Commit 0592dd4

Browse files
authored
1.x dev (#5)
* added rabbitmq amqp support
1 parent 7bf176b commit 0592dd4

25 files changed

+767
-70
lines changed

.integration-testing.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
"port": 5672,
2626
"user": "test",
2727
"password": "test",
28-
"vhost": "test",
28+
"vhost": "/",
2929
"fixtures": {
3030
"beforeFirstTest": {
3131
"purgeQueues": [
32-
"test-queue"
32+
"before-first-test-queue"
3333
],
3434
"publishMessages": [
3535
{
3636
"exchange": "test-exchange",
37-
"queue": "test-queue",
37+
"queue": "before-first-test-queue",
3838
"routing_key": "before-first-test",
3939
"path": "tests/fixtures/before-first-test",
4040
"extension": "json"
@@ -43,26 +43,24 @@
4343
},
4444
"beforeTest": {
4545
"purgeQueues": [
46-
"test-queue"
46+
"before-test-queue"
4747
],
4848
"publishMessages": [
4949
{
5050
"exchange": "test-exchange",
51-
"queue": "test-queue",
51+
"queue": "before-test-queue",
5252
"routing_key": "before-test",
5353
"path": "tests/fixtures/before-test"
5454
}
5555
]
5656
},
5757
"afterTest": {
5858
"purgeQueues": [
59-
"test-queue"
59+
"before-test-queue"
6060
]
6161
},
6262
"afterLastTest": {
63-
"purgeQueues": [
64-
"test-queue"
65-
]
63+
"purgeQueues": []
6664
}
6765
}
6866
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ test-integration: up
1818
merge-coverage:
1919
vendor/bin/phpcov merge --clover build/coverage/merged.xml build/coverage
2020
debug-test-integration:
21-
php -dxdebug.remote_mode=jit vendor/bin/phpunit --no-coverage --color --testdox --group debug -c phpunit-integration.xml.dist
21+
php vendor/bin/phpunit --no-coverage --color --testdox --group debug -c phpunit-integration.xml.dist

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,23 @@
2121
"prefer-stable": true,
2222
"require": {
2323
"php": "^7.3",
24-
"ext-pdo": "*",
25-
"ext-json": "*"
24+
"ext-json": "*",
25+
"ext-pdo": "*"
2626
},
2727
"require-dev": {
2828
"ext-json": "*",
2929
"ext-mbstring": "*",
3030
"ext-pdo_mysql": "*",
3131
"ext-xml": "*",
3232
"friendsofphp/php-cs-fixer": "^2.16",
33+
"php-amqplib/php-amqplib": "^2.11",
3334
"phpunit/phpcov": "^6.0",
3435
"phpunit/phpunit": "^8.5",
3536
"squizlabs/php_codesniffer": "^3.5"
3637
},
3738
"suggest": {
38-
"ext-pdo_mysql": "For PDO related integration tests"
39+
"ext-pdo_mysql": "For PDO related integration tests",
40+
"php-amqplib/php-amqplib": "For AMQP related integration tests"
3941
},
4042
"autoload": {
4143
"psr-4": {

docker-compose.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ services:
3232
environment:
3333
- RABBITMQ_VM_MEMORY_HIGH_WATERMARK=0.5
3434
- RABBITMQ_ERLANG_COOKIE=secret
35-
- RABBITMQ_DEFAULT_USER=user
36-
- RABBITMQ_DEFAULT_PASS=password
37-
- RABBITMQ_DEFAULT_VHOST=test
35+
- RABBITMQ_DEFAULT_USER=test
36+
- RABBITMQ_DEFAULT_PASS=test
37+
- RABBITMQ_DEFAULT_VHOST=/
3838
tmpfs:
3939
- /var/lib/rabbitmq
4040
ports:
4141
- 5672:5672
4242
- 15672:15672
43+
volumes:
44+
- $PWD/docker/rabbitmq/enabled_plugins:/etc/rabbitmq/enabled_plugins

docker/rabbitmq/enabled_plugins

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[rabbitmq_management].

phpunit-integration.xml.dist

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.0/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
4+
bootstrap="tests/integration/Bootstrap.php"
55
executionOrder="depends,defects"
66
forceCoversAnnotation="true"
77
beStrictAboutCoversAnnotation="true"
@@ -13,6 +13,10 @@
1313
<const name="DB_DSN" value="mysql:host=localhost:3306;dbname=test;charset=utf8"/>
1414
<const name="DB_USERNAME" value="test"/>
1515
<const name="DB_PASSWORD" value="test"/>
16+
<const name="RABBITMQ_HOST" value="localhost"/>
17+
<const name="RABBITMQ_PORT" value="5672"/>
18+
<const name="RABBITMQ_USER" value="test"/>
19+
<const name="RABBITMQ_PASSWORD" value="test"/>
1620
</php>
1721
<testsuites>
1822
<testsuite name="integration">

src/Driver/AMQPConnection.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace IntegrationTesting\Driver;
4+
5+
use PhpAmqpLib\Channel\AMQPChannel;
6+
use PhpAmqpLib\Connection\AMQPStreamConnection;
7+
8+
/**
9+
* Class AMQPConnection
10+
* @package IntegrationTesting\Driver
11+
* @internal NOT FOR PUBLIC USE
12+
*/
13+
class AMQPConnection
14+
{
15+
/**
16+
* @var AMQPStreamConnection
17+
*/
18+
private $connection;
19+
20+
private function __construct(
21+
AMQPStreamConnection $connection
22+
) {
23+
$this->connection = $connection;
24+
$this->connection->set_close_on_destruct(true);
25+
}
26+
27+
public static function create(
28+
string $host,
29+
int $port,
30+
string $user,
31+
string $password,
32+
string $vHost = '/',
33+
bool $insist = false,
34+
string $loginMethod = 'AMQPLAIN',
35+
string $locale = 'en_US',
36+
float $connectionTimeout = 3.0,
37+
float $readWriteTimeout = 3.0,
38+
bool $keepAlive = false,
39+
int $heartbeat = 0
40+
): self {
41+
return new self(
42+
new AMQPStreamConnection(
43+
$host,
44+
$port,
45+
$user,
46+
$password,
47+
$vHost,
48+
$insist,
49+
$loginMethod,
50+
null,
51+
$locale,
52+
$connectionTimeout,
53+
$readWriteTimeout,
54+
null,
55+
$keepAlive,
56+
$heartbeat
57+
)
58+
);
59+
}
60+
61+
public function connect(): void
62+
{
63+
if (!$this->connection->isConnected()) {
64+
$this->connection->reconnect();
65+
}
66+
}
67+
68+
public function disconnect(): void
69+
{
70+
if ($this->connection->isConnected()) {
71+
$this->connection->close();
72+
}
73+
}
74+
75+
/**
76+
* @param int $channelId
77+
* @return AMQPChannel
78+
*/
79+
public function getChannel(int $channelId = null): AMQPChannel
80+
{
81+
return $this->connection->channel($channelId);
82+
}
83+
}

src/Driver/AMQPService.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace IntegrationTesting\Driver;
4+
5+
use PhpAmqpLib\Channel\AMQPChannel;
6+
7+
/**
8+
* Interface AMQPService
9+
* @package IntegrationTesting\Driver
10+
* @internal NOT FOR PUBLIC USE
11+
*/
12+
interface AMQPService
13+
{
14+
public function createChannel(): AMQPChannel;
15+
16+
public function createDurableTopicExchanges(array $exchangeNames): void;
17+
18+
public function createDurableTopicExchange(string $name, array $arguments = []): AMQPChannel;
19+
20+
public function forceDeleteExchange(string $name): AMQPChannel;
21+
22+
public function forceDeleteQueue(string $name): AMQPChannel;
23+
24+
public function purgeQueue(string $name): AMQPChannel;
25+
26+
public function disconnect(): void;
27+
28+
public function createDurableQueue(string $name, array $arguments = []): AMQPChannel;
29+
30+
public function createDurableQueues(array $queueNames): void;
31+
32+
public function bindExchangeToQueueByRoutingKeys(string $exchangeName, string $queueName, array $routingKeys): void;
33+
34+
public function publishMessage(AMQPChannel $channel, string $body, array $properties, string $exchange, string $routingKey): void;
35+
36+
public function consumeMessage(AMQPChannel $channel, string $consumerTag, string $queue, callable $callback): void;
37+
}

src/Driver/FileSystem.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use IntegrationTesting\Exception\TestingException;
88
use Iterator;
99

10+
/**
11+
* Class FileSystem
12+
* @package IntegrationTesting\Driver
13+
* @internal NOT FOR PUBLIC USE
14+
*/
1015
class FileSystem
1116
{
1217
/**
@@ -53,11 +58,12 @@ public function getFileListIteratorFromPathByExtension(string $path, string $ext
5358
/**
5459
* @param Iterator $iterator
5560
* @param callable $callback
61+
* @throws TestingException
5662
*/
5763
public function runCallbackOnEachFileIteratorContents(Iterator $iterator, callable $callback): void
5864
{
5965
foreach ($iterator as $filePath) {
60-
$callback(file_get_contents($filePath));
66+
$callback($this->getFileContents($filePath));
6167
}
6268
}
6369
}

src/Driver/PDOConnection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use PDO;
66

7+
/**
8+
* Class PDOConnection
9+
* @package IntegrationTesting\Driver
10+
* @internal NOT FOR PUBLIC USE
11+
*/
712
class PDOConnection
813
{
914
private $PDO;

0 commit comments

Comments
 (0)