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

Use Rust FFI #13: Multipart #333

Merged
merged 5 commits into from
Sep 12, 2023
Merged
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: 2 additions & 2 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ linux_arm64_task:
container:
image: php:$VERSION
pre_req_script:
- apt update --yes && apt install --yes zip unzip git libffi-dev
- apt update --yes && apt install --yes zip unzip git libffi-dev shared-mime-info
tienvx marked this conversation as resolved.
Show resolved Hide resolved
- curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
- php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
- docker-php-ext-install sockets
Expand All @@ -46,7 +46,7 @@ macos_arm64_task:
macos_instance:
image: ghcr.io/cirruslabs/macos-ventura-base:latest
pre_req_script:
- brew install php@$VERSION composer
- brew install php@$VERSION composer shared-mime-info
version_check_script:
- php --version
<< : *BUILD_TEST_TASK_TEMPLATE
39 changes: 20 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,22 @@
"autoload-dev": {
"psr-4": {
"PhpPactTest\\": "tests/PhpPact",
"Consumer\\": [
"example/src/Consumer",
"example/tests/Consumer"
],
"MessageConsumer\\": [
"example/src/MessageConsumer",
"example/tests/MessageConsumer"
],
"MessageProvider\\": [
"example/src/MessageProvider",
"example/tests/MessageProvider"
],
"Provider\\": [
"example/src/Provider"
]
"JsonConsumer\\": "example/json/consumer/src",
"JsonConsumer\\Tests\\": "example/json/consumer/tests",
"JsonProvider\\": "example/json/provider/src",
"JsonProvider\\Tests\\": "example/json/provider/tests",
"MessageConsumer\\": "example/message/consumer/src",
"MessageConsumer\\Tests\\": "example/message/consumer/tests",
"MessageProvider\\": "example/message/provider/src",
"MessageProvider\\Tests\\": "example/message/provider/tests",
"BinaryConsumer\\": "example/binary/consumer/src",
"BinaryConsumer\\Tests\\": "example/binary/consumer/tests",
"BinaryProvider\\": "example/binary/provider/src",
"BinaryProvider\\Tests\\": "example/binary/provider/tests",
"MultipartConsumer\\": "example/multipart/consumer/src",
"MultipartConsumer\\Tests\\": "example/multipart/consumer/tests",
"MultipartProvider\\": "example/multipart/provider/src",
"MultipartProvider\\Tests\\": "example/multipart/provider/tests"
}
},
"scripts": {
Expand All @@ -65,19 +66,19 @@
"lint": "php-cs-fixer fix --dry-run",
"fix": "php-cs-fixer fix",
"test": [
"php -r \"array_map('unlink', glob('./example/output/*.json'));\"",
"phpunit --debug -c example/phpunit.all.xml"
"php -r \"array_map('unlink', glob('./example/*/pacts/*.json'));\"",
"phpunit --debug"
]
},
"extra": {
"downloads": {
"pact-ffi-headers": {
"version": "0.4.7",
"version": "0.4.8",
"url": "https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v{$version}/pact.h",
"path": "bin/pact-ffi-headers/pact.h"
},
"pact-ffi-lib": {
"version": "0.4.7",
"version": "0.4.8",
"variables": {
"{$prefix}": "PHP_OS_FAMILY === 'Windows' ? 'pact_ffi' : 'libpact_ffi'",
"{$os}": "PHP_OS === 'Darwin' ? 'osx' : strtolower(PHP_OS_FAMILY)",
Expand Down
24 changes: 0 additions & 24 deletions example/README.md

This file was deleted.

11 changes: 11 additions & 0 deletions example/binary/consumer/phpunit.xml
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>
31 changes: 31 additions & 0 deletions example/binary/consumer/src/Service/HttpClientService.php
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 example/binary/consumer/tests/Service/HttpClientServiceTest.php
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);
}
}
Binary file added example/binary/consumer/tests/_resource/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions example/binary/pacts/binaryConsumer-binaryProvider.json
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"
}
}
11 changes: 11 additions & 0 deletions example/binary/provider/phpunit.xml
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>
Binary file added example/binary/provider/public/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions example/binary/provider/tests/PactVerifyTest.php
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);
}
}
11 changes: 11 additions & 0 deletions example/json/consumer/phpunit.xml
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>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Consumer\Service;
namespace JsonConsumer\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

namespace Consumer\Service;
namespace JsonConsumer\Tests\Service;

use JsonConsumer\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Standalone\Exception\MissingEnvVariableException;
use PhpPact\Standalone\MockService\MockServerEnvConfig;
use PhpPact\Standalone\MockService\MockServerConfig;
use PHPUnit\Framework\TestCase;

class ConsumerServiceGoodbyeTest extends TestCase
{
/**
* @throws MissingEnvVariableException
*/
public function testGetGoodbyeString()
{
$request = new ConsumerRequest();
Expand All @@ -30,8 +27,15 @@ public function testGetGoodbyeString()
'message' => 'Goodbye, Bob'
]);

$config = new MockServerEnvConfig();
$builder = new InteractionBuilder($config);
$config = new MockServerConfig();
$config
->setConsumer('jsonConsumer')
->setProvider('jsonProvider')
->setPactDir(__DIR__.'/../../../pacts');
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($logLevel);
}
$builder = new InteractionBuilder($config);
$builder
->given('Get Goodbye')
->uponReceiving('A get request to /goodbye/{name}')
Expand Down
Loading