Skip to content

Commit 894c749

Browse files
committed
Updated the RabbitState class and started to write some tests
1 parent 56bd895 commit 894c749

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"autoload-dev": {
2222
"psr-4": {
2323
"unit\\Kiboko\\Component\\Flow\\RabbitMQ\\": "tests/unit/",
24-
"functional\\Kiboko\\Component\\Flow\\RabbitMQ\\": "tests/functional/"
24+
"functional\\Kiboko\\Component\\Flow\\RabbitMQ\\": "tests/functional/",
25+
"integration\\Kiboko\\Component\\Flow\\RabbitMQ\\": "tests/integration/"
2526
}
2627
},
2728
"config": {

src/RabbitState.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ final class RabbitState implements StateInterface
1010
{
1111
private Client $connection;
1212
private Channel $channel;
13-
private array $metrics;
13+
private \DateTimeInterface $date;
14+
private string $stepUuid;
1415

1516
public function __construct(
1617
private string $host,
@@ -21,6 +22,9 @@ public function __construct(
2122
private ?string $vhost = null,
2223
private ?string $exchange = null,
2324
) {
25+
$this->date = new \DateTime();
26+
$this->stepUuid = $this->generateRandomUuid();
27+
2428
$this->connection = new Client([
2529
'host' => $this->host,
2630
'vhost' => $this->vhost,
@@ -36,19 +40,25 @@ public function __construct(
3640

3741
public function initialize(int $start = 0): void
3842
{
39-
$this->metrics = [
40-
'accept' => 0,
41-
'reject' => 0,
42-
];
4343
}
4444

4545
public function accept(int $step = 1): void
4646
{
47-
$this->metrics['accept'] += $step;
48-
4947
$this->channel->publish(
5048
\json_encode([
51-
'Line accepted : ' => $this->metrics['accept']
49+
'id' => $this->generateRandomUuid(),
50+
'date' => ['date' => $this->date->format('c'), 'tz' => $this->date->getTimezone()->getName()],
51+
'stepsUpdates' => [
52+
[
53+
'code' => $this->stepUuid,
54+
'measurements' => [
55+
'increment' => [
56+
'code' => $this->generateRandomUuid(),
57+
'increment' => $step
58+
]
59+
]
60+
]
61+
]
5262
]),
5363
[
5464
'content-type' => 'application/json',
@@ -60,11 +70,21 @@ public function accept(int $step = 1): void
6070

6171
public function reject(int $step = 1): void
6272
{
63-
$this->metrics['reject'] += $step;
64-
6573
$this->channel->publish(
6674
\json_encode([
67-
'Line rejected : ' => $this->metrics['reject']
75+
'id' => $this->generateRandomUuid(),
76+
'date' => ['date' => $this->date->format('c'), 'tz' => $this->date->getTimezone()->getName()],
77+
'stepsUpdates' => [
78+
[
79+
'code' => $this->stepUuid,
80+
'measurements' => [
81+
'decrement' => [
82+
'code' => $this->generateRandomUuid(),
83+
'decrement' => $step
84+
]
85+
]
86+
]
87+
]
6888
]),
6989
[
7090
'content-type' => 'application/json',
@@ -74,6 +94,16 @@ public function reject(int $step = 1): void
7494
);
7595
}
7696

97+
private function generateRandomUuid(): string
98+
{
99+
$data = $data ?? random_bytes(16);
100+
assert(strlen($data) == 16);
101+
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
102+
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
103+
104+
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
105+
}
106+
77107
public function __destruct()
78108
{
79109
$this->channel->close();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace integration\Kiboko\Component\Flow\RabbitMQ;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class IntegrationTestCase extends TestCase
8+
{
9+
public function testIfJsonIsCorrect()
10+
{
11+
$array = [
12+
'id' => '7bbbf1bd-9f17-4334-855d-ef77747fbc72',
13+
'date' => new \DateTime('now'),
14+
'stepsUpdates' => [
15+
[
16+
'code' => '90ca518b-5d89-4088-a453-ab0c4779aa2d',
17+
'measurements' => [
18+
'increment' => [
19+
'code' => '752602f1-a184-454f-bc48-c745ca7d9241',
20+
'increment' => 1
21+
]
22+
]
23+
]
24+
]
25+
];
26+
27+
$this->assertArrayHasKey('id', $array);
28+
}
29+
}

0 commit comments

Comments
 (0)