-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
223 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Driver\Interaction; | ||
|
||
class MessageDriver extends AbstractDriver implements MessageDriverInterface | ||
{ | ||
public function newInteraction(string $description): void | ||
{ | ||
$this->id = $this->ffi->call('pactffi_new_message_interaction', $this->pactDriver->getId(), $description); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setMetadata(array $metadata): void | ||
{ | ||
foreach ($metadata as $key => $value) { | ||
$this->ffi->call('pactffi_message_with_metadata', $this->id, (string) $key, (string) $value); | ||
} | ||
} | ||
|
||
public function reify(): string | ||
{ | ||
return $this->ffi->call('pactffi_message_reify', $this->id); | ||
} | ||
|
||
public function update(): void | ||
{ | ||
$this->pactDriver->writePact(); | ||
$this->pactDriver->cleanUp(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/PhpPact/Consumer/Driver/Interaction/MessageDriverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Driver\Interaction; | ||
|
||
interface MessageDriverInterface extends DriverInterface | ||
{ | ||
/** | ||
* @param array<string, string> $metadata | ||
*/ | ||
public function setMetadata(array $metadata): void; | ||
|
||
public function reify(): string; | ||
|
||
public function update(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Factory; | ||
|
||
use PhpPact\Consumer\Driver\Interaction\MessageDriver; | ||
use PhpPact\Consumer\Driver\Pact\PactDriver; | ||
use PhpPact\Config\PactConfigInterface; | ||
use PhpPact\Consumer\Service\FFI; | ||
use PhpPact\Consumer\Service\MessageRegistryInterface; | ||
use PhpPact\Consumer\Service\MessageRegistry; | ||
|
||
class MessageRegistryFactory | ||
{ | ||
public static function create(PactConfigInterface $config): MessageRegistryInterface | ||
{ | ||
$ffi = new FFI(); | ||
$pactDriver = new PactDriver($ffi, $config); | ||
$messageDriver = new MessageDriver($ffi, $pactDriver); | ||
|
||
return new MessageRegistry($messageDriver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Service; | ||
|
||
use PhpPact\Consumer\Driver\Interaction\DriverInterface; | ||
use PhpPact\Consumer\Driver\Interaction\MessageDriverInterface; | ||
use PhpPact\Consumer\Model\Message; | ||
|
||
class MessageRegistry implements MessageRegistryInterface | ||
{ | ||
public function __construct(private MessageDriverInterface $driver) | ||
{ | ||
} | ||
|
||
public function registerMessage(Message $message): void | ||
{ | ||
$this | ||
->newInteraction($message) | ||
->given($message) | ||
->expectsToReceive($message) | ||
->withMetadata($message) | ||
->withContent($message); | ||
} | ||
|
||
private function newInteraction(Message $message): self | ||
{ | ||
$this->driver->newInteraction($message->getDescription()); | ||
|
||
return $this; | ||
} | ||
|
||
private function given(Message $message): self | ||
{ | ||
$this->driver->setProviderStates($message->getProviderStates()); | ||
|
||
return $this; | ||
} | ||
|
||
private function expectsToReceive(Message $message): self | ||
{ | ||
$this->driver->setDescription($message->getDescription()); | ||
|
||
return $this; | ||
} | ||
|
||
private function withMetadata(Message $message): self | ||
{ | ||
$this->driver->setMetadata($message->getMetadata()); | ||
|
||
return $this; | ||
} | ||
|
||
private function withContent(Message $message): self | ||
{ | ||
if (\is_string($message->getContents())) { | ||
$contents = $message->getContents(); | ||
$contentType = 'text/plain'; | ||
} else { | ||
$contents = \json_encode($message->getContents(), JSON_THROW_ON_ERROR); | ||
$contentType = 'application/json'; | ||
} | ||
|
||
$this->driver->setBody(DriverInterface::REQUEST, $contentType, $contents); | ||
|
||
return $this; | ||
} | ||
|
||
public function reify(Message $message): string | ||
{ | ||
$this->registerMessage($message); | ||
|
||
return $this->driver->reify(); | ||
} | ||
|
||
public function update(): bool | ||
{ | ||
$this->driver->update(); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Service; | ||
|
||
use PhpPact\Consumer\Model\Message; | ||
|
||
interface MessageRegistryInterface | ||
{ | ||
public function reify(Message $pact): string; | ||
|
||
public function update(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.