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 Jan 3, 2023
1 parent b4052af commit f6821fc
Show file tree
Hide file tree
Showing 11 changed files with 630 additions and 104 deletions.
45 changes: 45 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,51 @@ CHANGELOG
* `addUrl`
* `addBroker`

* Stub Server
* [BC BREAK] Updated `PhpPact\Standalone\StubService\StubServerConfigInterface`, see [pact-stub-server](https://github.com/pact-foundation/pact-stub-server)
* Removed methods:
* `getHost`
* `setHost`
* `isSecure`
* `setSecure`
* `getLog`
* `setLog`
* `getPactLocation`
* `setPactLocation`
* Added methods:
* `getBrokerUrl`
* `setBrokerUrl`
* `getToken`
* `setToken`
* `setDirs`
* `getDirs`
* `setFiles`
* `getFiles`
* `setUrls`
* `getUrls`
* `setUser`
* `getUser`
* `getExtension`
* `setExtension`
* `getLogLevel`
* `setLogLevel`
* `getProviderState`
* `setProviderState`
* `isEmptyProviderState`
* `setEmptyProviderState`
* `getProviderStateHeaderName`
* `setProviderStateHeaderName`
* `isCors`
* `setCors`
* `isCorsReferer`
* `setCorsReferer`
* `isInsecureTls`
* `setInsecureTls`
* `setConsumerNames`
* `getConsumerNames`
* `setProviderNames`
* `getProviderNames`

* PHPUnit
* [BC BREAK] Removed `PhpPact\Consumer\Hook\ContractDownloader`
* Replace broker http client by broker cli in `PhpPact\Consumer\Listener\PactTestListener`
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,11 @@ If you would like to test with fixtures, you can use the `pact-stub-service` lik

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

$config = (new StubServerConfig())
->setPactLocation($pactLocation)
->setHost($host)
->setFiles($pactLocation)
->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",
"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.3.19",
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.2",
"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 @@ -38,19 +38,14 @@ 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' : '');
}

/**
* @return string
*/
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 @@ -17,12 +17,12 @@ class StubServerHttpService implements StubServerHttpServiceInterface
/**
* @var ClientInterface
*/
private $client;
private ClientInterface $client;

/**
* @var StubServerConfigInterface
*/
private $config;
private StubServerConfigInterface $config;

/**
* StubServerHttpService constructor.
Expand Down
73 changes: 64 additions & 9 deletions src/PhpPact/Standalone/StubService/StubServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpPact\Standalone\StubService;

use Amp\Process\ProcessException;
use Exception;
use PhpPact\Standalone\Installer\Model\Scripts;
use PhpPact\Standalone\Runner\ProcessRunner;
Expand All @@ -13,14 +14,14 @@
class StubServer
{
/** @var StubServerConfigInterface */
private $config;
private StubServerConfigInterface $config;

/** @var ProcessRunner */
private $processRunner;
private ProcessRunner $processRunner;

public function __construct(StubServerConfigInterface $config)
{
$this->config = $config;
$this->config = $config;
}

/**
Expand All @@ -32,7 +33,7 @@ public function __construct(StubServerConfigInterface $config)
*
* @return int process ID of the started Stub Server
*/
public function start($wait = 1): int
public function start(int $wait = 1): int
{
$this->processRunner = new ProcessRunner(Scripts::getStubService(), $this->getArguments());

Expand All @@ -45,6 +46,8 @@ public function start($wait = 1): int
/**
* Stop the Stub Server process.
*
* @throws ProcessException
*
* @return bool Was stopping successful?
*/
public function stop(): bool
Expand All @@ -61,12 +64,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 f6821fc

Please sign in to comment.