From 6371c1de84ee35a17e8cc2d409b07f74bc2c3e5b Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Sat, 28 Sep 2024 11:27:38 +0700 Subject: [PATCH] refactor: Wrap ffi call > setup verifier methods --- helper/FFI/ClientTrait.php | 196 ++++++++++++++++++ src/PhpPact/FFI/Client.php | 150 ++++++++++++++ src/PhpPact/FFI/ClientInterface.php | 46 ++++ .../InvalidVerifierHandleException.php | 7 + .../Exception/VerifierNotCreatedException.php | 7 + .../Standalone/ProviderVerifier/Verifier.php | 69 +++--- tests/PhpPact/FFI/ClientTest.php | 124 +++++++++++ .../ProviderVerifier/VerifierTest.php | 109 ++++------ 8 files changed, 597 insertions(+), 111 deletions(-) create mode 100644 src/PhpPact/Standalone/ProviderVerifier/Exception/InvalidVerifierHandleException.php create mode 100644 src/PhpPact/Standalone/ProviderVerifier/Exception/VerifierNotCreatedException.php diff --git a/helper/FFI/ClientTrait.php b/helper/FFI/ClientTrait.php index 9322728a..4aa4af10 100644 --- a/helper/FFI/ClientTrait.php +++ b/helper/FFI/ClientTrait.php @@ -2,6 +2,7 @@ namespace PhpPactTest\Helper\FFI; +use FFI\CData; use PhpPact\Consumer\Driver\Exception\InteractionCommentNotSetException; use PhpPact\Consumer\Driver\Exception\InteractionKeyNotSetException; use PhpPact\Consumer\Driver\Exception\InteractionNotModifiedException; @@ -11,7 +12,10 @@ use PhpPact\Consumer\Exception\MockServerNotStartedException; use PhpPact\Consumer\Exception\MockServerPactFileNotWrittenException; use PhpPact\FFI\ClientInterface; +use PhpPact\FFI\Model\ArrayData; use PhpPact\Plugin\Exception\PluginNotLoadedException; +use PhpPact\Standalone\ProviderVerifier\Exception\VerifierNotCreatedException; +use PhpPact\Standalone\ProviderVerifier\Model\ConsumerVersionSelectors; use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\Constraint\IsIdentical; use PHPUnit\Framework\MockObject\MockObject; @@ -429,4 +433,196 @@ protected function expectsCreateMockServerForTransport(int $pact, string $host, }); } } + + protected function expectsVerifierNewForApplication(string $name, string $version, ?CData $result): void + { + $this->client + ->expects($this->once()) + ->method('verifierNewForApplication') + ->with($name, $version) + ->willReturn($result); + if (!$result) { + $this->expectException(VerifierNotCreatedException::class); + } + } + + protected function expectsVerifierSetProviderInfo(CData $handle, ?string $name, ?string $scheme, ?string $host, ?int $port, ?string $path): void + { + $this->client + ->expects($this->once()) + ->method('verifierSetProviderInfo') + ->with($handle, $name, $scheme, $host, $port, $path); + } + + protected function expectsVerifierAddProviderTransport(CData $handle, ?string $protocol, ?int $port, ?string $path, ?string $scheme): void + { + $this->client + ->expects($this->once()) + ->method('verifierAddProviderTransport') + ->with($handle, $protocol, $port, $path, $scheme); + } + + protected function expectsVerifierSetFilterInfo(CData $handle, ?string $filterDescription, ?string $filterState, bool $filterNoState): void + { + $this->client + ->expects($this->once()) + ->method('verifierSetFilterInfo') + ->with($handle, $filterDescription, $filterState, $filterNoState); + } + + protected function expectsVerifierSetProviderState(CData $handle, ?string $url, bool $teardown, bool $body): void + { + $this->client + ->expects($this->once()) + ->method('verifierSetProviderState') + ->with($handle, $url, $teardown, $body); + } + + protected function expectsVerifierSetVerificationOptions(CData $handle, bool $disableSslVerification, int $requestTimeout, int $result): void + { + $this->client + ->expects($this->once()) + ->method('verifierSetVerificationOptions') + ->with($handle, $disableSslVerification, $requestTimeout) + ->willReturn($result); + } + + /** + * @param string[] $providerTags + */ + protected function expectsVerifierSetPublishOptions(CData $handle, string $providerVersion, ?string $buildUrl, array $providerTags, ?string $providerBranch, int $result): void + { + $this->client + ->expects($this->once()) + ->method('verifierSetPublishOptions') + ->with( + $handle, + $providerVersion, + $buildUrl, + count($providerTags) > 0 ? $this->isInstanceOf(ArrayData::class) : null, + $providerBranch + ) + ->willReturn($result); + } + + /** + * @param string[] $consumerFilters + */ + protected function expectsVerifierSetConsumerFilters(CData $handle, array $consumerFilters): void + { + $this->client + ->expects($this->once()) + ->method('verifierSetConsumerFilters') + ->with( + $handle, + count($consumerFilters) > 0 ? $this->isInstanceOf(ArrayData::class) : null + ); + } + + /** + * @param array $customHeaders + */ + protected function expectsVerifierAddCustomHeader(CData $handle, array $customHeaders): void + { + $calls = []; + foreach ($customHeaders as $name => $value) { + $calls[] = [$handle, $name, $value]; + } + $this->client + ->expects($this->exactly(count($calls))) + ->method('verifierAddCustomHeader') + ->willReturnCallback(function (...$args) use (&$calls) { + $call = array_shift($calls); + foreach ($args as $key => $arg) { + $this->assertThat($arg, $call[$key] instanceof Constraint ? $call[$key] : new IsIdentical($call[$key])); + } + }); + } + + protected function expectsVerifierAddFileSource(CData $handle, string $file): void + { + $this->client + ->expects($this->once()) + ->method('verifierAddFileSource') + ->with($handle, $file); + } + + protected function expectsVerifierAddDirectorySource(CData $handle, string $directory): void + { + $this->client + ->expects($this->once()) + ->method('verifierAddDirectorySource') + ->with($handle, $directory); + } + + protected function expectsVerifierAddUrlSource(CData $handle, string $url, ?string $username, ?string $password, ?string $token): void + { + $this->client + ->expects($this->once()) + ->method('verifierAddUrlSource') + ->with($handle, $url, $username, $password, $token); + } + + protected function expectsVerifierBrokerSourceWithSelectors( + CData $handle, + string $url, + ?string $username, + ?string $password, + ?string $token, + bool $enablePending, + ?string $includeWipPactsSince, + array $providerTags, + ?string $providerBranch, + ConsumerVersionSelectors $consumerVersionSelectors, + array $consumerVersionTags + ): void { + $this->client + ->expects($this->once()) + ->method('verifierBrokerSourceWithSelectors') + ->with( + $handle, + $url, + $username, + $password, + $token, + $enablePending, + $includeWipPactsSince, + count($providerTags) > 0 ? $this->isInstanceOf(ArrayData::class) : null, + $providerBranch, + count($consumerVersionSelectors) > 0 ? $this->isInstanceOf(ArrayData::class) : null, + count($consumerVersionTags) > 0 ? $this->isInstanceOf(ArrayData::class) : null + ); + } + + protected function expectsVerifierExecute(CData $handle, int $result): void + { + $this->client + ->expects($this->once()) + ->method('verifierExecute') + ->with($handle) + ->willReturn($result); + } + + protected function expectsVerifierJson(CData $handle, bool $hasLogger, ?string $result): void + { + if ($hasLogger) { + $this->client + ->expects($this->once()) + ->method('verifierJson') + ->with($handle) + ->willReturn($result); + } else { + $this->client + ->expects($this->never()) + ->method('verifierJson'); + } + } + + protected function expectsVerifierShutdown(CData $handle): void + { + $this->client + ->expects($this->once()) + ->method('verifierShutdown') + ->with($handle); + } } diff --git a/src/PhpPact/FFI/Client.php b/src/PhpPact/FFI/Client.php index 6006bb2f..74ed0824 100644 --- a/src/PhpPact/FFI/Client.php +++ b/src/PhpPact/FFI/Client.php @@ -7,6 +7,7 @@ use PhpPact\FFI\Exception\HeaderNotReadException; use PhpPact\FFI\Exception\InvalidEnumException; use PhpPact\FFI\Exception\InvalidResultException; +use PhpPact\FFI\Model\ArrayData; use PhpPact\FFI\Model\BinaryData; use PhpPact\FFI\Model\Result; use PhpPact\Standalone\Installer\Model\Scripts; @@ -300,6 +301,155 @@ public function createMockServerForTransport(int $pact, string $host, int $port, return $result; } + public function verifierNewForApplication(?string $name, ?string $version): ?CData + { + $method = 'pactffi_verifier_new_for_application'; + $result = $this->call($method, $name, $version); + if ($result === null) { + return $result; + } + if (!$result instanceof CData) { + throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "%s", but got "%s"', $method, CData::class, get_debug_type($result))); + } + return $result; + } + + public function verifierSetProviderInfo(CData $handle, ?string $name, ?string $scheme, ?string $host, ?int $port, ?string $path): void + { + $method = 'pactffi_verifier_set_provider_info'; + $this->call($method, $handle, $name, $scheme, $host, $port, $path); + } + + public function verifierAddProviderTransport(CData $handle, ?string $protocol, ?int $port, ?string $path, ?string $scheme): void + { + $method = 'pactffi_verifier_add_provider_transport'; + $this->call($method, $handle, $protocol, $port, $path, $scheme); + } + + public function verifierSetFilterInfo(CData $handle, ?string $filterDescription, ?string $filterState, bool $filterNoState): void + { + $method = 'pactffi_verifier_set_filter_info'; + $this->call($method, $handle, $filterDescription, $filterState, $filterNoState); + } + + public function verifierSetProviderState(CData $handle, ?string $url, bool $teardown, bool $body): void + { + $method = 'pactffi_verifier_set_provider_state'; + $this->call($method, $handle, $url, $teardown, $body); + } + + public function verifierSetVerificationOptions(CData $handle, bool $disableSslVerification, int $requestTimeout): int + { + $method = 'pactffi_verifier_set_verification_options'; + $result = $this->call($method, $handle, $disableSslVerification, $requestTimeout); + 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 verifierSetPublishOptions(CData $handle, string $providerVersion, ?string $buildUrl, ?ArrayData $providerTags, ?string $providerBranch): int + { + $method = 'pactffi_verifier_set_publish_options'; + $result = $this->call($method, $handle, $providerVersion, $buildUrl, $providerTags?->getItems(), $providerTags?->getSize(), $providerBranch); + 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 verifierSetConsumerFilters(CData $handle, ?ArrayData $consumerFilters): void + { + $method = 'pactffi_verifier_set_consumer_filters'; + $this->call($method, $handle, $consumerFilters?->getItems(), $consumerFilters?->getSize()); + } + + public function verifierAddCustomHeader(CData $handle, string $name, string $value): void + { + $method = 'pactffi_verifier_add_custom_header'; + $this->call($method, $handle, $name, $value); + } + + public function verifierAddFileSource(CData $handle, string $file): void + { + $method = 'pactffi_verifier_add_file_source'; + $this->call($method, $handle, $file); + } + + public function verifierAddDirectorySource(CData $handle, string $directory): void + { + $method = 'pactffi_verifier_add_directory_source'; + $this->call($method, $handle, $directory); + } + + public function verifierAddUrlSource(CData $handle, string $url, ?string $username, ?string $password, ?string $token): void + { + $method = 'pactffi_verifier_url_source'; + $this->call($method, $handle, $url, $username, $password, $token); + } + + public function verifierBrokerSourceWithSelectors( + CData $handle, + string $url, + ?string $username, + ?string $password, + ?string $token, + bool $enablePending, + ?string $includeWipPactsSince, + ?ArrayData $providerTags, + ?string $providerBranch, + ?ArrayData $consumerVersionSelectors, + ?ArrayData $consumerVersionTags + ): void { + $method = 'pactffi_verifier_broker_source_with_selectors'; + $this->call( + $method, + $handle, + $url, + $username, + $password, + $token, + $enablePending, + $includeWipPactsSince, + $providerTags?->getItems(), + $providerTags?->getSize(), + $providerBranch, + $consumerVersionSelectors?->getItems(), + $consumerVersionSelectors?->getSize(), + $consumerVersionTags?->getItems(), + $consumerVersionTags?->getSize() + ); + } + + public function verifierExecute(CData $handle): int + { + $method = 'pactffi_verifier_execute'; + $result = $this->call($method, $handle); + 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 verifierJson(CData $handle): ?string + { + $method = 'pactffi_verifier_json'; + $result = $this->call($method, $handle); + if ($result === null) { + return null; + } + if (!is_string($result)) { + throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "string", but got "%s"', $method, get_debug_type($result))); + } + return $result; + } + + public function verifierShutdown(CData $handle): void + { + $method = 'pactffi_verifier_shutdown'; + $this->call($method, $handle); + } + public function getInteractionPartRequest(): int { return $this->getEnum('InteractionPart_Request'); diff --git a/src/PhpPact/FFI/ClientInterface.php b/src/PhpPact/FFI/ClientInterface.php index 544fca5b..41b6483b 100644 --- a/src/PhpPact/FFI/ClientInterface.php +++ b/src/PhpPact/FFI/ClientInterface.php @@ -2,6 +2,8 @@ namespace PhpPact\FFI; +use FFI\CData; +use PhpPact\FFI\Model\ArrayData; use PhpPact\FFI\Model\BinaryData; use PhpPact\FFI\Model\Result; @@ -65,6 +67,50 @@ public function writePactFile(int $port, string $directory, bool $overwrite): in public function createMockServerForTransport(int $pact, string $host, int $port, string $transport, ?string $transportConfig): int; + public function verifierNewForApplication(?string $name, ?string $version): ?CData; + + public function verifierSetProviderInfo(CData $handle, ?string $name, ?string $scheme, ?string $host, ?int $port, ?string $path): void; + + public function verifierAddProviderTransport(CData $handle, ?string $protocol, ?int $port, ?string $path, ?string $scheme): void; + + public function verifierSetFilterInfo(CData $handle, ?string $filterDescription, ?string $filterState, bool $filterNoState): void; + + public function verifierSetProviderState(CData $handle, ?string $url, bool $teardown, bool $body): void; + + public function verifierSetVerificationOptions(CData $handle, bool $disableSslVerification, int $requestTimeout): int; + + public function verifierSetPublishOptions(CData $handle, string $providerVersion, ?string $buildUrl, ?ArrayData $providerTags, ?string $providerBranch): int; + + public function verifierSetConsumerFilters(CData $handle, ?ArrayData $consumerFilters): void; + + public function verifierAddCustomHeader(CData $handle, string $name, string $value): void; + + public function verifierAddFileSource(CData $handle, string $file): void; + + public function verifierAddDirectorySource(CData $handle, string $directory): void; + + public function verifierAddUrlSource(CData $handle, string $url, ?string $username, ?string $password, ?string $token): void; + + public function verifierBrokerSourceWithSelectors( + CData $handle, + string $url, + ?string $username, + ?string $password, + ?string $token, + bool $enablePending, + ?string $includeWipPactsSince, + ?ArrayData $providerTags, + ?string $providerBranch, + ?ArrayData $consumerVersionSelectors, + ?ArrayData $consumerVersionTags + ): void; + + public function verifierExecute(CData $handle): int; + + public function verifierJson(CData $handle): ?string; + + public function verifierShutdown(CData $handle): void; + public function getInteractionPartRequest(): int; public function getInteractionPartResponse(): int; diff --git a/src/PhpPact/Standalone/ProviderVerifier/Exception/InvalidVerifierHandleException.php b/src/PhpPact/Standalone/ProviderVerifier/Exception/InvalidVerifierHandleException.php new file mode 100644 index 00000000..1b626f60 --- /dev/null +++ b/src/PhpPact/Standalone/ProviderVerifier/Exception/InvalidVerifierHandleException.php @@ -0,0 +1,7 @@ +handle = $this->client->call( - 'pactffi_verifier_new_for_application', + $result = $this->client->verifierNewForApplication( $config->getCallingApp()->getName(), $config->getCallingApp()->getVersion() ); + if (!$result) { + throw new VerifierNotCreatedException(); + } + $this->handle = $result; } private function setProviderInfo(VerifierConfigInterface $config): void { - $this->client->call( - 'pactffi_verifier_set_provider_info', + $this->client->verifierSetProviderInfo( $this->handle, $config->getProviderInfo()->getName(), $config->getProviderInfo()->getScheme(), @@ -56,8 +61,7 @@ private function setProviderInfo(VerifierConfigInterface $config): void private function setProviderTransports(VerifierConfigInterface $config): void { foreach ($config->getProviderTransports() as $transport) { - $this->client->call( - 'pactffi_verifier_add_provider_transport', + $this->client->verifierAddProviderTransport( $this->handle, $transport->getProtocol(), $transport->getPort(), @@ -69,8 +73,7 @@ private function setProviderTransports(VerifierConfigInterface $config): void private function setFilterInfo(VerifierConfigInterface $config): void { - $this->client->call( - 'pactffi_verifier_set_filter_info', + $this->client->verifierSetFilterInfo( $this->handle, $config->getFilterInfo()->getFilterDescription(), $config->getFilterInfo()->getFilterState(), @@ -80,8 +83,7 @@ private function setFilterInfo(VerifierConfigInterface $config): void private function setProviderState(VerifierConfigInterface $config): void { - $this->client->call( - 'pactffi_verifier_set_provider_state', + $this->client->verifierSetProviderState( $this->handle, $config->getProviderState()->getStateChangeUrl() ? (string) $config->getProviderState()->getStateChangeUrl() : null, $config->getProviderState()->isStateChangeTeardown(), @@ -91,8 +93,7 @@ private function setProviderState(VerifierConfigInterface $config): void private function setVerificationOptions(VerifierConfigInterface $config): void { - $this->client->call( - 'pactffi_verifier_set_verification_options', + $this->client->verifierSetVerificationOptions( $this->handle, $config->getVerificationOptions()->isDisableSslVerification(), $config->getVerificationOptions()->getRequestTimeout() @@ -103,13 +104,11 @@ private function setPublishOptions(VerifierConfigInterface $config): void { if ($config->isPublishResults()) { $providerTags = ArrayData::createFrom($config->getPublishOptions()->getProviderTags()); - $this->client->call( - 'pactffi_verifier_set_publish_options', + $this->client->verifierSetPublishOptions( $this->handle, $config->getPublishOptions()->getProviderVersion(), $config->getPublishOptions()->getBuildUrl(), - $providerTags?->getItems(), - $providerTags?->getSize(), + $providerTags, $config->getPublishOptions()->getProviderBranch() ); } @@ -118,19 +117,16 @@ private function setPublishOptions(VerifierConfigInterface $config): void private function setConsumerFilters(VerifierConfigInterface $config): void { $filterConsumerNames = ArrayData::createFrom($config->getConsumerFilters()->getFilterConsumerNames()); - $this->client->call( - 'pactffi_verifier_set_consumer_filters', + $this->client->verifierSetConsumerFilters( $this->handle, - $filterConsumerNames?->getItems(), - $filterConsumerNames?->getSize() + $filterConsumerNames ); } private function setCustomHeaders(VerifierConfigInterface $config): void { foreach ($config->getCustomHeaders()->getHeaders() as $name => $value) { - $this->client->call( - 'pactffi_verifier_add_custom_header', + $this->client->verifierAddCustomHeader( $this->handle, $name, $value @@ -147,22 +143,21 @@ private function setLogLevel(VerifierConfigInterface $config): void public function addFile(string $file): self { - $this->client->call('pactffi_verifier_add_file_source', $this->handle, $file); + $this->client->verifierAddFileSource($this->handle, $file); return $this; } public function addDirectory(string $directory): self { - $this->client->call('pactffi_verifier_add_directory_source', $this->handle, $directory); + $this->client->verifierAddDirectorySource($this->handle, $directory); return $this; } public function addUrl(UrlInterface $url): self { - $this->client->call( - 'pactffi_verifier_url_source', + $this->client->verifierAddUrlSource( $this->handle, (string) $url->getUrl(), $url->getUsername(), @@ -178,8 +173,7 @@ public function addBroker(BrokerInterface $broker): self $providerTags = ArrayData::createFrom($broker->getProviderTags()); $consumerVersionSelectors = ArrayData::createFrom(iterator_to_array($broker->getConsumerVersionSelectors())); $consumerVersionTags = ArrayData::createFrom($broker->getConsumerVersionTags()); - $this->client->call( - 'pactffi_verifier_broker_source_with_selectors', + $this->client->verifierBrokerSourceWithSelectors( $this->handle, (string) $broker->getUrl(), $broker->getUsername(), @@ -187,13 +181,10 @@ public function addBroker(BrokerInterface $broker): self $broker->getToken(), $broker->isEnablePending(), $broker->getIncludeWipPactSince(), - $providerTags?->getItems(), - $providerTags?->getSize(), + $providerTags, $broker->getProviderBranch(), - $consumerVersionSelectors?->getItems(), - $consumerVersionSelectors?->getSize(), - $consumerVersionTags?->getItems(), - $consumerVersionTags?->getSize() + $consumerVersionSelectors, + $consumerVersionTags ); return $this; @@ -201,11 +192,15 @@ public function addBroker(BrokerInterface $broker): self public function verify(): bool { - $error = $this->client->call('pactffi_verifier_execute', $this->handle); + $error = $this->client->verifierExecute($this->handle); if ($this->logger) { - $this->logger->log($this->client->call('pactffi_verifier_json', $this->handle)); + $output = $this->client->verifierJson($this->handle); + if (is_null($output)) { + throw new InvalidVerifierHandleException(); + } + $this->logger->log($output); } - $this->client->call('pactffi_verifier_shutdown', $this->handle); + $this->client->verifierShutdown($this->handle); return !$error; } diff --git a/tests/PhpPact/FFI/ClientTest.php b/tests/PhpPact/FFI/ClientTest.php index 8c3824c1..73105774 100644 --- a/tests/PhpPact/FFI/ClientTest.php +++ b/tests/PhpPact/FFI/ClientTest.php @@ -2,6 +2,7 @@ namespace PhpPactTest\FFI; +use FFI\CData; use PhpPact\FFI\Client; use PhpPact\FFI\ClientInterface; use PhpPact\FFI\Model\BinaryData; @@ -195,6 +196,129 @@ public function testCreateMockServerForTransport(): void $this->assertSame(-1, $result); } + public function testVerifierNewForApplication(): void + { + $result = $this->client->verifierNewForApplication('name', '1.1'); + $this->assertInstanceOf(CData::class, $result); + } + + public function testVerifierSetProviderInfo(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierSetProviderInfo($handle, null, null, null, null, null); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierAddProviderTransport(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierAddProviderTransport($handle, null, null, null, null); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierSetFilterInfo(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierSetFilterInfo($handle, null, null, true); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierSetProviderState(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierSetProviderState($handle, null, true, true); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierSetVerificationOptions(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $result = $this->client->verifierSetVerificationOptions($handle, true, 1); + $this->assertSame(0, $result); + } + + public function testVerifierSetPublishOptions(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $result = $this->client->verifierSetPublishOptions($handle, '1.0.0', null, null, 'some-branch'); + $this->assertSame(0, $result); + } + + public function testVerifierSetConsumerFilters(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierSetConsumerFilters($handle, null); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierAddCustomHeader(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierAddCustomHeader($handle, 'name', 'value'); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierAddFileSource(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierAddFileSource($handle, '/path/to/file'); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierAddDirectorySource(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierAddDirectorySource($handle, '/path/to/directory'); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierAddUrlSource(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierAddUrlSource($handle, 'http://example.domain/file.ext', null, null, null); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierBrokerSourceWithSelectors(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierBrokerSourceWithSelectors( + $handle, + 'http://example.domain/file.ext', + null, + null, + null, + true, + null, + null, + null, + null, + null + ); + $this->expectNotToPerformAssertions(); + } + + public function testVerifierExecute(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $result = $this->client->verifierExecute($handle); + $this->assertSame(0, $result); + } + + public function testVerifierJson(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $result = $this->client->verifierJson($handle); + $this->assertSame('{"errors":[],"notices":[],"output":[],"pendingErrors":[],"result":true}', $result); + } + + public function testVerifierShutdown(): void + { + $handle = $this->client->verifierNewForApplication('name', '1.1'); + $this->client->verifierShutdown($handle); + $this->expectNotToPerformAssertions(); + } + public function testGetInteractionPartRequest(): void { $this->assertSame(0, $this->client->getInteractionPartRequest()); diff --git a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php index 71ba77da..3ff2d152 100644 --- a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php +++ b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php @@ -29,10 +29,6 @@ class VerifierTest extends TestCase private VerifierConfigInterface $config; private LoggerInterface&MockObject $logger; private CData $handle; - /** - * @var mixed[][] - */ - private array $calls; protected function setUp(): void { @@ -82,45 +78,22 @@ private function setUpCalls(bool $hasProviderTags = true, bool $hasFilterConsume $this->config->getCustomHeaders() ->setHeaders($customHeaders = ['name-1' => 'value-1', 'name-2' => 'value-2']); $this->config->setLogLevel($logLevel = 'info'); - $this->calls = [ - ['pactffi_verifier_new_for_application', $callingAppName, $callingAppVersion, $this->handle], - ['pactffi_verifier_set_provider_info', $this->handle, $providerName, $providerScheme, $providerHost, $providerPort, $providerPath, null], - ['pactffi_verifier_add_provider_transport', $this->handle, $transportProtocol, $transportPort, $transportPath, $transportScheme, null], - ['pactffi_verifier_set_filter_info', $this->handle, $filterDescription, $filterState, $filterNoState, null], - ['pactffi_verifier_set_provider_state', $this->handle, (string) $stateChangeUrl, $stateChangeTearDown, $stateChangeAsBody, null], - ['pactffi_verifier_set_verification_options', $this->handle, $disableSslVerification, $requestTimeout, null], - [ - 'pactffi_verifier_set_publish_options', - $this->handle, - $providerVersion, - $buildUrl, - $hasProviderTags ? $this->isInstanceOf(CData::class) : null, - $hasProviderTags ? count($providerTags) : null, - $providerBranch, - null - ], - [ - 'pactffi_verifier_set_consumer_filters', - $this->handle, - $hasFilterConsumerNames ? $this->isInstanceOf(CData::class) : null, - $hasFilterConsumerNames ? count($filterConsumerNames) : null, - null - ], - [ - 'pactffi_verifier_add_custom_header', - $this->handle, - 'name-1', - $customHeaders['name-1'], - null, - ], - [ - 'pactffi_verifier_add_custom_header', - $this->handle, - 'name-2', - $customHeaders['name-2'], - null, - ], - ]; + $this->expectsVerifierNewForApplication($callingAppName, $callingAppVersion, $this->handle); + $this->expectsVerifierSetProviderInfo($this->handle, $providerName, $providerScheme, $providerHost, $providerPort, $providerPath); + $this->expectsVerifierAddProviderTransport($this->handle, $transportProtocol, $transportPort, $transportPath, $transportScheme); + $this->expectsVerifierSetFilterInfo($this->handle, $filterDescription, $filterState, $filterNoState); + $this->expectsVerifierSetProviderState($this->handle, (string) $stateChangeUrl, $stateChangeTearDown, $stateChangeAsBody); + $this->expectsVerifierSetVerificationOptions($this->handle, $disableSslVerification, $requestTimeout, 0); + $this->expectsVerifierSetPublishOptions( + $this->handle, + $providerVersion, + $buildUrl, + $providerTags, + $providerBranch, + 0 + ); + $this->expectsVerifierSetConsumerFilters($this->handle, $filterConsumerNames); + $this->expectsVerifierAddCustomHeader($this->handle, $customHeaders); $this->expectsInitWithLogLevel(strtoupper($logLevel)); } @@ -131,7 +104,6 @@ private function setUpCalls(bool $hasProviderTags = true, bool $hasFilterConsume public function testConstruct(bool $hasProviderTags, bool $hasFilterConsumerNames): void { $this->setUpCalls($hasProviderTags, $hasFilterConsumerNames); - $this->assertClientCalls($this->calls); $this->verifier = new Verifier($this->config, $this->logger, $this->client); } @@ -139,8 +111,7 @@ public function testAddFile(): void { $this->setUpCalls(); $file = '/path/to/file.json'; - $this->calls[] = ['pactffi_verifier_add_file_source', $this->handle, $file, null]; - $this->assertClientCalls($this->calls); + $this->expectsVerifierAddFileSource($this->handle, $file); $this->verifier = new Verifier($this->config, $this->logger, $this->client); $this->assertSame($this->verifier, $this->verifier->addFile($file)); } @@ -149,8 +120,7 @@ public function testAddDirectory(): void { $this->setUpCalls(); $directory = '/path/to/directory'; - $this->calls[] = ['pactffi_verifier_add_directory_source', $this->handle, $directory, null]; - $this->assertClientCalls($this->calls); + $this->expectsVerifierAddDirectorySource($this->handle, $directory); $this->verifier = new Verifier($this->config, $this->logger, $this->client); $this->assertSame($this->verifier, $this->verifier->addDirectory($directory)); } @@ -164,8 +134,7 @@ public function testAddUrl(): void ->setToken($token = 'secret token') ->setUsername($username = 'my username') ->setPassword($password = 'secret password'); - $this->calls[] = ['pactffi_verifier_url_source', $this->handle, (string) $url, $username, $password, $token, null]; - $this->assertClientCalls($this->calls); + $this->expectsVerifierAddUrlSource($this->handle, (string) $url, $username, $password, $token); $this->verifier = new Verifier($this->config, $this->logger, $this->client); $this->assertSame($this->verifier, $this->verifier->addUrl($source)); } @@ -191,52 +160,44 @@ public function testAddBroker(bool $hasVersionSelectors, bool $hasProviderTags, ->setUsername($username = 'my username') ->setPassword($password = 'secret password') ->setEnablePending($enablePending = true) - ->setIncludeWipPactSince($wipPactSince = '2020-01-30') + ->setIncludeWipPactSince($includeWipPactSince = '2020-01-30') ->setProviderTags($providerTags = $hasProviderTags ? ['prod', 'staging'] : []) ->setProviderBranch($providerBranch = 'main') ->setConsumerVersionSelectors($consumerVersionSelectors) ->setConsumerVersionTags($consumerVersionTags = $hasConsumerVersionTags ? ['dev'] : []); - $this->calls[] = [ - 'pactffi_verifier_broker_source_with_selectors', + $this->expectsVerifierBrokerSourceWithSelectors( $this->handle, (string) $url, $username, $password, $token, $enablePending, - $wipPactSince, - $hasProviderTags ? $this->isInstanceOf(CData::class) : null, - $hasProviderTags ? count($providerTags) : null, + $includeWipPactSince, + $providerTags, $providerBranch, - $hasVersionSelectors ? $this->isInstanceOf(CData::class) : null, - $hasVersionSelectors ? count($consumerVersionSelectors) : null, - $hasConsumerVersionTags ? $this->isInstanceOf(CData::class) : null, - $hasConsumerVersionTags ? count($consumerVersionTags) : null, - null - ]; - $this->assertClientCalls($this->calls); + $consumerVersionSelectors, + $consumerVersionTags, + ); $this->verifier = new Verifier($this->config, $this->logger, $this->client); $this->assertSame($this->verifier, $this->verifier->addBroker($source)); } - #[TestWith([0, true, false])] - #[TestWith([0, true, true])] - #[TestWith([1, false, false])] - #[TestWith([2, false, false])] - public function testVerify(int $error, bool $success, bool $hasLogger): void + #[TestWith([0, true, false, null])] + #[TestWith([0, true, true, null])] + #[TestWith([0, true, true, '{"key": "value"}'])] + #[TestWith([1, false, false, null])] + #[TestWith([2, false, false, null])] + public function testVerify(int $error, bool $success, bool $hasLogger, ?string $json): void { $this->setUpCalls(); $json = '{"key": "value"}'; - $this->calls[] = ['pactffi_verifier_execute', $this->handle, $error]; + $this->expectsVerifierExecute($this->handle, $error); + $this->expectsVerifierJson($this->handle, $hasLogger, $json); $this->logger ->expects($hasLogger ? $this->once() : $this->never()) ->method('log') ->with($json); - if ($hasLogger) { - $this->calls[] = ['pactffi_verifier_json', $this->handle, $json]; - } - $this->calls[] = ['pactffi_verifier_shutdown', $this->handle, null]; - $this->assertClientCalls($this->calls); + $this->expectsVerifierShutdown($this->handle); $this->verifier = new Verifier($this->config, $hasLogger ? $this->logger : null, $this->client); $this->assertSame($success, $this->verifier->verify()); }