Skip to content

Commit

Permalink
Merge pull request #331 from tienvx/reorganize-examples
Browse files Browse the repository at this point in the history
chore: reorganize examples (create separate directory for each example)
  • Loading branch information
YOU54F committed Sep 12, 2023
2 parents 8a3581b + 2f5baac commit 3e1a78e
Show file tree
Hide file tree
Showing 31 changed files with 254 additions and 191 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ All of the following code will be used exclusively for the Consumer.

Create a standard PHPUnit test case class and function.

[Click here](/example/tests/Consumer/Service/ConsumerServiceHelloTest.php) to see the full sample file.
[Click here](/example/json/consumer/tests/Service/ConsumerServiceHelloTest.php) to see the full sample file.

### Create Mock Request

Expand Down Expand Up @@ -381,7 +381,7 @@ to processing class. Aside from changing default ports, this should be transpa
Both the provider and consumer side make heavy use of lambda functions.

### Consumer Side Message Processing
The examples provided are pretty basic. See examples\tests\MessageConsumer.
The examples provided are pretty basic. See [example](/example/message/consumer/tests/ExampleMessageConsumerTest.php).
1. Create the content and metadata (array)
1. Annotate the MessageBuilder appropriate content and states
1. Given = Provider State
Expand Down Expand Up @@ -425,7 +425,7 @@ Handle these requests on your provider:
1. Return message's content in body
2. Return message's metadata in header `PACT-MESSAGE-METADATA`

[Click here](/example/src/Provider/public/index.php) to see the full sample file.
[Click here](/example/message/provider/public/index.php) to see the full sample file.

## Usage for the optional `pact-stub-service`

Expand Down
27 changes: 10 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,14 @@
"autoload-dev": {
"psr-4": {
"PhpPactTest\\": "tests/PhpPact",
"Consumer\\": [
"example/src/Consumer",
"example/tests/Consumer"
],
"MessageConsumer\\": [
"example/src/MessageConsumer",
"example/tests/MessageConsumer"
],
"MessageProvider\\": [
"example/src/MessageProvider",
"example/tests/MessageProvider"
],
"Provider\\": [
"example/src/Provider"
]
"JsonConsumer\\": "example/json/consumer/src",
"JsonConsumer\\Tests\\": "example/json/consumer/tests",
"JsonProvider\\": "example/json/provider/src",
"JsonProvider\\Tests\\": "example/json/provider/tests",
"MessageConsumer\\": "example/message/consumer/src",
"MessageConsumer\\Tests\\": "example/message/consumer/tests",
"MessageProvider\\": "example/message/provider/src",
"MessageProvider\\Tests\\": "example/message/provider/tests"
}
},
"scripts": {
Expand All @@ -65,8 +58,8 @@
"lint": "php-cs-fixer fix --dry-run",
"fix": "php-cs-fixer fix",
"test": [
"php -r \"array_map('unlink', glob('./example/output/*.json'));\"",
"phpunit --debug -c example/phpunit.all.xml"
"php -r \"array_map('unlink', glob('./example/*/pacts/*.json'));\"",
"phpunit --debug"
]
},
"extra": {
Expand Down
24 changes: 0 additions & 24 deletions example/README.md

This file was deleted.

11 changes: 11 additions & 0 deletions example/json/consumer/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="PhpPact Example Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="PACT_LOGLEVEL" value="DEBUG"/>
</php>
</phpunit>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Consumer\Service;
namespace JsonConsumer\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

namespace Consumer\Service;
namespace JsonConsumer\Tests\Service;

use JsonConsumer\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Standalone\Exception\MissingEnvVariableException;
use PhpPact\Standalone\MockService\MockServerEnvConfig;
use PhpPact\Standalone\MockService\MockServerConfig;
use PHPUnit\Framework\TestCase;

class ConsumerServiceGoodbyeTest extends TestCase
{
/**
* @throws MissingEnvVariableException
*/
public function testGetGoodbyeString()
{
$request = new ConsumerRequest();
Expand All @@ -30,8 +27,15 @@ public function testGetGoodbyeString()
'message' => 'Goodbye, Bob'
]);

$config = new MockServerEnvConfig();
$builder = new InteractionBuilder($config);
$config = new MockServerConfig();
$config
->setConsumer('jsonConsumer')
->setProvider('jsonProvider')
->setPactDir(__DIR__.'/../../../pacts');
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($logLevel);
}
$builder = new InteractionBuilder($config);
$builder
->given('Get Goodbye')
->uponReceiving('A get request to /goodbye/{name}')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Consumer\Service;
namespace JsonConsumer\Tests\Service;

use JsonConsumer\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Matcher\Matcher;
use PhpPact\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Standalone\MockService\MockServerEnvConfig;
use PhpPact\Standalone\MockService\MockServerConfig;
use PHPUnit\Framework\TestCase;

class ConsumerServiceHelloTest extends TestCase
Expand Down Expand Up @@ -37,7 +38,14 @@ public function testGetHelloString()
]);

// Create a configuration that reflects the server that was started. You can create a custom MockServerConfigInterface if needed.
$config = new MockServerEnvConfig();
$config = new MockServerConfig();
$config
->setConsumer('jsonConsumer')
->setProvider('jsonProvider')
->setPactDir(__DIR__.'/../../../pacts');
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($logLevel);
}
$builder = new InteractionBuilder($config);
$builder
->uponReceiving('A get request to /hello/{name}')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"consumer": {
"name": "someConsumer"
"name": "jsonConsumer"
},
"interactions": [
{
Expand Down Expand Up @@ -63,15 +63,15 @@
],
"metadata": {
"pactRust": {
"ffi": "0.4.4",
"mockserver": "1.0.3",
"models": "1.0.13"
"ffi": "0.4.7",
"mockserver": "1.2.3",
"models": "1.1.9"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "someProvider"
"name": "jsonProvider"
}
}
11 changes: 11 additions & 0 deletions example/json/provider/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="PhpPact Example Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="PACT_LOGLEVEL" value="DEBUG"/>
</php>
</phpunit>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Provider\ExampleProvider;
use JsonProvider\ExampleProvider;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
Expand All @@ -26,20 +26,6 @@
return $response->withHeader('Content-Type', 'application/json');
});

$app->post('/pact-messages', function (Request $request, Response $response) use ($provider) {
$body = $request->getParsedBody();
$message = $provider->dispatchMessage($body['description'], $body['providerStates']);
if ($message) {
$response->getBody()->write(\json_encode($message->getContents()));

return $response
->withHeader('Content-Type', 'application/json')
->withHeader('Pact-Message-Metadata', \base64_encode(\json_encode($message->getMetadata())));
}

return $response;
});

$app->post('/pact-change-state', function (Request $request, Response $response) use ($provider) {
$body = $request->getParsedBody();
$provider->changeSate($body['action'], $body['state'], $body['params']);
Expand Down
27 changes: 27 additions & 0 deletions example/json/provider/src/ExampleProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace JsonProvider;

class ExampleProvider
{
private array $currentState = [];

public function sayHello(string $name): string
{
return "Hello, {$name}";
}

public function sayGoodbye(string $name): string
{
return "Goodbye, {$name}";
}

public function changeSate(string $action, string $state, array $params): void
{
$this->currentState = [
'action' => $action,
'state' => $state,
'params' => $params,
];
}
}
68 changes: 68 additions & 0 deletions example/json/provider/tests/PactVerifyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace JsonProvider\Tests;

use GuzzleHttp\Psr7\Uri;
use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig;
use PhpPact\Standalone\ProviderVerifier\Verifier;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

class PactVerifyTest extends TestCase
{
private Process $process;

/**
* Run the PHP build-in web server.
*/
protected function setUp(): void
{
$publicPath = __DIR__ . '/../public/';

$this->process = new Process(['php', '-S', '127.0.0.1:7202', '-t', $publicPath]);

$this->process->start();
$this->process->waitUntil(function (): bool {
$fp = @fsockopen('127.0.0.1', 7202);
$isOpen = is_resource($fp);
if ($isOpen) {
fclose($fp);
}

return $isOpen;
});
}

/**
* Stop the web server process once complete.
*/
protected function tearDown(): void
{
$this->process->stop();
}

/**
* This test will run after the web server is started.
*/
public function testPactVerifyConsumer()
{
$config = new VerifierConfig();
$config->getProviderInfo()
->setName('jsonProvider') // Providers name to fetch.
->setHost('localhost')
->setPort(7202);
$config->getProviderState()
->setStateChangeUrl(new Uri('http://localhost:7202/pact-change-state'))
;
if ($level = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($level);
}

$verifier = new Verifier($config);
$verifier->addFile(__DIR__ . '/../../pacts/jsonConsumer-jsonProvider.json');

$verifyResult = $verifier->verify();

$this->assertTrue($verifyResult);
}
}
11 changes: 11 additions & 0 deletions example/message/consumer/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="PhpPact Example Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="PACT_LOGLEVEL" value="DEBUG"/>
</php>
</phpunit>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

use PhpAmqpLib\Connection\AMQPStreamConnection;
Expand Down
Loading

0 comments on commit 3e1a78e

Please sign in to comment.