Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reorganize examples (create separate directory for each example) #331

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php

namespace MessageConsumer;
namespace MessageConsumer\Tests;

use Exception;
use MessageConsumer\ExampleMessageConsumer;
use PhpPact\Consumer\MessageBuilder;
use PhpPact\Config\PactConfigInterface;
use PhpPact\Standalone\PactMessage\PactMessageConfig;
use PHPUnit\Framework\TestCase;
use stdClass;

/**
* Class ExampleMessageConsumerTest
*/
class ExampleMessageConsumerTest extends TestCase
{
private static PactConfigInterface $config;
Expand All @@ -21,9 +19,12 @@ public static function setUpBeforeClass(): void
parent::setUpBeforeClass();

self::$config = (new PactMessageConfig())
->setConsumer('test_consumer')
->setProvider('test_provider')
->setPactDir(__DIR__ . '/../../output/');
->setConsumer('messageConsumer')
->setProvider('messageProvider')
->setPactDir(__DIR__.'/../../pacts');
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
self::$config->setLogLevel($logLevel);
}
}

/**
Expand All @@ -36,7 +37,7 @@ public function testProcessText()
$contents = new stdClass();
$contents->text = 'Hello Mary';

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

$builder
->given('a message', ['foo' => 'bar'])
Expand Down Expand Up @@ -64,7 +65,7 @@ public function testProcessSong()
$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'];
$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')
Expand Down
Loading
Loading