From 127bae6a748afb7ab0a8cfab2ac344e6ac152b71 Mon Sep 17 00:00:00 2001 From: tienvx Date: Tue, 3 Jan 2023 23:32:48 +0700 Subject: [PATCH] Use Rust FFI: Offload process --- .github/workflows/build.yml | 2 +- composer.json | 6 +- example/tests/Provider/PactVerifyTest.php | 14 +- phpstan.neon | 6 + src/PhpPact/Standalone/Broker/Broker.php | 271 +++++++----------- .../Standalone/Installer/Model/Scripts.php | 4 +- .../Standalone/Runner/ProcessRunner.php | 193 ------------- .../Standalone/StubService/StubServer.php | 30 +- .../PhpPact/Standalone/Broker/BrokerTest.php | 11 +- .../ProviderVerifier/VerifierTest.php | 14 +- .../Standalone/Runner/ProcessRunnerTest.php | 74 ----- tests/PhpPact/Standalone/Runner/verifier.bat | 11 - tests/PhpPact/Standalone/Runner/verifier.sh | 13 - 13 files changed, 143 insertions(+), 506 deletions(-) delete mode 100644 src/PhpPact/Standalone/Runner/ProcessRunner.php delete mode 100644 tests/PhpPact/Standalone/Runner/ProcessRunnerTest.php delete mode 100755 tests/PhpPact/Standalone/Runner/verifier.bat delete mode 100755 tests/PhpPact/Standalone/Runner/verifier.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 427531c89..b75b69f7c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -60,7 +60,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - extensions: openssl, sockets, curl, zip, ffi + extensions: sockets, curl, zip, ffi php-version: ${{ matrix.php }} coverage: none diff --git a/composer.json b/composer.json index 681298092..b1ce60781 100644 --- a/composer.json +++ b/composer.json @@ -19,13 +19,9 @@ ], "require": { "php": "^8.0", - "ext-openssl": "*", "ext-json": "*", "composer/semver": "^1.4.0|^3.2.0", - "amphp/amp": "^2.5.1", - "amphp/byte-stream": "^1.8", - "amphp/log": "^1.1", - "amphp/process": "^1.1.1", + "symfony/process": "^4.4|^5.4|^6.0", "guzzlehttp/guzzle": "^6.5.8|^7.4.5", "phpunit/phpunit": ">=8.5.23 <10", "tienvx/composer-downloads-plugin": "^1.2.0" diff --git a/example/tests/Provider/PactVerifyTest.php b/example/tests/Provider/PactVerifyTest.php index e09a09d78..a16012b9d 100644 --- a/example/tests/Provider/PactVerifyTest.php +++ b/example/tests/Provider/PactVerifyTest.php @@ -6,8 +6,8 @@ use PhpPact\Standalone\ProviderVerifier\Model\Config\ProviderTransport; use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig; use PhpPact\Standalone\ProviderVerifier\Verifier; -use PhpPact\Standalone\Runner\ProcessRunner; use PHPUnit\Framework\TestCase; +use Symfony\Component\Process\Process; /** * This is an example on how you could use the included amphp/process wrapper to start your API to run PACT verification against a Provider. @@ -15,8 +15,8 @@ */ class PactVerifyTest extends TestCase { - /** @var ProcessRunner */ - private ProcessRunner $processRunner; + /** @var Process */ + private Process $process; /** * Run the PHP build-in web server. @@ -25,10 +25,10 @@ protected function setUp(): void { $publicPath = __DIR__ . '/../../src/Provider/public/'; - $this->processRunner = new ProcessRunner('php', ['-S', 'localhost:7202', '-t', $publicPath]); + $this->process = new Process(['php', '-S', '127.0.0.1:7202', '-t', $publicPath, $publicPath . 'proxy.php']); - $this->processRunner->run(); - \usleep(300000); // wait for server to start + $this->process->start(); + $this->process->waitUntil(fn () => is_resource(@fsockopen('127.0.0.1', 7202))); } /** @@ -36,7 +36,7 @@ protected function setUp(): void */ protected function tearDown(): void { - $this->processRunner->stop(); + $this->process->stop(); } /** diff --git a/phpstan.neon b/phpstan.neon index 1d5823a9b..02aff4b23 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,3 +2,9 @@ parameters: level: 7 paths: - src + ignoreErrors: + - + messages: + - '#Method PhpPact\\Standalone\\Broker\\Broker::[a-zA-Z0-9\\_]+\(\) return type has no value type specified in iterable type array\.#' + - '#Method PhpPact\\Standalone\\Broker\\Broker::[a-zA-Z0-9\\_]+\(\) has parameter \$options with no value type specified in iterable type array\.#' + path: src/PhpPact/Standalone/Broker/Broker.php diff --git a/src/PhpPact/Standalone/Broker/Broker.php b/src/PhpPact/Standalone/Broker/Broker.php index f7caa83e1..529bf80da 100644 --- a/src/PhpPact/Standalone/Broker/Broker.php +++ b/src/PhpPact/Standalone/Broker/Broker.php @@ -2,8 +2,9 @@ namespace PhpPact\Standalone\Broker; +use Exception; use PhpPact\Standalone\Installer\Model\Scripts; -use PhpPact\Standalone\Runner\ProcessRunner; +use Symfony\Component\Process\Process; class Broker { @@ -17,25 +18,13 @@ public function __construct(BrokerConfig $config) $this->command = Scripts::getBroker(); } - /** - * @throws \Exception - */ - public function canIDeploy(): mixed + public function canIDeploy(): array { - $runner = new ProcessRunner( - $this->command, - \array_merge( - [ - 'can-i-deploy', - '--pacticipant=\'' . $this->config->getPacticipant().'\'', - '--version=' . $this->config->getVersion() - ], - $this->getArguments() - ) - ); - $runner->runBlocking(); - - return \json_decode($runner->getOutput(), true, 512, JSON_THROW_ON_ERROR); + return $this->runThenDecodeJson([ + 'can-i-deploy', + '--pacticipant', $this->config->getPacticipant(), + '--version', $this->config->getVersion(), + ]); } /** @@ -46,161 +35,93 @@ public function getArguments(): array $parameters = []; if ($this->config->getBrokerUri() !== null) { - $parameters[] = "--broker-base-url={$this->config->getBrokerUri()}"; + $parameters[] = '--broker-base-url'; + $parameters[] = $this->config->getBrokerUri(); } if ($this->config->getBrokerToken() !== null) { - $parameters[] = "--broker-token={$this->config->getBrokerToken()}"; + $parameters[] = '--broker-token'; + $parameters[] = $this->config->getBrokerToken(); } if ($this->config->getBrokerUsername() !== null) { - $parameters[] = "--broker-username={$this->config->getBrokerUsername()}"; + $parameters[] = '--broker-username'; + $parameters[] = $this->config->getBrokerUsername(); } if ($this->config->getBrokerPassword() !== null) { - $parameters[] = "--broker-password={$this->config->getBrokerPassword()}"; + $parameters[] = '--broker-password'; + $parameters[] = $this->config->getBrokerPassword(); } return $parameters; } - /** - * @throws \Exception - */ - public function createOrUpdatePacticipant(): mixed + public function createOrUpdatePacticipant(): array { - $runner = new ProcessRunner( - $this->command, - \array_merge( - [ - 'create-or-update-pacticipant', - '--name=' . $this->config->getName(), - '--repository-url=' . $this->config->getRepositoryUrl(), - ], - $this->getArguments() - ) - ); - $runner->runBlocking(); - - return \json_decode($runner->getOutput(), true, 512, JSON_THROW_ON_ERROR); + return $this->runThenDecodeJson([ + 'create-or-update-pacticipant', + '--name', $this->config->getName(), + '--repository-url', $this->config->getRepositoryUrl(), + ]); } - /** - * @throws \Exception - */ - public function createOrUpdateWebhook(): mixed + public function createOrUpdateWebhook(): array { - $runner = new ProcessRunner( - $this->command, - \array_merge( - [ - 'create-or-update-webhook', - $this->config->getUrl(), - '--request=' . $this->config->getRequest(), - '--header=' . $this->config->getHeader(), - '--data=' . $this->config->getData(), - '--user=' . $this->config->getUser(), - '--consumer=' . $this->config->getConsumer(), - '--provider=' . $this->config->getProvider(), - '--description=' . $this->config->getDescription(), - '--uuid=' . $this->config->getUuid(), - ], - $this->getArguments() - ) - ); - $runner->runBlocking(); - - return \json_decode($runner->getOutput(), true, 512, JSON_THROW_ON_ERROR); + return $this->runThenDecodeJson([ + 'create-or-update-webhook', + $this->config->getUrl(), + '--request', $this->config->getRequest(), + '--header', $this->config->getHeader(), + '--data', $this->config->getData(), + '--user', $this->config->getUser(), + '--consumer', $this->config->getConsumer(), + '--provider', $this->config->getProvider(), + '--description', $this->config->getDescription(), + '--uuid', $this->config->getUuid(), + ]); } - /** - * @throws \Exception - */ - public function createVersionTag(): mixed + public function createVersionTag(): array { - $runner = new ProcessRunner( - $this->command, - \array_merge( - [ - 'create-version-tag', - '--pacticipant=\'' . $this->config->getPacticipant().'\'', - '--version=' . $this->config->getVersion(), - '--tag=' . $this->config->getTag(), - ], - $this->getArguments() - ) - ); - $runner->runBlocking(); - - return \json_decode($runner->getOutput(), true, 512, JSON_THROW_ON_ERROR); + return $this->runThenDecodeJson([ + 'create-version-tag', + '--pacticipant', $this->config->getPacticipant(), + '--version', $this->config->getVersion(), + '--tag', $this->config->getTag(), + ]); } - /** - * @throws \Exception - */ - public function createWebhook(): mixed + public function createWebhook(): array { - $runner = new ProcessRunner( - $this->command, - \array_merge( - [ - 'create-webhook', - $this->config->getUrl(), - '--request=' . $this->config->getRequest(), - '--header=' . $this->config->getHeader(), - '--data=' . $this->config->getData(), - '--user=' . $this->config->getUser(), - '--consumer=' . $this->config->getConsumer(), - '--provider=' . $this->config->getProvider(), - '--description=' . $this->config->getDescription(), - ], - $this->getArguments() - ) - ); - $runner->runBlocking(); - - return \json_decode($runner->getOutput(), true, 512, JSON_THROW_ON_ERROR); + return $this->runThenDecodeJson([ + 'create-webhook', + $this->config->getUrl(), + '--request', $this->config->getRequest(), + '--header', $this->config->getHeader(), + '--data', $this->config->getData(), + '--user', $this->config->getUser(), + '--consumer', $this->config->getConsumer(), + '--provider', $this->config->getProvider(), + '--description', $this->config->getDescription(), + ]); } - /** - * @throws \Exception - */ - public function describeVersion(): mixed + public function describeVersion(): array { - $runner = new ProcessRunner( - $this->command, - \array_merge( - [ - 'describe-version', - '--pacticipant=\'' . $this->config->getPacticipant().'\'', - '--output=json', - ], - $this->getArguments() - ) - ); - $runner->runBlocking(); - - return \json_decode($runner->getOutput(), true, 512, JSON_THROW_ON_ERROR); + return $this->runThenDecodeJson([ + 'describe-version', + '--pacticipant', $this->config->getPacticipant(), + '--output', 'json', + ]); } - /** - * @throws \Exception - */ - public function listLatestPactVersions(): mixed + public function listLatestPactVersions(): array { - $runner = new ProcessRunner( - $this->command, - \array_merge( - [ - 'list-latest-pact-versions', - '--output=json', - ], - $this->getArguments() - ) - ); - $runner->runBlocking(); - - return \json_decode($runner->getOutput(), true, 512, JSON_THROW_ON_ERROR); + return $this->runThenDecodeJson([ + 'list-latest-pact-versions', + '--output', 'json', + ]); } public function publish(): void @@ -208,53 +129,53 @@ public function publish(): void $options = [ 'publish', $this->config->getPactLocations(), - '--consumer-app-version=' . $this->config->getConsumerVersion(), + '--consumer-app-version', $this->config->getConsumerVersion(), ]; if (null !== $this->config->getBranch()) { - $options[] = '--branch=' . $this->config->getBranch(); + $options[] = '--branch'; + $options[] = $this->config->getBranch(); } if (null !== $this->config->getTag()) { - $options[] = '--tag=' . $this->config->getTag(); + $options[] = '--tag'; + $options[] = $this->config->getTag(); } - $runner = new ProcessRunner( - $this->command, - \array_merge( - $options, - $this->getArguments() - ) - ); + $this->run($options); + } + + public function testWebhook(): array + { + return $this->runThenDecodeJson([ + 'test-webhook', + '--uuid', $this->config->getUuid(), + ]); + } - $runner->runBlocking(); + public function generateUuid(): string + { + return \rtrim($this->run(['generate-uuid'])); } - /** - * @throws \Exception - */ - public function testWebhook(): mixed + private function run(array $options): string { - $runner = new ProcessRunner( + $process = new Process([ $this->command, - \array_merge( - [ - 'test-webhook', - '--uuid=' . $this->config->getUuid(), - ], - $this->getArguments() - ) - ); - $runner->runBlocking(); + ...$options, + ...$this->getArguments(), + ]); + $process->run(); + + if (!$process->isSuccessful()) { + throw new Exception("PactPHP Process returned non-zero exit code: {$process->getExitCode()}", $process->getExitCode()); + } - return \json_decode($runner->getOutput(), true, 512, JSON_THROW_ON_ERROR); + return $process->getOutput(); } - public function generateUuid(): string + private function runThenDecodeJson(array $options): array { - $runner = new ProcessRunner($this->command, ['generate-uuid']); - $runner->runBlocking(); - - return \rtrim($runner->getOutput()); + return \json_decode($this->run($options), true, 512, JSON_THROW_ON_ERROR); } } diff --git a/src/PhpPact/Standalone/Installer/Model/Scripts.php b/src/PhpPact/Standalone/Installer/Model/Scripts.php index 67e96568a..3c7747c10 100644 --- a/src/PhpPact/Standalone/Installer/Model/Scripts.php +++ b/src/PhpPact/Standalone/Installer/Model/Scripts.php @@ -28,11 +28,11 @@ public static function getLibrary(): string public static function getStubService(): string { - return self::$destinationDir . '/bin/pact-stub-server/pact-stub-server' . (PHP_OS_FAMILY === 'Windows' ? '.exe' : ''); + return self::$destinationDir . '/bin/pact-stub-server/pact-stub-server'; } public static function getBroker(): string { - return self::$destinationDir . '/bin/pact-ruby-standalone/bin/pact-broker' . (PHP_OS_FAMILY === 'Windows' ? '.bat' : ''); + return self::$destinationDir . '/bin/pact-ruby-standalone/bin/pact-broker'; } } diff --git a/src/PhpPact/Standalone/Runner/ProcessRunner.php b/src/PhpPact/Standalone/Runner/ProcessRunner.php deleted file mode 100644 index 2da9eb156..000000000 --- a/src/PhpPact/Standalone/Runner/ProcessRunner.php +++ /dev/null @@ -1,193 +0,0 @@ - $arguments - */ - public function __construct(string $command, array $arguments) - { - $this->exitCode = -1; - $this->process = new Process($command . ' ' . \implode(' ', $arguments)); - } - - public function setLogger(LoggerInterface $logger): self - { - $this->logger = $logger; - - return $this; - } - - public function getOutput(): string - { - return $this->output; - } - - public function setOutput(string $output): void - { - $this->output = $output; - } - - public function getExitCode(): int - { - return $this->exitCode; - } - - public function setExitCode(int $exitCode): void - { - $this->exitCode = $exitCode; - } - - public function getCommand(): string - { - return $this->process->getCommand(); - } - - public function getStderr(): string - { - return $this->stderr; - } - - public function setStderr(string $stderr): void - { - $this->stderr = $stderr; - } - - /** - * Run a blocking, synchronous process - */ - public function runBlocking(): int - { - $logger = $this->getLogger(); - $pid = null; - $lambdaLoop = function () use ($logger, &$pid) { - $logger->debug("Process command: {$this->process->getCommand()}"); - - $pid = yield $this->process->start(); - - $this->output .= yield ByteStream\buffer($this->process->getStdout()); - $this->stderr .= yield ByteStream\buffer($this->process->getStderr()); - - $exitCode = yield $this->process->join(); - $this->setExitCode($exitCode); - $logger->debug("Exit code: {$this->getExitCode()}"); - - if ($this->getExitCode() !== 0) { - $this->logger->info('out > ' . $this->getOutput()); - $this->logger->error('err > ' . $this->getStderr()); - throw new \Exception("PactPHP Process returned non-zero exit code: {$this->getExitCode()}", $this->getExitCode()); - } - - Loop::stop(); - }; - - Loop::run($lambdaLoop); - - return $pid; - } - - /** - * Run a blocking, synchronous process - */ - public function runNonBlocking(): int - { - $logger = $this->getLogger(); - - $pid = null; - - $lambdaLoop = function () use ($logger, &$pid) { - $logger->debug("start background command: {$this->process->getCommand()}"); - - $pid = yield $this->process->start(); - - $this->process->getStdout()->read()->onResolve(function (\Throwable $reason = null, $value) { - $this->output .= $value; - }); - $this->process->getStderr()->read()->onResolve(function (\Throwable $reason = null, $value) { - $this->output .= $value; - }); - - Loop::stop(); - }; - - Loop::run($lambdaLoop); - - $logger->debug("started process pid=$pid"); - - return $pid; - } - - /** - * Run the process and set output - * - * @return int Process Id - */ - public function run(bool $blocking = false): int - { - return $blocking - ? $this->runBlocking() - : $this->runNonBlocking(); - } - - /** - * Stop the running process - * - * @throws ProcessException - */ - public function stop(): bool - { - $pid = $this->process->getPid(); - - print "\nStopping Process Id: {$pid}\n"; - - if ('\\' === \DIRECTORY_SEPARATOR) { - \exec(\sprintf('taskkill /F /T /PID %d 2>&1', $pid), $output, $exitCode); - } - - $this->process->kill(); - - if ($this->process->isRunning()) { - throw new ProcessException(\sprintf('Error while killing process "%s".', $pid)); - } - - return true; - } - - private function getLogger(): LoggerInterface - { - if (null === $this->logger) { - $logHandler = new StreamHandler(new ResourceOutputStream(\STDOUT)); - $logHandler->setFormatter(new ConsoleFormatter(null, null, true)); - $this->logger = new Logger('server'); - $this->logger->pushHandler($logHandler); - } - - return $this->logger; - } -} diff --git a/src/PhpPact/Standalone/StubService/StubServer.php b/src/PhpPact/Standalone/StubService/StubServer.php index 4805770cc..551e884aa 100644 --- a/src/PhpPact/Standalone/StubService/StubServer.php +++ b/src/PhpPact/Standalone/StubService/StubServer.php @@ -2,10 +2,9 @@ namespace PhpPact\Standalone\StubService; -use Amp\Process\ProcessException; use Exception; use PhpPact\Standalone\Installer\Model\Scripts; -use PhpPact\Standalone\Runner\ProcessRunner; +use Symfony\Component\Process\Process; /** * Ruby Standalone Stub Server Wrapper @@ -14,7 +13,7 @@ class StubServer { private StubServerConfigInterface $config; - private ProcessRunner $processRunner; + private Process $process; public function __construct(StubServerConfigInterface $config) { @@ -24,33 +23,34 @@ public function __construct(StubServerConfigInterface $config) /** * Start the Stub Server. Verify that it is running. * - * @param int $wait seconds to delay for the server to come up - * * @throws Exception * - * @return int process ID of the started Stub Server + * @return int|null process ID of the started Stub Server if running, null otherwise */ - public function start(int $wait = 1): int + public function start(): ?int { - $this->processRunner = new ProcessRunner(Scripts::getStubService(), $this->getArguments()); + $this->process = new Process([Scripts::getStubService(), ...$this->getArguments()]); - $processId = $this->processRunner->run(); - \sleep($wait); // wait for server to start + $this->process->start(function (string $type, string $buffer) { + echo $buffer; + }); + $this->process->waitUntil(function (string $type, string $output) { + return false !== \strpos($output, 'Server started on port'); + }); - return $processId; + return $this->process->getPid(); } /** * Stop the Stub Server process. * - * @throws ProcessException - * * @return bool Was stopping successful? - * @throws ProcessException */ public function stop(): bool { - return $this->processRunner->stop(); + $this->process->stop(); + + return true; } /** diff --git a/tests/PhpPact/Standalone/Broker/BrokerTest.php b/tests/PhpPact/Standalone/Broker/BrokerTest.php index 13b67b385..bdbd79087 100644 --- a/tests/PhpPact/Standalone/Broker/BrokerTest.php +++ b/tests/PhpPact/Standalone/Broker/BrokerTest.php @@ -21,9 +21,14 @@ public function getArguments(): void ->setBrokerPassword('somepassword') ))->getArguments(); - $this->assertContains('--broker-token=someToken', $arguments); - $this->assertContains('--broker-username=someusername', $arguments); - $this->assertContains('--broker-password=somepassword', $arguments); + $this->assertSame([ + '--broker-token', + 'someToken', + '--broker-username', + 'someusername', + '--broker-password', + 'somepassword', + ], $arguments); } /** diff --git a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php index c6cf11f22..ba3fb34c0 100644 --- a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php +++ b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php @@ -4,13 +4,13 @@ use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig; use PhpPact\Standalone\ProviderVerifier\Verifier; -use PhpPact\Standalone\Runner\ProcessRunner; use PHPUnit\Framework\TestCase; +use Symfony\Component\Process\Process; class VerifierTest extends TestCase { - /** @var ProcessRunner */ - private ProcessRunner $processRunner; + /** @var Process */ + private Process $process; /** * Run the PHP build-in web server. @@ -19,10 +19,10 @@ protected function setUp(): void { $publicPath = __DIR__ . '/../../../_public/'; - $this->processRunner = new ProcessRunner('php', ['-S', 'localhost:7202', '-t', $publicPath]); + $this->process = new Process(['php', '-S', '127.0.0.1:7202', '-t', $publicPath]); - $this->processRunner->run(); - \usleep(300000); // wait for server to start + $this->process->start(); + $this->process->waitUntil(fn () => is_resource(@fsockopen('127.0.0.1', 7202))); } /** @@ -30,7 +30,7 @@ protected function setUp(): void */ protected function tearDown(): void { - $this->processRunner->stop(); + $this->process->stop(); } public function testVerify(): void diff --git a/tests/PhpPact/Standalone/Runner/ProcessRunnerTest.php b/tests/PhpPact/Standalone/Runner/ProcessRunnerTest.php deleted file mode 100644 index ff1b45f4a..000000000 --- a/tests/PhpPact/Standalone/Runner/ProcessRunnerTest.php +++ /dev/null @@ -1,74 +0,0 @@ -runBlocking(); - $exitCode = $p->getExitCode(); - - $this->assertEquals($exitCode, 0, 'Expect the exit code to be 0'); - $this->assertStringContainsString($expectedOutput, $p->getOutput(), "Expect '{$expectedOutput}' to be in the output"); - $this->assertEquals(null, $p->getStderr(), 'Expect a null stderr'); - - // try an app that does not exists - if ('\\' !== \DIRECTORY_SEPARATOR) { - $p = new ProcessRunner('failedApp', []); - $expectedErr = 'failedApp'; - } else { - $p = new ProcessRunner('cmd /c echo myError 1>&2 && exit 42', []); - $expectedErr = 'myError'; - } - - try { - $p->runBlocking(); - } catch (\Exception $e) { - $exitCode = $p->getExitCode(); - $this->assertEquals($exitCode, $e->getCode()); - $this->assertStringContainsString("PactPHP Process returned non-zero exit code: $exitCode", $e->getMessage()); - $this->assertNotEquals($exitCode, 0, 'Expect the exit code to be non-zero: ' . $exitCode); - $this->assertStringContainsString($expectedErr, $p->getStderr(), "Expect '{$expectedErr}' to be in the stderr"); - $this->assertEquals(null, $p->getOutput(), 'Expect a null stdout'); - } - } - - /** - * @throws \Exception - */ - public function testProcessRunnerShouldReturnCompleteOutput() - { - if ('\\' !== \DIRECTORY_SEPARATOR) { - $cmd = __DIR__ . \DIRECTORY_SEPARATOR . 'verifier.sh'; - } else { - $cmd = 'cmd /c' . __DIR__ . \DIRECTORY_SEPARATOR . 'verifier.bat'; - } - - $p = new ProcessRunner($cmd, []); - $expectedOutput = 'third line'; - $expectedErr = 'fourth line'; - try { - $p->runBlocking(); - } catch (\Exception $e) { - $this->assertEquals(42, $e->getCode()); - $this->assertStringContainsString("PactPHP Process returned non-zero exit code: 42", $e->getMessage()); - } - $this->assertTrue((\stripos($p->getOutput(), $expectedOutput) !== false), "Expect '{$expectedOutput}' to be in the output:"); - $this->assertTrue((\stripos($p->getStderr(), $expectedErr) !== false), "Expect '{$expectedErr}' to be in the stderr"); - } -} diff --git a/tests/PhpPact/Standalone/Runner/verifier.bat b/tests/PhpPact/Standalone/Runner/verifier.bat deleted file mode 100755 index 3639d40fe..000000000 --- a/tests/PhpPact/Standalone/Runner/verifier.bat +++ /dev/null @@ -1,11 +0,0 @@ -@ECHO OFF - -REM this script simulates a command (like pact-verifier) which prints several lines to stdout and stderr - -ECHO "first line" -ECHO "second line" 1>&2 -ECHO "third line" -ECHO "fourth line" 1>&2 -ECHO "fifth line" - -exit 42 \ No newline at end of file diff --git a/tests/PhpPact/Standalone/Runner/verifier.sh b/tests/PhpPact/Standalone/Runner/verifier.sh deleted file mode 100755 index 8290c3081..000000000 --- a/tests/PhpPact/Standalone/Runner/verifier.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - -# this script simulates a command (like pact-verifier) which prints several lines to stdout and stderr - -echoerr() { echo "$@" 1>&2; } - -echo "first line" -echoerr "second line" -echo "third line" -echoerr "fourth line" -echo "fifth line" - -exit 42 \ No newline at end of file