Skip to content

Commit

Permalink
Use Rust FFI: Stub Server
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed May 8, 2023
1 parent d73729a commit 06080e6
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 174 deletions.
14 changes: 5 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
schedule:
- cron: 0 6 1 * *

concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

jobs:
php-cs:
runs-on: ubuntu-latest
Expand All @@ -16,11 +20,6 @@ jobs:
php: [ '8.2' ]

steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.11.0
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v3
name: Checkout repository

Expand Down Expand Up @@ -53,10 +52,6 @@ jobs:
name: PHP ${{ matrix.php }} on ${{ matrix.operating-system }} with ${{ matrix.dependencies }} dependencies

steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.11.0
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v3
name: Checkout repository

Expand All @@ -65,6 +60,7 @@ jobs:
with:
extensions: openssl, sockets, curl, zip, ffi
php-version: ${{ matrix.php }}
coverage: none

- name: Composer install
uses: ramsey/composer-install@v2
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,12 @@ Handle these requests on provider's proxy:
If you would like to test with fixtures, you can use the `pact-stub-service` like this:

```php
$pactLocation = __DIR__ . '/someconsumer-someprovider.json';
$host = 'localhost';
$port = 7201;
$endpoint = 'test';
$files = [__DIR__ . '/someconsumer-someprovider.json'];
$port = 7201;
$endpoint = 'test';

$config = (new StubServerConfig())
->setPactLocation($pactLocation)
->setHost($host)
->setFiles($files)
->setPort($port)
->setEndpoint($endpoint);

Expand Down
21 changes: 18 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"amphp/process": "^1.1.1",
"guzzlehttp/guzzle": "^6.5.8|^7.4.5",
"phpunit/phpunit": ">=8.5.23 <10",
"tienvx/composer-downloads-plugin": "^1.1.0"
"tienvx/composer-downloads-plugin": "^1.2.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
Expand Down Expand Up @@ -77,10 +77,15 @@
"variables": {
"{$os}": "PHP_OS_FAMILY === 'Windows' ? 'win32' : (PHP_OS === 'Darwin' ? 'osx' : 'linux')",
"{$architecture}": "PHP_OS === 'Linux' ? '-x86_64' : ''",
"{$extension}": "PHP_OS_FAMILY === 'Windows' ? 'zip' : 'tar.gz'"
"{$extension}": "PHP_OS_FAMILY === 'Windows' ? 'zip' : 'tar.gz'",
"{$keep}": "PHP_OS_FAMILY === 'Windows' ? 'pact-broker.bat' : 'pact-broker'"
},
"url": "https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v{$version}/pact-{$version}-{$os}{$architecture}.{$extension}",
"path": "bin/pact-ruby-standalone"
"path": "bin/pact-ruby-standalone",
"ignore": [
"bin/*",
"!bin/{$keep}"
]
},
"pact-ffi-headers": {
"version": "0.4.4",
Expand All @@ -97,6 +102,16 @@
},
"url": "https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v{$version}/{$prefix}-{$os}-{$architecture}.{$extension}.gz",
"path": "bin/pact-ffi-lib/pact.{$extension}"
},
"pact-stub-server": {
"version": "0.5.3",
"variables": {
"{$os}": "PHP_OS === 'Darwin' ? 'osx' : strtolower(PHP_OS_FAMILY)",
"{$extension}": "PHP_OS_FAMILY === 'Windows' ? '.exe' : ''"
},
"url": "https://github.com/pact-foundation/pact-stub-server/releases/download/v{$version}/pact-stub-server-{$os}-x86_64{$extension}.gz",
"path": "bin/pact-stub-server/pact-stub-server{$extension}",
"executable": true
}
}
},
Expand Down
9 changes: 2 additions & 7 deletions src/PhpPact/Standalone/Installer/Model/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@ public static function getLibrary(): string

public static function getStubService(): string
{
return self::$destinationDir . '/bin/pact-ruby-standalone/bin/pact-stub-service' . self::getSuffix();
return self::$destinationDir . '/bin/pact-stub-server/pact-stub-server' . (PHP_OS_FAMILY === 'Windows' ? '.exe' : '');
}

public static function getBroker(): string
{
return self::$destinationDir . '/bin/pact-ruby-standalone/bin/pact-broker' . self::getSuffix();
}

private static function getSuffix(): string
{
return (PHP_OS_FAMILY === 'Windows' ? '.bat' : '');
return self::$destinationDir . '/bin/pact-ruby-standalone/bin/pact-broker' . (PHP_OS_FAMILY === 'Windows' ? '.bat' : '');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PhpPact\Standalone\StubService\Service;

use PhpPact\Exception\ConnectionException;
use PhpPact\Http\ClientInterface;
use PhpPact\Standalone\StubService\StubServerConfigInterface;

Expand All @@ -23,30 +22,6 @@ public function __construct(ClientInterface $client, StubServerConfigInterface $
$this->config = $config;
}

/**
* {@inheritdoc}
*/
public function healthCheck(): bool
{
$uri = $this->config->getBaseUri()->withPath('/');

$response = $this->client->get($uri, [
'headers' => [
'Content-Type' => 'application/json',
'X-Pact-Mock-Service' => true,
],
]);

$body = $response->getBody()->getContents();

if ($response->getStatusCode() !== 200
|| $body !== "Mock service running\n") {
throw new ConnectionException('Failed to receive a successful response from the Stub Server.');
}

return true;
}

/**
* {@inheritdoc}
* @throws \JsonException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@

namespace PhpPact\Standalone\StubService\Service;

use PhpPact\Exception\ConnectionException;

interface StubServerHttpServiceInterface
{
/**
* Verify that the Ruby PhpPact Stub Server is running.
*
* @throws ConnectionException
*/
public function healthCheck(): bool;

/**
* Get the current state of the PACT JSON file and write it to disk.
*/
Expand Down
64 changes: 59 additions & 5 deletions src/PhpPact/Standalone/StubService/StubServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function start(int $wait = 1): int
/**
* Stop the Stub Server process.
*
* @throws ProcessException
*
* @return bool Was stopping successful?
* @throws ProcessException
*/
Expand All @@ -60,12 +62,64 @@ private function getArguments(): array
{
$results = [];

$results[] = $this->config->getPactLocation();
$results[] = "--host={$this->config->getHost()}";
$results[] = "--port={$this->config->getPort()}";
if ($this->config->getBrokerUrl() !== null) {
$results[] = "--broker-url={$this->config->getBrokerUrl()}";
}

foreach ($this->config->getDirs() as $dir) {
$results[] = "--dir={$dir}";
}

if ($this->config->getExtension() !== null) {
$results[] = "--extension={$this->config->getExtension()}";
}

foreach ($this->config->getFiles() as $file) {
$results[] = "--file={$file}";
}

if ($this->config->getLogLevel() !== null) {
$results[] = "--loglevel={$this->config->getLogLevel()}";
}

if ($this->config->getPort() !== null) {
$results[] = "--port={$this->config->getPort()}";
}

if ($this->config->getProviderState() !== null) {
$results[] = "--provider-state={$this->config->getProviderState()}";
}

if ($this->config->getProviderStateHeaderName() !== null) {
$results[] = "--provider-state-header-name={$this->config->getProviderStateHeaderName()}";
}

if ($this->config->getToken() !== null) {
$results[] = "--token={$this->config->getToken()}";
}

foreach ($this->config->getUrls() as $url) {
$results[] = "--url={$url}";
}

if ($this->config->getUser() !== null) {
$results[] = "--user={$this->config->getUser()}";
}

if ($this->config->isCors()) {
$results[] = '--cors';
}

if ($this->config->isCorsReferer()) {
$results[] = '--cors-referer';
}

if ($this->config->isEmptyProviderState()) {
$results[] = '--empty-provider-state';
}

if ($this->config->getLog() !== null) {
$results[] = "--log={$this->config->getLog()}";
if ($this->config->isInsecureTls()) {
$results[] = '--insecure-tls';
}

return $results;
Expand Down
Loading

0 comments on commit 06080e6

Please sign in to comment.