Skip to content

Commit

Permalink
Merge pull request #433 from tienvx/add-pact-path-dto
Browse files Browse the repository at this point in the history
test(compatibility-suite): Add PactPath DTO
  • Loading branch information
tienvx committed Dec 25, 2023
2 parents e2f4b70 + f034d33 commit 96b862c
Show file tree
Hide file tree
Showing 24 changed files with 168 additions and 135 deletions.
18 changes: 11 additions & 7 deletions compatibility-suite/tests/Context/V1/Http/ProviderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Psr7\Uri;
use PhpPact\Standalone\ProviderVerifier\Model\Config\PublishOptions;
use PhpPact\Standalone\ProviderVerifier\Model\Source\Broker;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Service\InteractionsStorageInterface;
use PhpPactTest\CompatibilitySuite\Service\PactBrokerInterface;
use PhpPactTest\CompatibilitySuite\Service\PactWriterInterface;
Expand Down Expand Up @@ -41,8 +42,9 @@ public function aProviderIsStartedThatReturnsTheResponseFromInteraction(int $id)
*/
public function aPactFileForInteractionIsToBeVerified(int $id): void
{
$this->pactWriter->write($id, "c-$id");
$this->providerVerifier->addSource($this->pactWriter->getPactPath());
$pactPath = new PactPath("c-$id");
$this->pactWriter->write($id, $pactPath);
$this->providerVerifier->addSource($pactPath);
}

/**
Expand All @@ -59,7 +61,8 @@ public function aProviderIsStartedThatReturnsTheResponsesFromInteractions(string
*/
public function aPactFileForInteractionIsToBeVerifiedFromAPactBroker(int $id): void
{
$this->pactWriter->write($id, "c-$id");
$pactPath = new PactPath("c-$id");
$this->pactWriter->write($id, $pactPath);
$this->pactBroker->publish($id);
$broker = new Broker();
$broker->setUrl(new Uri('http:/localhost:9292'));
Expand Down Expand Up @@ -107,11 +110,12 @@ public function aFailedVerificationResultWillBePublishedBackForTheInteraction(in
*/
public function aPactFileForInteractionIsToBeVerifiedWithAProviderStateDefined(int $id, string $state): void
{
$this->pactWriter->write($id, "c-$id");
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$pactPath = new PactPath("c-$id");
$this->pactWriter->write($id, $pactPath);
$pact = json_decode(file_get_contents($pactPath), true);
$pact['interactions'][0]['providerStates'][] = ['name' => $state];
file_put_contents($this->pactWriter->getPactPath(), json_encode($pact));
$this->providerVerifier->addSource($this->pactWriter->getPactPath());
file_put_contents($pactPath, json_encode($pact));
$this->providerVerifier->addSource($pactPath);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions compatibility-suite/tests/Context/V3/Http/ConsumerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use PhpPact\Consumer\Model\Interaction;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Service\InteractionBuilderInterface;
use PhpPactTest\CompatibilitySuite\Service\InteractionsStorageInterface;
use PhpPactTest\CompatibilitySuite\Service\PactWriterInterface;
Expand All @@ -14,12 +15,14 @@ final class ConsumerContext implements Context
{
private Interaction $interaction;
private int $id = 1;
private PactPath $pactPath;

public function __construct(
private InteractionBuilderInterface $builder,
private PactWriterInterface $pactWriter,
private InteractionsStorageInterface $storage,
) {
$this->pactPath = new PactPath();
}

/**
Expand Down Expand Up @@ -48,15 +51,15 @@ public function aProviderStateIsSpecified(string $state): void
*/
public function thePactFileForTheTestIsGenerated(): void
{
$this->pactWriter->write($this->id);
$this->pactWriter->write($this->id, $this->pactPath);
}

/**
* @Then the interaction in the Pact file will contain :states provider state(s)
*/
public function theInteractionInThePactFileWillContainProviderStates(int $states): void
{
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$pact = json_decode(file_get_contents($this->pactPath), true);
Assert::assertCount($states, $pact['interactions'][0]['providerStates']);
}

Expand All @@ -65,7 +68,7 @@ public function theInteractionInThePactFileWillContainProviderStates(int $states
*/
public function theInteractionInThePactFileWillContainProviderState(string $name): void
{
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$pact = json_decode(file_get_contents($this->pactPath), true);
Assert::assertNotEmpty(array_filter(
$pact['interactions'][0]['providerStates'],
fn (array $providerState) => $providerState['name'] === $name
Expand All @@ -90,7 +93,7 @@ public function theProviderStateInThePactFileWillContainTheFollowingParameters(s
$rows = $table->getHash();
$row = reset($rows);
$params = json_decode($row['parameters'], true);
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$pact = json_decode(file_get_contents($this->pactPath), true);
Assert::assertNotEmpty(array_filter(
$pact['interactions'][0]['providerStates'],
fn (array $providerState) => $providerState['name'] === $name && $providerState['params'] === $params
Expand Down
12 changes: 8 additions & 4 deletions compatibility-suite/tests/Context/V3/Http/ProviderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Service\PactWriterInterface;
use PhpPactTest\CompatibilitySuite\Service\ProviderStateServerInterface;
use PhpPactTest\CompatibilitySuite\Service\ProviderVerifierInterface;
use PHPUnit\Framework\Assert;

final class ProviderContext implements Context
{
private PactPath $pactPath;

public function __construct(
private PactWriterInterface $pactWriter,
private ProviderStateServerInterface $providerStateServer,
private ProviderVerifierInterface $providerVerifier,
) {
$this->pactPath = new PactPath();
}

/**
* @Given a Pact file for interaction :id is to be verified with the following provider states defined:
*/
public function aPactFileForInteractionIsToBeVerifiedWithTheFollowingProviderStatesDefined(int $id, TableNode $table): void
{
$this->pactWriter->write($id);
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$this->pactWriter->write($id, $this->pactPath);
$pact = json_decode(file_get_contents($this->pactPath), true);
$rows = $table->getHash();
$pact['interactions'][0]['providerStates'] = array_map(fn (array $row): array => ['name' => $row['State Name'], 'params' => json_decode($row['Parameters'] ?? '{}', true)], $rows);
file_put_contents($this->pactWriter->getPactPath(), json_encode($pact));
$this->providerVerifier->addSource($this->pactWriter->getPactPath());
file_put_contents($this->pactPath, json_encode($pact));
$this->providerVerifier->addSource($this->pactPath);
}

/**
Expand Down
14 changes: 6 additions & 8 deletions compatibility-suite/tests/Context/V3/Message/ConsumerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpPact\Standalone\PactMessage\PactMessageConfig;
use PhpPactTest\CompatibilitySuite\Constant\Path;
use PhpPactTest\CompatibilitySuite\Model\Message;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Service\BodyStorageInterface;
use PhpPactTest\CompatibilitySuite\Service\BodyValidatorInterface;
use PhpPactTest\CompatibilitySuite\Service\FixtureLoaderInterface;
Expand All @@ -23,7 +24,7 @@ final class ConsumerContext implements Context
private object|null $receivedMessage;
private bool $verifyResult;
private array $pact;
private string $pactPath;
private PactPath $pactPath;

public function __construct(
private string $specificationVersion,
Expand All @@ -33,15 +34,12 @@ public function __construct(
private BodyStorageInterface $bodyStorage,
private FixtureLoaderInterface $fixtureLoader
) {
$consumer = sprintf('compatibility-suite_message-consumer_specification-%s_c', $specificationVersion);
$provider = 'p';
$pactDir = Path::PACTS_PATH;
$this->pactPath = "$pactDir/$consumer-$provider.json";
$this->pactPath = new PactPath(sprintf('message_consumer_specification_%s', $specificationVersion));
$config = new PactMessageConfig();
$config
->setConsumer($consumer)
->setProvider($provider)
->setPactDir($pactDir)
->setConsumer($this->pactPath->getConsumer())
->setProvider(PactPath::PROVIDER)
->setPactDir(Path::PACTS_PATH)
->setPactSpecificationVersion($specificationVersion)
->setPactFileWriteMode(PactConfigInterface::MODE_OVERWRITE);
$this->builder = new MessageBuilder($config);
Expand Down
22 changes: 12 additions & 10 deletions compatibility-suite/tests/Context/V3/Message/ProviderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Behat\Behat\Hook\Scope\BeforeStepScope;
use Behat\Gherkin\Node\TableNode;
use PhpPact\Standalone\ProviderVerifier\Model\Config\ProviderTransport;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Service\FixtureLoaderInterface;
use PhpPactTest\CompatibilitySuite\Service\InteractionBuilderInterface;
use PhpPactTest\CompatibilitySuite\Service\InteractionsStorageInterface;
Expand All @@ -18,6 +19,7 @@ final class ProviderContext implements Context
{
private int $id = 1;
private array $ids = [];
private PactPath $pactPath;

public function __construct(
private ServerInterface $server,
Expand All @@ -28,6 +30,7 @@ public function __construct(
private ParserInterface $parser,
private FixtureLoaderInterface $fixtureLoader
) {
$this->pactPath = new PactPath();
}

/**
Expand Down Expand Up @@ -77,8 +80,8 @@ public function registerInteractions(BeforeStepScope $scope): void
*/
public function aPactFileForIsToBeVerified(string $name, string $fixture): void
{
$this->pactWriter->write($name, $fixture, "c-$name");
$this->providerVerifier->addSource($this->pactWriter->getPactPath());
$this->pactWriter->write($name, $fixture, $this->pactPath);
$this->providerVerifier->addSource($this->pactPath);
}

/**
Expand All @@ -87,9 +90,9 @@ public function aPactFileForIsToBeVerified(string $name, string $fixture): void
public function aPactFileForIsToBeVerifiedWithProviderState(string $name, string $fixture, string $state): void
{
$this->aPactFileForIsToBeVerified($name, $fixture);
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$pact = json_decode(file_get_contents($this->pactPath), true);
$pact['messages'][0]['providerState'] = $state;
file_put_contents($this->pactWriter->getPactPath(), json_encode($pact));
file_put_contents($this->pactPath, json_encode($pact));
}

/**
Expand Down Expand Up @@ -125,11 +128,10 @@ public function aProviderIsStartedThatCanGenerateTheMessageWithAndTheFollowingMe
*/
public function aPactFileForIsToBeVerifiedWithTheFollowingMetadata(string $name, string $fixture, TableNode $table): void
{
$this->pactWriter->write($name, $fixture);
$this->providerVerifier->addSource($this->pactWriter->getPactPath());
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$this->aPactFileForIsToBeVerified($name, $fixture);
$pact = json_decode(file_get_contents($this->pactPath), true);
$pact['messages'][0]['metaData'] = $this->parser->parseMetadataTable($table->getHash());
file_put_contents($this->pactWriter->getPactPath(), json_encode($pact));
file_put_contents($this->pactPath, json_encode($pact));
}

/**
Expand All @@ -156,11 +158,11 @@ public function aPactFileForIsToBeVerifiedWithTheFollowing(string $name, TableNo
}
}
$this->aPactFileForIsToBeVerified($name, $body);
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$pact = json_decode(file_get_contents($this->pactPath), true);
if (isset($metadata)) {
$pact['messages'][0]['metadata'] = array_merge($pact['messages'][0]['metadata'], $this->parser->parseMetadataMultiValues($metadata));
}
$pact['messages'][0]['matchingRules'] = $matchingRules;
file_put_contents($this->pactWriter->getPactPath(), json_encode($pact));
file_put_contents($this->pactPath, json_encode($pact));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Psr7\Uri;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Service\BodyStorageInterface;
use PhpPactTest\CompatibilitySuite\Service\GeneratorServerInterface;
use PhpPactTest\CompatibilitySuite\Service\InteractionBuilderInterface;
Expand All @@ -17,6 +18,7 @@
final class RequestGeneratorsContext implements Context
{
private int $id = 1;
private PactPath $pactPath;

public function __construct(
private InteractionBuilderInterface $builder,
Expand All @@ -27,6 +29,7 @@ public function __construct(
private ProviderVerifierInterface $providerVerifier,
private BodyStorageInterface $bodyStorage,
) {
$this->pactPath = new PactPath();
}

/**
Expand All @@ -52,9 +55,9 @@ public function aRequestConfiguredWithTheFollowingGenerators(TableNode $table):
public function theRequestIsPreparedForUse(): void
{
$this->generatorServer->start();
$this->pactWriter->write($this->id);
$this->pactWriter->write($this->id, $this->pactPath);
$this->providerVerifier->getConfig()->getProviderInfo()->setPort($this->generatorServer->getPort());
$this->providerVerifier->addSource($this->pactWriter->getPactPath());
$this->providerVerifier->addSource($this->pactPath);
$this->providerVerifier->verify();
$this->generatorServer->stop();
$this->bodyStorage->setBody($this->generatorServer->getBody());
Expand All @@ -74,7 +77,7 @@ public function theGeneratorTestModeIsSetAs(string $mode): void
public function theRequestIsPreparedForUseWithAProviderStateContext(TableNode $table): void
{
$this->generatorServer->start();
$this->pactWriter->write($this->id);
$this->pactWriter->write($this->id, $this->pactPath);
$port = $this->generatorServer->getPort();
$this->providerVerifier->getConfig()->getProviderInfo()->setPort($port);
$params = json_decode($table->getRow(0)[0], true);
Expand All @@ -83,7 +86,7 @@ public function theRequestIsPreparedForUseWithAProviderStateContext(TableNode $t
->getProviderState()
->setStateChangeUrl(new Uri("http://localhost:$port/return-provider-state-values?" . http_build_query($params)))
->setStateChangeTeardown(false);
$this->providerVerifier->addSource($this->pactWriter->getPactPath());
$this->providerVerifier->addSource($this->pactPath);
$this->providerVerifier->verify();
$this->generatorServer->stop();
$this->bodyStorage->setBody($this->generatorServer->getBody());
Expand Down
10 changes: 6 additions & 4 deletions compatibility-suite/tests/Context/V4/CombinedContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Behat\Behat\Context\Context;
use PhpPact\Config\PactConfigInterface;
use PhpPact\Consumer\Model\Interaction;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Service\InteractionBuilderInterface;
use PhpPactTest\CompatibilitySuite\Service\InteractionsStorageInterface;
use PhpPactTest\CompatibilitySuite\Service\MessagePactWriterInterface;
Expand All @@ -14,13 +14,15 @@
final class CombinedContext implements Context
{
private int $id = 1;
private PactPath $pactPath;

public function __construct(
private InteractionBuilderInterface $builder,
private InteractionsStorageInterface $storage,
private PactWriterInterface $pactWriter,
private MessagePactWriterInterface $messagePactWriter,
) {
$this->pactPath = new PactPath();
}

/**
Expand Down Expand Up @@ -48,16 +50,16 @@ public function aMessageInteractionIsBeingDefinedForAConsumerTest(): void
*/
public function thePactFileForTheTestIsGenerated(): void
{
$this->pactWriter->write($this->id, 'c', 'p', PactConfigInterface::MODE_MERGE);
$this->messagePactWriter->write('message interaction', '', 'c', 'p', PactConfigInterface::MODE_MERGE);
$this->pactWriter->write($this->id, $this->pactPath, PactConfigInterface::MODE_MERGE);
$this->messagePactWriter->write('message interaction', '', $this->pactPath, PactConfigInterface::MODE_MERGE);
}

/**
* @Then there will be an interaction in the Pact file with a type of :type
*/
public function thereWillBeAnInteractionInThePactFileWithATypeOf(string $type): void
{
$pact = json_decode(file_get_contents($this->pactWriter->getPactPath()), true);
$pact = json_decode(file_get_contents($this->pactPath), true);
$types = array_map(fn (array $interaction) => $interaction['type'], $pact['interactions']);
Assert::assertContains($type, $types);
}
Expand Down
Loading

0 comments on commit 96b862c

Please sign in to comment.