-
Notifications
You must be signed in to change notification settings - Fork 92
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
Use Rust FFI #12: Binary #332
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="../../../vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="PhpPact Example Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="PACT_LOGLEVEL" value="DEBUG"/> | ||
</php> | ||
</phpunit> |
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,31 @@ | ||
<?php | ||
|
||
namespace BinaryConsumer\Service; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Uri; | ||
|
||
/** | ||
* Example HTTP Service | ||
*/ | ||
class HttpClientService | ||
{ | ||
private Client $httpClient; | ||
|
||
private string $baseUri; | ||
|
||
public function __construct(string $baseUri) | ||
{ | ||
$this->httpClient = new Client(); | ||
$this->baseUri = $baseUri; | ||
} | ||
|
||
public function getImageContent(): string | ||
{ | ||
$response = $this->httpClient->get(new Uri("{$this->baseUri}/image.jpg"), [ | ||
'headers' => ['Accept' => 'image/jpeg'] | ||
]); | ||
|
||
return $response->getBody(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
example/binary/consumer/tests/Service/HttpClientServiceTest.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,53 @@ | ||
<?php | ||
|
||
namespace BinaryConsumer\Tests\Service; | ||
|
||
use BinaryConsumer\Service\HttpClientService; | ||
use PhpPact\Consumer\InteractionBuilder; | ||
use PhpPact\Consumer\Model\Body\Binary; | ||
use PhpPact\Consumer\Model\ConsumerRequest; | ||
use PhpPact\Consumer\Model\ProviderResponse; | ||
use PhpPact\Standalone\MockService\MockServerConfig; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class HttpClientServiceTest extends TestCase | ||
{ | ||
public function testGetImageContent() | ||
{ | ||
$imageContent = file_get_contents(__DIR__ . '/../_resource/image.jpg'); | ||
|
||
$request = new ConsumerRequest(); | ||
$request | ||
->setMethod('GET') | ||
->setPath('/image.jpg') | ||
->addHeader('Accept', 'image/jpeg'); | ||
|
||
$response = new ProviderResponse(); | ||
$response | ||
->setStatus(200) | ||
->addHeader('Content-Type', 'image/jpeg') | ||
->setBody(new Binary($imageContent, in_array(php_uname('m'), ['AMD64', 'arm64']) ? 'application/octet-stream' : 'image/jpeg')); | ||
|
||
$config = new MockServerConfig(); | ||
$config | ||
->setConsumer('binaryConsumer') | ||
->setProvider('binaryProvider') | ||
->setPactDir(__DIR__.'/../../../pacts'); | ||
if ($logLevel = \getenv('PACT_LOGLEVEL')) { | ||
$config->setLogLevel($logLevel); | ||
} | ||
$builder = new InteractionBuilder($config); | ||
$builder | ||
->given('Image file image.jpg exists') | ||
->uponReceiving('A get request to /image.jpg') | ||
->with($request) | ||
->willRespondWith($response); | ||
|
||
$service = new HttpClientService($config->getBaseUri()); | ||
$imageContentResult = $service->getImageContent(); | ||
$verifyResult = $builder->verify(); | ||
|
||
$this->assertTrue($verifyResult); | ||
$this->assertEquals($imageContent, $imageContentResult); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,56 @@ | ||
{ | ||
"consumer": { | ||
"name": "binaryConsumer" | ||
}, | ||
"interactions": [ | ||
{ | ||
"description": "A get request to /image.jpg", | ||
"providerStates": [ | ||
{ | ||
"name": "Image file image.jpg exists" | ||
} | ||
], | ||
"request": { | ||
"headers": { | ||
"Accept": "image/jpeg" | ||
}, | ||
"method": "GET", | ||
"path": "/image.jpg" | ||
}, | ||
"response": { | ||
"body": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/wAALCAAPAA8BAREA/8QAFgABAQEAAAAAAAAAAAAAAAAAAgME/8QAIBAAAgIDAAIDAQAAAAAAAAAAAQIDBAUREhMhACIxUf/aAAgBAQAAPwBNlclNW6S0s7wbvv4K6iNI2HJcp9Rrnk+T0ByNofwZIjdnyYx0stWrYWAP5y03SBjt+GQ/YnmHbaUkfoA107Br40Wa+DrRLNXhjchECDwyjjlXPpdFSo3Gw5CbDFdmUVOrHcfEU4JYb2PWSJCzbVPUEmh7PQaOZHOyD0x/hHz/2Q==", | ||
"headers": { | ||
"Content-Type": "image/jpeg" | ||
}, | ||
"matchingRules": { | ||
"body": { | ||
"$": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "contentType", | ||
"value": "image/jpeg" | ||
} | ||
] | ||
} | ||
}, | ||
"header": {} | ||
}, | ||
"status": 200 | ||
} | ||
} | ||
], | ||
"metadata": { | ||
"pactRust": { | ||
"ffi": "0.4.7", | ||
"mockserver": "1.2.3", | ||
"models": "1.1.9" | ||
}, | ||
"pactSpecification": { | ||
"version": "3.0.0" | ||
} | ||
}, | ||
"provider": { | ||
"name": "binaryProvider" | ||
} | ||
} |
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="../../../vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="PhpPact Example Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="PACT_LOGLEVEL" value="DEBUG"/> | ||
</php> | ||
</phpunit> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,62 @@ | ||
<?php | ||
|
||
namespace BinaryConsumer\Tests; | ||
|
||
use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig; | ||
use PhpPact\Standalone\ProviderVerifier\Verifier; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Process\Process; | ||
|
||
class PactVerifyTest extends TestCase | ||
{ | ||
private Process $process; | ||
|
||
/** | ||
* Run the PHP build-in web server. | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->process = new Process(['php', '-S', '127.0.0.1:7202', '-t', __DIR__ . '/../public/']); | ||
|
||
$this->process->start(); | ||
$this->process->waitUntil(function (): bool { | ||
$fp = @fsockopen('127.0.0.1', 7202); | ||
$isOpen = is_resource($fp); | ||
if ($isOpen) { | ||
fclose($fp); | ||
} | ||
|
||
return $isOpen; | ||
}); | ||
} | ||
|
||
/** | ||
* Stop the web server process once complete. | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
$this->process->stop(); | ||
} | ||
|
||
/** | ||
* This test will run after the web server is started. | ||
*/ | ||
public function testPactVerifyConsumer() | ||
{ | ||
$config = new VerifierConfig(); | ||
$config->getProviderInfo() | ||
->setName('binaryProvider') // Providers name to fetch. | ||
->setHost('localhost') | ||
->setPort(7202); | ||
if ($level = \getenv('PACT_LOGLEVEL')) { | ||
$config->setLogLevel($level); | ||
} | ||
|
||
$verifier = new Verifier($config); | ||
$verifier->addFile(__DIR__ . '/../../pacts/binaryConsumer-binaryProvider.json'); | ||
|
||
$verifyResult = $verifier->verify(); | ||
|
||
$this->assertTrue($verifyResult); | ||
} | ||
} |
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="../../../vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="PhpPact Example Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="PACT_LOGLEVEL" value="DEBUG"/> | ||
</php> | ||
</phpunit> |
2 changes: 1 addition & 1 deletion
2
...rc/Consumer/Service/HttpClientService.php → ...onsumer/src/Service/HttpClientService.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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to pact-foundation/pact-reference#305 I presume
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think pact-foundation/pact-reference#171 is more precise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh yes, spot on 🔍