From 936cf8435691dd7399eb6489cb53b352afe68a38 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 27 Sep 2024 13:27:26 +0700 Subject: [PATCH 1/5] refactor: Wrap ffi call > setup pact methods --- helper/FFI/ClientTrait.php | 103 +++++++++++++++++- phpunit.xml | 1 + .../InteractionNotModifiedException.php | 2 +- .../Exception/PactNotModifiedException.php | 7 ++ .../Consumer/Driver/Pact/PactDriver.php | 18 ++- src/PhpPact/FFI/Client.php | 62 +++++++++++ src/PhpPact/FFI/ClientInterface.php | 14 +++ .../Driver/Pact/AbstractPluginPactDriver.php | 8 +- .../Exception/PluginNotLoadedException.php | 17 +++ .../Standalone/ProviderVerifier/Verifier.php | 2 +- .../Consumer/Driver/Pact/PactDriverTest.php | 44 +++----- tests/PhpPact/FFI/ClientTest.php | 40 +++++++ .../Pact/AbstractPluginPactDriverTestCase.php | 41 +++---- .../ProviderVerifier/VerifierTest.php | 2 +- tests/_resources/plugins/repository.index | 23 ++++ .../plugins/repository.index.sha256 | 1 + .../plugins/test-0.0.0/pact-plugin.json | 1 + .../plugins/test-0.0.0/pact-test-plugin | 4 + 18 files changed, 322 insertions(+), 68 deletions(-) create mode 100644 src/PhpPact/Consumer/Driver/Exception/PactNotModifiedException.php create mode 100644 src/PhpPact/Plugin/Exception/PluginNotLoadedException.php create mode 100644 tests/_resources/plugins/repository.index create mode 100644 tests/_resources/plugins/repository.index.sha256 create mode 100644 tests/_resources/plugins/test-0.0.0/pact-plugin.json create mode 100755 tests/_resources/plugins/test-0.0.0/pact-test-plugin diff --git a/helper/FFI/ClientTrait.php b/helper/FFI/ClientTrait.php index ebdf5e8b..61b3fa2a 100644 --- a/helper/FFI/ClientTrait.php +++ b/helper/FFI/ClientTrait.php @@ -6,7 +6,10 @@ use PhpPact\Consumer\Driver\Exception\InteractionKeyNotSetException; use PhpPact\Consumer\Driver\Exception\InteractionNotModifiedException; use PhpPact\Consumer\Driver\Exception\InteractionPendingNotSetException; +use PhpPact\Consumer\Driver\Exception\PactFileNotWrittenException; +use PhpPact\Consumer\Driver\Exception\PactNotModifiedException; use PhpPact\FFI\ClientInterface; +use PhpPact\Plugin\Exception\PluginNotLoadedException; use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\Constraint\IsIdentical; use PHPUnit\Framework\MockObject\MockObject; @@ -152,7 +155,7 @@ protected function expectsGiven(int $interaction, string $name, bool $result): v ->willReturn($result); if (!$result) { $this->expectException(InteractionNotModifiedException::class); - $this->expectExceptionMessage("The interaction or Pact can't be modified (i.e. the mock server for it has already started)"); + $this->expectExceptionMessage("The interaction can't be modified (i.e. the mock server for it has already started)"); } } @@ -180,7 +183,7 @@ protected function expectsGivenWithParam(int $interaction, string $name, array $ }); if (!$result) { $this->expectException(InteractionNotModifiedException::class); - $this->expectExceptionMessage("The interaction or Pact can't be modified (i.e. the mock server for it has already started)"); + $this->expectExceptionMessage("The interaction can't be modified (i.e. the mock server for it has already started)"); } } @@ -193,7 +196,7 @@ protected function expectsUponReceiving(int $interaction, string $description, b ->willReturn($result); if (!$result) { $this->expectException(InteractionNotModifiedException::class); - $this->expectExceptionMessage("The interaction or Pact can't be modified (i.e. the mock server for it has already started)"); + $this->expectExceptionMessage("The interaction can't be modified (i.e. the mock server for it has already started)"); } } @@ -252,4 +255,98 @@ protected function expectsMessageGivenWithParam(int $message, string $name, arra } }); } + + protected function expectsFreePactHandle(int $pact): void + { + $this->client + ->expects($this->once()) + ->method('freePactHandle') + ->with($pact); + } + + protected function expectsNewPact(string $consumer, string $provider, int $pact): void + { + $this->client + ->expects($this->once()) + ->method('newPact') + ->with($consumer, $provider) + ->willReturn($pact); + } + + protected function expectsWithSpecification(int $pact, int $specification, bool $result): void + { + $this->client + ->expects($this->once()) + ->method('withSpecification') + ->with($pact, $specification) + ->willReturn($result); + if (!$result) { + $this->expectException(PactNotModifiedException::class); + $this->expectExceptionMessage("The pact can't be modified (i.e. the mock server for it has already started, or the version is invalid)"); + } + } + + protected function expectsInitWithLogLevel(?string $logLevel): void + { + if ($logLevel) { + $this->client + ->expects($this->once()) + ->method('initWithLogLevel') + ->with($logLevel); + } else { + $this->client + ->expects($this->never()) + ->method('initWithLogLevel'); + } + } + + protected function expectsPactHandleWriteFile(int $pact, string $directory, bool $overwrite, int $result): void + { + $this->client + ->expects($this->once()) + ->method('pactHandleWriteFile') + ->with($pact, $directory, $overwrite) + ->willReturn($result); + if ($result) { + $this->expectException(PactFileNotWrittenException::class); + $this->expectExceptionMessage(match ($result) { + 1 => 'The function panicked.', + 2 => 'The pact file was not able to be written.', + 3 => 'The pact for the given handle was not found.', + default => 'Unknown error', + }); + } + } + + protected function expectsCleanupPlugins(int $pact): void + { + $this->client + ->expects($this->once()) + ->method('cleanupPlugins') + ->with($pact); + } + + protected function expectsUsingPlugin(int $pact, string $name, ?string $version, int $result, bool $supported): void + { + if ($supported) { + $this->client + ->expects($this->once()) + ->method('usingPlugin') + ->with($pact, $name, $version) + ->willReturn($result); + } else { + $this->client + ->expects($this->never()) + ->method('usingPlugin'); + } + if ($supported && $result) { + $this->expectException(PluginNotLoadedException::class); + $this->expectExceptionMessage(match ($result) { + 1 => 'A general panic was caught.', + 2 => 'Failed to load the plugin.', + 3 => 'Pact Handle is not valid.', + default => 'Unknown error', + }); + } + } } diff --git a/phpunit.xml b/phpunit.xml index 40039fd6..4c4a133c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -17,6 +17,7 @@ + diff --git a/src/PhpPact/Consumer/Driver/Exception/InteractionNotModifiedException.php b/src/PhpPact/Consumer/Driver/Exception/InteractionNotModifiedException.php index 8847994c..932914ba 100644 --- a/src/PhpPact/Consumer/Driver/Exception/InteractionNotModifiedException.php +++ b/src/PhpPact/Consumer/Driver/Exception/InteractionNotModifiedException.php @@ -6,6 +6,6 @@ class InteractionNotModifiedException extends DriverException { public function __construct() { - parent::__construct("The interaction or Pact can't be modified (i.e. the mock server for it has already started)"); + parent::__construct("The interaction can't be modified (i.e. the mock server for it has already started)"); } } diff --git a/src/PhpPact/Consumer/Driver/Exception/PactNotModifiedException.php b/src/PhpPact/Consumer/Driver/Exception/PactNotModifiedException.php new file mode 100644 index 00000000..41d0b0a9 --- /dev/null +++ b/src/PhpPact/Consumer/Driver/Exception/PactNotModifiedException.php @@ -0,0 +1,7 @@ +validatePact(); - $this->client->call('pactffi_free_pact_handle', $this->pact->handle); + $success = $this->client->freePactHandle($this->pact->handle) === 0; + if (!$success) { + trigger_error('Can not free pact handle. The handle is not valid or does not refer to a valid Pact. Could be that it was previously deleted.', E_USER_WARNING); + } $this->pact = null; } public function writePact(): void { $this->validatePact(); - $error = $this->client->call( - 'pactffi_pact_handle_write_file', + $error = $this->client->pactHandleWriteFile( $this->pact->handle, $this->config->getPactDir(), $this->config->getPactFileWriteMode() === PactConfigInterface::MODE_OVERWRITE @@ -89,17 +92,20 @@ private function initWithLogLevel(): void { $logLevel = $this->config->getLogLevel(); if ($logLevel) { - $this->client->call('pactffi_init_with_log_level', $logLevel); + $this->client->initWithLogLevel($logLevel); } } private function newPact(): void { - $this->pact = new Pact($this->client->call('pactffi_new_pact', $this->config->getConsumer(), $this->config->getProvider())); + $this->pact = new Pact($this->client->newPact($this->config->getConsumer(), $this->config->getProvider())); } private function withSpecification(): void { - $this->client->call('pactffi_with_specification', $this->pact->handle, $this->getSpecification()); + $success = $this->client->withSpecification($this->pact->handle, $this->getSpecification()); + if (!$success) { + throw new PactNotModifiedException("The pact can't be modified (i.e. the mock server for it has already started, or the version is invalid)"); + } } } diff --git a/src/PhpPact/FFI/Client.php b/src/PhpPact/FFI/Client.php index d8cee134..75bf924d 100644 --- a/src/PhpPact/FFI/Client.php +++ b/src/PhpPact/FFI/Client.php @@ -185,6 +185,68 @@ public function messageGivenWithParam(int $message, string $name, string $key, s $this->call($method, $message, $name, $key, $value); } + public function freePactHandle(int $pact): int + { + $method = 'pactffi_free_pact_handle'; + $result = $this->call($method, $pact); + if (!is_int($result)) { + throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "integer", but got "%s"', $method, get_debug_type($result))); + } + return $result; + } + + public function newPact(string $consumer, string $provider): int + { + $method = 'pactffi_new_pact'; + $result = $this->call($method, $consumer, $provider); + if (!is_int($result)) { + throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "integer", but got "%s"', $method, get_debug_type($result))); + } + return $result; + } + + public function withSpecification(int $pact, int $specification): bool + { + $method = 'pactffi_with_specification'; + $result = $this->call($method, $pact, $specification); + if (!is_bool($result)) { + throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "boolean", but got "%s"', $method, get_debug_type($result))); + } + return $result; + } + + public function initWithLogLevel(string $logLevel): void + { + $method = 'pactffi_init_with_log_level'; + $this->call($method, $logLevel); + } + + public function pactHandleWriteFile(int $pact, string $directory, bool $overwrite): int + { + $method = 'pactffi_pact_handle_write_file'; + $result = $this->call($method, $pact, $directory, $overwrite); + if (!is_int($result)) { + throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "integer", but got "%s"', $method, get_debug_type($result))); + } + return $result; + } + + public function cleanupPlugins(int $pact): void + { + $method = 'pactffi_cleanup_plugins'; + $this->call($method, $pact); + } + + public function usingPlugin(int $pact, string $name, ?string $version): int + { + $method = 'pactffi_using_plugin'; + $result = $this->call($method, $pact, $name, $version); + if (!is_int($result)) { + throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "integer", but got "%s"', $method, get_debug_type($result))); + } + return $result; + } + public function getInteractionPartRequest(): int { return $this->getEnum('InteractionPart_Request'); diff --git a/src/PhpPact/FFI/ClientInterface.php b/src/PhpPact/FFI/ClientInterface.php index f860ffb0..71f78a11 100644 --- a/src/PhpPact/FFI/ClientInterface.php +++ b/src/PhpPact/FFI/ClientInterface.php @@ -41,6 +41,20 @@ public function messageGiven(int $message, string $name): void; public function messageGivenWithParam(int $message, string $name, string $key, string $value): void; + public function freePactHandle(int $pact): int; + + public function newPact(string $consumer, string $provider): int; + + public function withSpecification(int $pact, int $specification): bool; + + public function initWithLogLevel(string $logLevel): void; + + public function pactHandleWriteFile(int $pact, string $directory, bool $overwrite): int; + + public function cleanupPlugins(int $pact): void; + + public function usingPlugin(int $pact, string $name, ?string $version): int; + public function getInteractionPartRequest(): int; public function getInteractionPartResponse(): int; diff --git a/src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php b/src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php index 43e73700..69cc5417 100644 --- a/src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php +++ b/src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php @@ -3,6 +3,7 @@ namespace PhpPact\Plugin\Driver\Pact; use PhpPact\Consumer\Driver\Pact\PactDriver; +use PhpPact\Plugin\Exception\PluginNotLoadedException; use PhpPact\Plugin\Exception\PluginNotSupportedBySpecificationException; abstract class AbstractPluginPactDriver extends PactDriver @@ -10,7 +11,7 @@ abstract class AbstractPluginPactDriver extends PactDriver public function cleanUp(): void { $this->validatePact(); - $this->client->call('pactffi_cleanup_plugins', $this->pact->handle); + $this->client->cleanupPlugins($this->pact->handle); parent::cleanUp(); } @@ -33,7 +34,10 @@ private function usingPlugin(): self throw new PluginNotSupportedBySpecificationException($this->config->getPactSpecificationVersion()); } - $this->client->call('pactffi_using_plugin', $this->pact->handle, $this->getPluginName(), $this->getPluginVersion()); + $error = $this->client->usingPlugin($this->pact->handle, $this->getPluginName(), $this->getPluginVersion()); + if ($error) { + throw new PluginNotLoadedException($error); + } return $this; } diff --git a/src/PhpPact/Plugin/Exception/PluginNotLoadedException.php b/src/PhpPact/Plugin/Exception/PluginNotLoadedException.php new file mode 100644 index 00000000..fe5499dd --- /dev/null +++ b/src/PhpPact/Plugin/Exception/PluginNotLoadedException.php @@ -0,0 +1,17 @@ + 'A general panic was caught.', + 2 => 'Failed to load the plugin.', + 3 => 'Pact Handle is not valid.', + default => 'Unknown error', + }; + parent::__construct($message, $code); + } +} diff --git a/src/PhpPact/Standalone/ProviderVerifier/Verifier.php b/src/PhpPact/Standalone/ProviderVerifier/Verifier.php index d5b132a1..861fce8f 100644 --- a/src/PhpPact/Standalone/ProviderVerifier/Verifier.php +++ b/src/PhpPact/Standalone/ProviderVerifier/Verifier.php @@ -141,7 +141,7 @@ private function setCustomHeaders(VerifierConfigInterface $config): void private function setLogLevel(VerifierConfigInterface $config): void { if ($logLevel = $config->getLogLevel()) { - $this->client->call('pactffi_init_with_log_level', $logLevel); + $this->client->initWithLogLevel($logLevel); } } diff --git a/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php b/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php index 142a3c3b..5bdb4c37 100644 --- a/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php +++ b/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php @@ -51,15 +51,9 @@ public function setUp(): void public function testSetUp(?string $logLevel, string $version, int $specificationHandle): void { $this->assertConfig($logLevel, $version); - $calls = $logLevel ? [ - ['pactffi_init_with_log_level', $logLevel, null], - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, $specificationHandle, null], - ] : [ - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, $specificationHandle, null], - ]; - $this->assertClientCalls($calls); + $this->expectsInitWithLogLevel($logLevel); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, $specificationHandle, true); $this->driver->setUp(); $this->assertSame($this->pactHandle, $this->driver->getPact()->handle); } @@ -67,11 +61,8 @@ public function testSetUp(?string $logLevel, string $version, int $specification public function testSetUpMultipleTimes(): void { $this->assertConfig(null, '1.0.0'); - $calls = [ - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, self::SPEC_V1, null], - ]; - $this->assertClientCalls($calls); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, self::SPEC_V1, true); $this->driver->setUp(); $this->driver->setUp(); $this->driver->setUp(); @@ -80,12 +71,9 @@ public function testSetUpMultipleTimes(): void public function testCleanUp(): void { $this->assertConfig(null, '1.0.0'); - $calls = [ - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, self::SPEC_V1, null], - ['pactffi_free_pact_handle', $this->pactHandle, null], - ]; - $this->assertClientCalls($calls); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, self::SPEC_V1, true); + $this->expectsFreePactHandle($this->pactHandle); $this->driver->setUp(); $this->driver->cleanUp(); } @@ -99,11 +87,8 @@ public function testCleanUpWithoutPact(): void public function testGetPact(): void { $this->assertConfig(null, '1.0.0'); - $calls = [ - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, self::SPEC_V1, null], - ]; - $this->assertClientCalls($calls); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, self::SPEC_V1, true); $this->driver->setUp(); $pact = $this->driver->getPact(); $this->assertSame($this->pactHandle, $pact->handle); @@ -136,12 +121,9 @@ public function testWritePact(int $error, string $writeMode): void ->expects($this->once()) ->method('getPactFileWriteMode') ->willReturn($writeMode); - $calls = [ - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, self::SPEC_V1, null], - ['pactffi_pact_handle_write_file', $this->pactHandle, $this->pactDir, $writeMode === PactConfigInterface::MODE_OVERWRITE, $error], - ]; - $this->assertClientCalls($calls); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, self::SPEC_V1, true); + $this->expectsPactHandleWriteFile($this->pactHandle, $this->pactDir, $writeMode === PactConfigInterface::MODE_OVERWRITE, $error); $this->driver->setUp(); if ($error) { $this->expectException(PactFileNotWrittenException::class); diff --git a/tests/PhpPact/FFI/ClientTest.php b/tests/PhpPact/FFI/ClientTest.php index 966f1033..73c1e2bf 100644 --- a/tests/PhpPact/FFI/ClientTest.php +++ b/tests/PhpPact/FFI/ClientTest.php @@ -120,6 +120,46 @@ public function testMessageGivenWithParam(): void $this->expectNotToPerformAssertions(); } + public function testFreePactHandle(): void + { + $result = $this->client->freePactHandle(1); + $this->assertSame(1, $result); + } + + public function testNewPact(): void + { + $result = $this->client->newPact('consumer', 'provider'); + $this->assertIsInt($result); + $this->client->freePactHandle($result); + } + + public function testInitWithLogLevel(): void + { + $this->client->initWithLogLevel('test'); + $this->expectNotToPerformAssertions(); + } + + public function testPactHandleWriteFile(): void + { + $result = $this->client->pactHandleWriteFile(1, 'test', true); + $this->assertSame(3, $result); + } + + public function testCleanupPlugins(): void + { + $this->client->cleanupPlugins(1); + $this->expectNotToPerformAssertions(); + } + + public function testUsingPlugin(): void + { + putenv('PACT_DO_NOT_TRACK=true'); + // putenv(sprintf('PACT_PLUGIN_DIR=%s', __DIR__ . '/../../_resources/plugins')); + $result = $this->client->usingPlugin(1, 'test', null); + $this->assertSame(2, $result); + // putenv('PACT_PLUGIN_DIR='); + } + public function testGetInteractionPartRequest(): void { $this->assertSame(0, $this->client->getInteractionPartRequest()); diff --git a/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php b/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php index 56537349..912d8f2e 100644 --- a/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php +++ b/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php @@ -10,23 +10,21 @@ abstract class AbstractPluginPactDriverTestCase extends PactDriverTest { - #[TestWith(['1.0.0', self::SPEC_V1, false])] - #[TestWith(['1.1.0', self::SPEC_V1_1, false])] - #[TestWith(['2.0.0', self::SPEC_V2, false])] - #[TestWith(['3.0.0', self::SPEC_V3, false])] - #[TestWith(['4.0.0', self::SPEC_V4, true])] - public function testSetUpUsingPlugin(string $version, int $specificationHandle, bool $supported): void + #[TestWith(['1.0.0', self::SPEC_V1, false, 0])] + #[TestWith(['1.1.0', self::SPEC_V1_1, false, 0])] + #[TestWith(['2.0.0', self::SPEC_V2, false, 0])] + #[TestWith(['3.0.0', self::SPEC_V3, false, 0])] + #[TestWith(['4.0.0', self::SPEC_V4, true, 0])] + #[TestWith(['4.0.0', self::SPEC_V4, true, 1])] + #[TestWith(['4.0.0', self::SPEC_V4, true, 2])] + #[TestWith(['4.0.0', self::SPEC_V4, true, 3])] + #[TestWith(['4.0.0', self::SPEC_V4, true, 4])] + public function testSetUpUsingPlugin(string $version, int $specificationHandle, bool $supported, int $error): void { $this->assertConfig(null, $version); - $calls = $supported ? [ - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, $specificationHandle, null], - ['pactffi_using_plugin', $this->pactHandle, $this->getPluginName(), null, null], - ] : [ - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, $specificationHandle, null], - ]; - $this->assertClientCalls($calls); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, $specificationHandle, true); + $this->expectsUsingPlugin($this->pactHandle, $this->getPluginName(), null, $error, $supported); if (!$supported) { $this->expectException(PluginNotSupportedBySpecificationException::class); $this->expectExceptionMessage(sprintf( @@ -41,14 +39,11 @@ public function testSetUpUsingPlugin(string $version, int $specificationHandle, public function testCleanUpPlugin(): void { $this->assertConfig(null, '4.0.0'); - $calls = [ - ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], - ['pactffi_with_specification', $this->pactHandle, self::SPEC_V4, null], - ['pactffi_using_plugin', $this->pactHandle, $this->getPluginName(), null, null], - ['pactffi_cleanup_plugins', $this->pactHandle, null], - ['pactffi_free_pact_handle', $this->pactHandle, null], - ]; - $this->assertClientCalls($calls); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, self::SPEC_V4, true); + $this->expectsUsingPlugin($this->pactHandle, $this->getPluginName(), null, 0, true); + $this->expectsCleanupPlugins($this->pactHandle); + $this->expectsFreePactHandle($this->pactHandle); $this->driver = $this->createPactDriver(); $this->driver->setUp(); $this->driver->cleanUp(); diff --git a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php index 8584107c..71ba77da 100644 --- a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php +++ b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php @@ -120,8 +120,8 @@ private function setUpCalls(bool $hasProviderTags = true, bool $hasFilterConsume $customHeaders['name-2'], null, ], - ['pactffi_init_with_log_level', strtoupper($logLevel), null], ]; + $this->expectsInitWithLogLevel(strtoupper($logLevel)); } #[TestWith([true, true])] diff --git a/tests/_resources/plugins/repository.index b/tests/_resources/plugins/repository.index new file mode 100644 index 00000000..fc3b92de --- /dev/null +++ b/tests/_resources/plugins/repository.index @@ -0,0 +1,23 @@ +index_version = 1 +format_version = 0 +timestamp = "2023-03-10T05:36:45.725083896Z" + +[entries.test] +name = "test" +latest_version = "0.0.0" + +[[entries.test.versions]] +version = "0.0.0" + +[entries.test.versions.source] + +[entries.test.versions.manifest] +pluginInterfaceVersion = 1 +name = "test" +version = "0.0.0" +executableType = "exec" +entryPoint = "pact-test-plugin" + +[entries.test.versions.manifest.entryPoints] + +[entries.test.versions.manifest.pluginConfig] diff --git a/tests/_resources/plugins/repository.index.sha256 b/tests/_resources/plugins/repository.index.sha256 new file mode 100644 index 00000000..92ed6111 --- /dev/null +++ b/tests/_resources/plugins/repository.index.sha256 @@ -0,0 +1 @@ +e55274d81e67213135251e2885b5cd7e8d3c54502b8ce61dcd9dbf568de4519f \ No newline at end of file diff --git a/tests/_resources/plugins/test-0.0.0/pact-plugin.json b/tests/_resources/plugins/test-0.0.0/pact-plugin.json new file mode 100644 index 00000000..1f522daa --- /dev/null +++ b/tests/_resources/plugins/test-0.0.0/pact-plugin.json @@ -0,0 +1 @@ +{"pluginInterfaceVersion":1,"name":"test","version":"0.0.0","executableType":"exec","minimumRequiredVersion":null,"entryPoint":"pact-test-plugin","entryPoints":{},"args":null,"dependencies":null,"pluginConfig":{}} diff --git a/tests/_resources/plugins/test-0.0.0/pact-test-plugin b/tests/_resources/plugins/test-0.0.0/pact-test-plugin new file mode 100755 index 00000000..2ab3b245 --- /dev/null +++ b/tests/_resources/plugins/test-0.0.0/pact-test-plugin @@ -0,0 +1,4 @@ +#!/usr/bin/env php + Date: Fri, 27 Sep 2024 13:52:36 +0700 Subject: [PATCH 2/5] test: No need to add plugin binary --- phpunit.xml | 1 - tests/PhpPact/FFI/ClientTest.php | 3 +-- tests/_resources/plugins/test-0.0.0/pact-test-plugin | 4 ---- 3 files changed, 1 insertion(+), 7 deletions(-) delete mode 100755 tests/_resources/plugins/test-0.0.0/pact-test-plugin diff --git a/phpunit.xml b/phpunit.xml index 4c4a133c..40039fd6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -17,7 +17,6 @@ - diff --git a/tests/PhpPact/FFI/ClientTest.php b/tests/PhpPact/FFI/ClientTest.php index 73c1e2bf..2eeb73d4 100644 --- a/tests/PhpPact/FFI/ClientTest.php +++ b/tests/PhpPact/FFI/ClientTest.php @@ -154,10 +154,9 @@ public function testCleanupPlugins(): void public function testUsingPlugin(): void { putenv('PACT_DO_NOT_TRACK=true'); - // putenv(sprintf('PACT_PLUGIN_DIR=%s', __DIR__ . '/../../_resources/plugins')); + putenv(sprintf('PACT_PLUGIN_DIR=%s', __DIR__ . '/../../_resources/plugins')); $result = $this->client->usingPlugin(1, 'test', null); $this->assertSame(2, $result); - // putenv('PACT_PLUGIN_DIR='); } public function testGetInteractionPartRequest(): void diff --git a/tests/_resources/plugins/test-0.0.0/pact-test-plugin b/tests/_resources/plugins/test-0.0.0/pact-test-plugin deleted file mode 100755 index 2ab3b245..00000000 --- a/tests/_resources/plugins/test-0.0.0/pact-test-plugin +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env php - Date: Fri, 27 Sep 2024 14:16:59 +0700 Subject: [PATCH 3/5] test: Test can not modify pact --- tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php b/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php index 5bdb4c37..62e93ab0 100644 --- a/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php +++ b/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php @@ -143,6 +143,14 @@ public function testWritePactWithoutPact(): void $this->driver->writePact(); } + public function testWithSpecificationCanNotModifyPact(): void + { + $this->assertConfig(null, 'x.y.z'); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, self::SPEC_UNKNOWN, false); + $this->driver->setUp(); + } + protected function assertConfig(?string $logLevel, string $version): void { $this->config From 698202f44317d9a9acb24649fcad92a66421dad5 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 27 Sep 2024 14:20:08 +0700 Subject: [PATCH 4/5] test: Test can not clean up pact --- helper/FFI/ClientTrait.php | 5 +++-- .../PhpPact/Consumer/Driver/Pact/PactDriverTest.php | 12 +++++++++++- .../Driver/Pact/AbstractPluginPactDriverTestCase.php | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/helper/FFI/ClientTrait.php b/helper/FFI/ClientTrait.php index 61b3fa2a..ebfc7342 100644 --- a/helper/FFI/ClientTrait.php +++ b/helper/FFI/ClientTrait.php @@ -256,12 +256,13 @@ protected function expectsMessageGivenWithParam(int $message, string $name, arra }); } - protected function expectsFreePactHandle(int $pact): void + protected function expectsFreePactHandle(int $pact, int $result): void { $this->client ->expects($this->once()) ->method('freePactHandle') - ->with($pact); + ->with($pact) + ->willReturn($result); } protected function expectsNewPact(string $consumer, string $provider, int $pact): void diff --git a/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php b/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php index 62e93ab0..f11df3a4 100644 --- a/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php +++ b/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php @@ -73,7 +73,7 @@ public function testCleanUp(): void $this->assertConfig(null, '1.0.0'); $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); $this->expectsWithSpecification($this->pactHandle, self::SPEC_V1, true); - $this->expectsFreePactHandle($this->pactHandle); + $this->expectsFreePactHandle($this->pactHandle, 0); $this->driver->setUp(); $this->driver->cleanUp(); } @@ -84,6 +84,16 @@ public function testCleanUpWithoutPact(): void $this->driver->cleanUp(); } + public function testCanNotCleanUpPact(): void + { + $this->assertConfig(null, '1.0.0'); + $this->expectsNewPact($this->consumer, $this->provider, $this->pactHandle); + $this->expectsWithSpecification($this->pactHandle, self::SPEC_V1, true); + $this->expectsFreePactHandle($this->pactHandle, 1); + $this->driver->setUp(); + $this->driver->cleanUp(); + } + public function testGetPact(): void { $this->assertConfig(null, '1.0.0'); diff --git a/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php b/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php index 912d8f2e..3d0b7d78 100644 --- a/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php +++ b/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php @@ -43,7 +43,7 @@ public function testCleanUpPlugin(): void $this->expectsWithSpecification($this->pactHandle, self::SPEC_V4, true); $this->expectsUsingPlugin($this->pactHandle, $this->getPluginName(), null, 0, true); $this->expectsCleanupPlugins($this->pactHandle); - $this->expectsFreePactHandle($this->pactHandle); + $this->expectsFreePactHandle($this->pactHandle, 0); $this->driver = $this->createPactDriver(); $this->driver->setUp(); $this->driver->cleanUp(); From d5d1465199ad852318cbbb06dab2475a90acb693 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 27 Sep 2024 14:23:21 +0700 Subject: [PATCH 5/5] test: Test FFI\Client::withSpecification() --- tests/PhpPact/FFI/ClientTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/PhpPact/FFI/ClientTest.php b/tests/PhpPact/FFI/ClientTest.php index 2eeb73d4..5ece2e18 100644 --- a/tests/PhpPact/FFI/ClientTest.php +++ b/tests/PhpPact/FFI/ClientTest.php @@ -133,6 +133,12 @@ public function testNewPact(): void $this->client->freePactHandle($result); } + public function testWithSpecification(): void + { + $result = $this->client->withSpecification(1, 123); + $this->assertFalse($result); + } + public function testInitWithLogLevel(): void { $this->client->initWithLogLevel('test');