Skip to content

Commit

Permalink
Merge pull request #366 from tienvx/update-message-example
Browse files Browse the repository at this point in the history
chore: Clean up and use matchers/generators for message example
  • Loading branch information
tienvx committed Nov 29, 2023
2 parents a7fe1a1 + d2d02fd commit d86b8c2
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 146 deletions.
17 changes: 5 additions & 12 deletions example/message/consumer/src/ExampleMessageConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@

class ExampleMessageConsumer
{
public function ProcessText(string $message): object
public function processMessage(string $message): void
{
$obj = \json_decode($message);
print ' [x] Processed ' . \print_r($obj->contents->text, true) . "\n";

return $obj;
}

public function ProcessSong(string $message): object
{
$obj = \json_decode($message);
print ' [x] Processed ' . \print_r($obj->contents->song, true) . "\n";

return $obj;
print " [x] Processing \n";
print ' [x] Text: ' . \print_r($obj->contents->text, true) . "\n";
print ' [x] Number: ' . \print_r($obj->contents->number, true) . "\n";
print " [x] Processed \n";
}
}
2 changes: 1 addition & 1 deletion example/message/consumer/src/receive.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$callback = function ($msg) {
// process that invokes the use of the message
$processor = new MessageConsumer\ExampleMessageConsumer();
$processor->ProcessText($msg->body);
$processor->processMessage($msg->body);

$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};
Expand Down
38 changes: 9 additions & 29 deletions example/message/consumer/tests/ExampleMessageConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
use MessageConsumer\ExampleMessageConsumer;
use PhpPact\Consumer\MessageBuilder;
use PhpPact\Config\PactConfigInterface;
use PhpPact\Consumer\Matcher\Matcher;
use PhpPact\Standalone\PactMessage\PactMessageConfig;
use PHPUnit\Framework\TestCase;
use stdClass;

class ExampleMessageConsumerTest extends TestCase
{
private static PactConfigInterface $config;
private Matcher $matcher;

public static function setUpBeforeClass(): void
{
Expand All @@ -27,6 +29,11 @@ public static function setUpBeforeClass(): void
}
}

public function setUp(): void
{
$this->matcher = new Matcher();
}

/**
* @throws Exception
*/
Expand All @@ -36,6 +43,7 @@ public function testProcessText()

$contents = new stdClass();
$contents->text = 'Hello Mary';
$contents->number = $this->matcher->integerV3();

$metadata = ['queue' => 'wind cries', 'routing_key' => 'wind cries'];

Expand All @@ -47,35 +55,7 @@ public function testProcessText()

// established mechanism to this via callbacks
$consumerMessage = new ExampleMessageConsumer();
$callback = [$consumerMessage, 'ProcessText'];
$builder->setCallback($callback);

$verifyResult = $builder->verify();

$this->assertTrue($verifyResult);
}

/**
* @throws Exception
*/
public function testProcessSong()
{
$builder = new MessageBuilder(self::$config);

$contents = new stdClass();
$contents->song = 'And the wind whispers Mary';

$metadata = ['queue' => 'And the clowns have all gone to bed', 'routing_key' => 'And the clowns have all gone to bed'];

$builder
->given('You can hear happiness staggering on down the street')
->expectsToReceive('footprints dressed in red')
->withMetadata($metadata)
->withContent($contents);

// established mechanism to this via callbacks
$consumerMessage = new ExampleMessageConsumer();
$callback = [$consumerMessage, 'ProcessSong'];
$callback = [$consumerMessage, 'processMessage'];
$builder->setCallback($callback);

$verifyResult = $builder->verify();
Expand Down
40 changes: 23 additions & 17 deletions example/message/pacts/messageConsumer-messageProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@
"messages": [
{
"contents": {
"song": "And the wind whispers Mary"
"number": null,
"text": "Hello Mary"
},
"description": "footprints dressed in red",
"metadata": {
"contentType": "application/json",
"queue": "And the clowns have all gone to bed",
"routing_key": "And the clowns have all gone to bed"
"description": "an alligator named Mary exists",
"generators": {
"body": {
"$.number": {
"max": 10,
"min": 0,
"type": "RandomInt"
}
}
},
"providerStates": [
{
"name": "You can hear happiness staggering on down the street"
"matchingRules": {
"body": {
"$.number": {
"combine": "AND",
"matchers": [
{
"match": "integer"
}
]
}
}
]
},
{
"contents": {
"text": "Hello Mary"
},
"description": "an alligator named Mary exists",
"metadata": {
"contentType": "application/json",
"queue": "wind cries",
Expand All @@ -41,8 +47,8 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.9",
"models": "1.1.11"
"ffi": "0.4.10",
"models": "1.1.12"
},
"pactSpecification": {
"version": "3.0.0"
Expand Down
28 changes: 28 additions & 0 deletions example/message/provider/src/ExampleMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace MessageProvider;

class ExampleMessage
{
public function __construct(private mixed $contents, private array $metadata = [])
{
}

public function getMetadata(): array
{
return $this->metadata;
}

public function getContents(): mixed
{
return $this->contents;
}

public function __toString(): string
{
return json_encode([
'metadata' => $this->metadata,
'contents' => $this->contents,
]);
}
}
51 changes: 0 additions & 51 deletions example/message/provider/src/ExampleMessageProvider.php

This file was deleted.

45 changes: 13 additions & 32 deletions example/message/provider/src/ExampleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,28 @@

namespace MessageProvider;

use MessageProvider\ExampleMessageProvider;

class ExampleProvider
{
private array $messages;
private array $message = [
'metadata' => [
'queue' => 'wind cries',
'routing_key' => 'wind cries',
],
'contents' => [
'text' => 'Hello Mary',
'number' => 123,
]
];

private array $currentState = [];

public function __construct()
{
$this->messages = [
'an alligator named Mary exists' => [
'metadata' => [
'queue' => 'wind cries',
'routing_key' => 'wind cries',
],
'contents' => [
'text' => 'Hello Mary',
]
],
'footprints dressed in red' => [
'metadata' => [
'queue' => 'And the clowns have all gone to bed',
'routing_key' => 'And the clowns have all gone to bed',
],
'contents' => [
'song' => 'And the wind whispers Mary',
]
],
];
}

public function dispatchMessage(string $description, array $providerStates): ?ExampleMessageProvider
public function dispatchMessage(string $description, array $providerStates): ?ExampleMessage
{
if (!isset($this->messages[$description])) {
if ($description !== 'an alligator named Mary exists') {
return null;
}

return (new ExampleMessageProvider())
->setMetadata($this->messages[$description]['metadata'])
->setContents($this->messages[$description]['contents']);
return (new ExampleMessage($this->message['contents'], $this->message['metadata']));
}

public function changeSate(string $action, string $state, array $params): void
Expand Down
8 changes: 4 additions & 4 deletions example/message/provider/src/send.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?php

require_once __DIR__ . '/../../../vendor/autoload.php';
require_once __DIR__ . '/ExampleMessageProvider.php';

use MessageProvider\ExampleMessage;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');

// build the message with appropriate metadata
$providerMessage = new \MessageProvider\ExampleMessageProvider(['queue' => 'myKey', 'routing_key' => 'myKey']);
$content = new \stdClass();
$content->text = 'Hello Mary';
$providerMessage->setContents($content);
$metadata = ['queue' => 'myKey', 'routing_key' => 'myKey'];
$providerMessage = new ExampleMessage($content, $metadata);

$channel = $connection->channel();
$channel->queue_declare($providerMessage->getMetadata()['queue'], false, false, false, false);

// transform message to AMQP
$msg = new AMQPMessage($providerMessage->Build());
$msg = new AMQPMessage($providerMessage);

// publish it
$channel->basic_publish($msg, '', $providerMessage->getMetadata()['routing_key']);
Expand Down

0 comments on commit d86b8c2

Please sign in to comment.