Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clearInteraction to mock service per test & add local pact writin… #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function setMethod ($method) {
else if (strtolower($method) == 'post') {
$this->setOption(CURLOPT_POST, 1);
}
else if (strtolower($method) == 'delete') {
$this->setOption( CURLOPT_CUSTOMREQUEST, 'DELETE');
}
$this->setOption(CURLOPT_HTTPHEADER, ['Content-Type: application/json','X-Pact-Mock-Service: true']);
}
}
Expand All @@ -53,4 +56,5 @@ public function execute () {
public function getOption ($name) {
return $this->options[$name];
}

}
45 changes: 36 additions & 9 deletions lib/MockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ function __construct($args) {
if (array_key_exists('pactfile_write_mode', $args) && ($args['pactfile_write_mode'] == 'update' || $args['pactfile_write_mode'] == 'overwrite')) {
$this->pactDetails['pactfile_write_mode'] = $args['pactfile_write_mode'];
}

if (array_key_exists('local_pact_location', $args)) {
$this->localPactLocation = $args['local_pact_location'];
}
}

private $host = '127.0.0.1';
private $port;
private $pactDetails = [];
private $interactions = [];
private $baseURL;
private $currentPact;
private $localPactLocation;

public function given($providerState) {
$interaction = new Interaction();
Expand All @@ -44,33 +50,54 @@ public function uponReceiving($description) {

private function setup() {
$interactions = $this->interactions;
$this->interactions = [];
$this->interactions = array();
$mockServiceRequests = new MockServiceRequests();
$mockServiceRequests->putInteractions($interactions, $this->baseURL);
}

private function verifyAndWrite() {
$this->verify();
$this->write();
}

private function verify() {
$mockServiceRequests = new MockServiceRequests();
$mockServiceRequests->getVerification($this->baseURL);
}

private function write() {
$mockServiceRequests = new MockServiceRequests();
$mockServiceRequests->postPact($this->pactDetails, $this->baseURL);
return $mockServiceRequests->postPact($this->pactDetails, $this->baseURL);
}

public function clear() {
$this->interactions = array();
$mockServiceRequests = new MockServiceRequests();
$mockServiceRequests->clearInteractions($this->baseURL);
}


public function run($testMethod) {
if (!is_callable($testMethod)) {
throw new \InvalidArgumentException('Error running MockService. Please provide a callable test method.');
}
$this->setup();
$testMethod();
$this->verifyAndWrite();
}
$this->verify();
$this->currentPact = $this->write();

return $this->currentPact;
}

public function flushLocalPact() {
if (!$this->currentPact) {
throw new \RuntimeException('No pacts produced. Nothing to write.');
}

$pact = json_decode($this->currentPact);

if ($pact == null) {
throw new \RuntimeException('Invalid pacts produced. Cannot to write.');
}
$fileName = $this->localPactLocation . '/' . strtolower($pact->consumer->name) . '-' . strtolower($pact->provider->name) . '.json';
$result = file_put_contents($fileName, $this->currentPact);

return $result;
}

}
17 changes: 16 additions & 1 deletion lib/MockServiceRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@ class MockServiceRequests {
function __construct($httpClient = null) {
$this->httpClient = is_null($httpClient) ? new HttpClient() : $httpClient;
}


public function clearInteractions($baseURL) {
$this->httpClient->setUrl($baseURL . '/interactions');
$this->httpClient->setMethod('DELETE');

$response = $this->httpClient->execute();
if (!$response) {
throw new \RuntimeException('Pact interaction clear failed');
}
}

public function putInteractions($interactions, $baseURL) {

$this->httpClient->setUrl($baseURL . '/interactions');
$this->httpClient->setMethod('PUT');
$this->httpClient->setBody(json_encode(['interactions' => $interactions]));

$response = $this->httpClient->execute();
if (!$response) {
throw new \RuntimeException('Pact interaction setup failed');
Expand All @@ -30,9 +42,12 @@ public function postPact($pactDetails, $baseURL) {
$this->httpClient->setUrl($baseURL . '/pact');
$this->httpClient->setMethod('POST');
$this->httpClient->setBody(json_encode($pactDetails));

$response = $this->httpClient->execute();
if (!$response) {
throw new \RuntimeException('Could not write the pact file');
}

return $response;
}
}