Skip to content

Commit

Permalink
refactor: Wrap ffi call > setup verifier methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Sep 28, 2024
1 parent 9e3c94d commit 6371c1d
Show file tree
Hide file tree
Showing 8 changed files with 597 additions and 111 deletions.
196 changes: 196 additions & 0 deletions helper/FFI/ClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<string, string> $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);
}
}
150 changes: 150 additions & 0 deletions src/PhpPact/FFI/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
Loading

0 comments on commit 6371c1d

Please sign in to comment.