|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpPactTest\CompatibilitySuite\Context\Shared; |
| 4 | + |
| 5 | +use Behat\Behat\Context\Context; |
| 6 | +use GuzzleHttp\Psr7\Uri; |
| 7 | +use PhpPactTest\CompatibilitySuite\Constant\Mismatch; |
| 8 | +use PhpPactTest\CompatibilitySuite\Service\ProviderStateServerInterface; |
| 9 | +use PhpPactTest\CompatibilitySuite\Service\ProviderVerifierInterface; |
| 10 | +use PhpPactTest\CompatibilitySuite\Service\ServerInterface; |
| 11 | +use PHPUnit\Framework\Assert; |
| 12 | + |
| 13 | +final class ProviderContext implements Context |
| 14 | +{ |
| 15 | + public function __construct( |
| 16 | + private ServerInterface $server, |
| 17 | + private ProviderVerifierInterface $providerVerifier, |
| 18 | + private ProviderStateServerInterface $providerStateServer, |
| 19 | + ) { |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @When the verification is run |
| 24 | + */ |
| 25 | + public function theVerificationIsRun(): void |
| 26 | + { |
| 27 | + $this->providerVerifier->getConfig()->getProviderInfo()->setPort($this->server->getPort()); |
| 28 | + $this->providerVerifier->verify(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @Then the verification will be successful |
| 33 | + */ |
| 34 | + public function theVerificationWillBeSuccessful(): void |
| 35 | + { |
| 36 | + Assert::assertTrue($this->providerVerifier->getVerifyResult()->isSuccess()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @Then the verification will NOT be successful |
| 41 | + */ |
| 42 | + public function theVerificationWillNotBeSuccessful(): void |
| 43 | + { |
| 44 | + Assert::assertFalse($this->providerVerifier->getVerifyResult()->isSuccess()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @Given a provider state callback is configured |
| 49 | + */ |
| 50 | + public function aProviderStateCallbackIsConfigured(): void |
| 51 | + { |
| 52 | + $port = $this->providerStateServer->getPort(); |
| 53 | + $this->providerVerifier |
| 54 | + ->getConfig() |
| 55 | + ->getProviderState() |
| 56 | + ->setStateChangeUrl(new Uri("http://localhost:$port/pact-change-state")) |
| 57 | + ->setStateChangeTeardown(true); |
| 58 | + ; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @Then the provider state callback will be called before the verification is run |
| 63 | + */ |
| 64 | + public function theProviderStateCallbackWillBeCalledBeforeTheVerificationIsRun(): void |
| 65 | + { |
| 66 | + Assert::assertTrue($this->providerStateServer->hasAction(ProviderStateServerInterface::ACTION_SETUP)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @Then the provider state callback will receive a setup call with :state as the provider state parameter |
| 71 | + */ |
| 72 | + public function theProviderStateCallbackWillReceiveASetupCallWithAsTheProviderStateParameter(string $state): void |
| 73 | + { |
| 74 | + Assert::assertTrue($this->providerStateServer->hasState(ProviderStateServerInterface::ACTION_SETUP, $state)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @Then the provider state callback will be called after the verification is run |
| 79 | + */ |
| 80 | + public function theProviderStateCallbackWillBeCalledAfterTheVerificationIsRun(): void |
| 81 | + { |
| 82 | + Assert::assertTrue($this->providerStateServer->hasAction(ProviderStateServerInterface::ACTION_TEARDOWN)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @Then the provider state callback will receive a teardown call :state as the provider state parameter |
| 87 | + */ |
| 88 | + public function theProviderStateCallbackWillReceiveATeardownCallAsTheProviderStateParameter(string $state): void |
| 89 | + { |
| 90 | + Assert::assertTrue($this->providerStateServer->hasState(ProviderStateServerInterface::ACTION_TEARDOWN, $state)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @Given a provider state callback is configured, but will return a failure |
| 95 | + */ |
| 96 | + public function aProviderStateCallbackIsConfiguredButWillReturnAFailure(): void |
| 97 | + { |
| 98 | + $port = $this->providerStateServer->getPort(); |
| 99 | + $this->providerVerifier |
| 100 | + ->getConfig() |
| 101 | + ->getProviderState() |
| 102 | + ->setStateChangeUrl(new Uri("http://localhost:$port/failed-pact-change-state")) |
| 103 | + ->setStateChangeTeardown(true); |
| 104 | + ; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @Then the provider state callback will NOT receive a teardown call |
| 109 | + */ |
| 110 | + public function theProviderStateCallbackWillNotReceiveATeardownCall(): void |
| 111 | + { |
| 112 | + Assert::assertFalse($this->providerStateServer->hasAction(ProviderStateServerInterface::ACTION_TEARDOWN)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @Then the verification results will contain a :error error |
| 117 | + */ |
| 118 | + public function theVerificationResultsWillContainAError(string $error): void |
| 119 | + { |
| 120 | + $output = json_decode($this->providerVerifier->getVerifyResult()->getOutput(), true); |
| 121 | + $errors = array_reduce( |
| 122 | + $output['errors'], |
| 123 | + function (array $errors, array $error) { |
| 124 | + switch ($error['mismatch']['type']) { |
| 125 | + case 'error': |
| 126 | + $errors[] = Mismatch::VERIFIER_MISMATCH_ERROR_MAP[$error['mismatch']['message']]; |
| 127 | + break; |
| 128 | + |
| 129 | + case 'mismatches': |
| 130 | + foreach ($error['mismatch']['mismatches'] as $mismatch) { |
| 131 | + $errors[] = Mismatch::VERIFIER_MISMATCH_TYPE_MAP[$mismatch['type']]; |
| 132 | + } |
| 133 | + break; |
| 134 | + |
| 135 | + default: |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + return $errors; |
| 140 | + }, |
| 141 | + [] |
| 142 | + ); |
| 143 | + Assert::assertContains($error, $errors); |
| 144 | + } |
| 145 | +} |
0 commit comments