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 #6.2: Move endpoint config #296

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
b45e9c0
Use Rust FFI: Consumer
tienvx Dec 23, 2022
849efd5
Merge branch 'master' into use-rust-ffi-consumer
tienvx Jan 11, 2023
e1d49cc
Remove method BuilderInterface::writePact
tienvx Jan 11, 2023
0d7fe0a
Remove CHANGELOG.md. Add '@internal' to classes are not supposed to b…
tienvx Jan 15, 2023
0812b31
Add missing return type to method Matcher::regex
tienvx Jan 15, 2023
cf5144b
Add more details to UPGRADE-9.0.md
tienvx Jan 15, 2023
d20f1f5
Revert multiple values support in header and query parameter
tienvx Jan 16, 2023
7d57b25
Merge branch 'master' into use-rust-ffi-consumer
Feb 14, 2023
bb256ef
Support multiple values in header and query parameter
tienvx Mar 1, 2023
d6a9d0d
Update pact ffi library
tienvx Mar 1, 2023
ca46297
Rename method getCode to getHeader
tienvx Mar 2, 2023
39718b9
Revert breaking change that require to call createMockServer manually
tienvx Mar 12, 2023
ddc46e5
Remove composer/semver
tienvx Mar 2, 2023
5dae03a
Make sure every value of header and query parameter is string
tienvx Mar 14, 2023
52e9a99
Move exception message to exception
tienvx Mar 14, 2023
bff0f9b
Extract method getSpecification() to check for supporting plugin
tienvx Mar 14, 2023
312c45c
Extract, change visibility, and move methods to abstract class to sup…
tienvx Mar 14, 2023
e7675e6
Use Rust FFI: Support multiple provider states with params for intera…
tienvx Mar 7, 2023
1e0b409
Fix provider state without params not added
tienvx Mar 16, 2023
47b0a81
Remove jsonSerialize methods
tienvx Jan 5, 2023
2754f47
Rename pact to driver
tienvx Mar 17, 2023
18b85b2
Move log level config to trait for reusing
tienvx Mar 23, 2023
5a4b15f
Allow mock server write pact file
tienvx Mar 23, 2023
072cc93
Merge branch 'master' into use-rust-ffi-consumer
tienvx May 3, 2023
29b33ec
Revert removing semver
tienvx May 3, 2023
a0302bc
Extract services and helpers
tienvx May 4, 2023
7fbc58d
Make PR smaller
tienvx May 4, 2023
ff2120f
Extract drivers and factories
tienvx May 4, 2023
420f4c2
Extract set up method
tienvx May 5, 2023
7f472fe
Simplify default interaction registry factory
tienvx May 5, 2023
c87dd62
Move methods from AbstractDriver to InteractionDriver and rename
tienvx May 5, 2023
53cef3d
Group assertions together for better readability
tienvx May 5, 2023
d5a4b83
Remove condition to make code shorter
tienvx May 5, 2023
6cf8128
Update ffi library to 0.4.4
tienvx May 5, 2023
156f989
Remove not useful annotation
tienvx May 5, 2023
bd8b751
Rename ffi service and move it to FFI namespace
tienvx May 6, 2023
d41e1ac
Assign result to variable before asserting
tienvx May 8, 2023
cc7f14c
Change type hint from 'mixed' to 'string|array|null'
tienvx May 8, 2023
dfaa021
Add @throws annotation to setPactFileWriteMode() method declaration b…
tienvx May 8, 2023
693daad
Rename FFI Proxy to Client
tienvx May 8, 2023
d960221
Extract interaction part drivers
tienvx May 8, 2023
770e6e0
Extract interaction contents drivers
tienvx May 9, 2023
29f7b4c
Extract interaction part traits for reusing
tienvx May 9, 2023
768b772
Allow sub-class access properties
tienvx May 9, 2023
a96c56a
Move common code back to MockServer for reusing. Remove methods from …
tienvx May 9, 2023
264018a
Move code from registry to driver
tienvx May 9, 2023
3a3afda
Switch term 'Driver' <--> 'Registry'
tienvx May 10, 2023
02b7fc3
Merge branch 'master' into use-rust-ffi-consumer
tienvx May 11, 2023
1d6d39e
Inject driver factory into builder
tienvx May 12, 2023
f844be0
Revert namespace change
tienvx May 12, 2023
d5d14e8
Use Rust FFI: Message Consumer
tienvx Dec 28, 2022
6aad456
Use Rust FFI: Provider
tienvx Jan 2, 2023
e96453f
Use Rust FFI: Stub Server
tienvx Jan 3, 2023
cd49913
Move endpoint from config to parameter
tienvx Mar 6, 2023
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
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- uses: ramsey/composer-install@v2
with:
Expand All @@ -48,6 +49,7 @@ jobs:
operating-system: [ ubuntu-latest, macos-latest, windows-latest ]
php: [ '8.0', '8.1', '8.2' ]
dependencies: [ 'lowest', 'locked' ]
timeout-minutes: 5

name: PHP ${{ matrix.php }} on ${{ matrix.operating-system }} with ${{ matrix.dependencies }} dependencies

Expand All @@ -58,8 +60,9 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
extensions: openssl, sockets, curl, zip
extensions: openssl, sockets, curl, zip, ffi
php-version: ${{ matrix.php }}
coverage: none

- name: Composer install
uses: ramsey/composer-install@v2
Expand Down
3 changes: 2 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
->name('*.php');

$config = new PhpCsFixer\Config();
$config->setRules(['@PSR12' => true,
$config->setRules([
'@PSR12' => true,
'strict_param' => false,
'array_syntax' => ['syntax' => 'short'],
])
Expand Down
223 changes: 87 additions & 136 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Table of contents
- [Specifications](#specifications)
- [Installation](#installation)
- [Basic Consumer Usage](#basic-consumer-usage)
- [Start and Stop the Mock Server](#start-and-stop-the-mock-server)
- [Publish Contracts To Pact Broker](#publish-contracts-to-pact-broker)
- [Create Consumer Unit Test](#create-consumer-unit-test)
- [Create Mock Request](#create-mock-request)
- [Create Mock Response](#create-mock-response)
Expand All @@ -30,7 +30,7 @@ Table of contents
- [Start API](#start-api)
- [Provider Verification](#provider-verification)
- [Verify From Pact Broker](#verify-from-pact-broker)
- [Verify All from Pact Broker](#verify-all-from-pact-broker)
- [Verify Files in Directory](#verify-files-in-directory)
- [Verify Files by Path](#verify-files-by-path)
- [Tips](#tips)
- [Starting API Asynchronously](#starting-api-asynchronously)
Expand All @@ -42,7 +42,7 @@ Table of contents
- [Usage for the optional `pact-stub-service`](#usage-for-the-optional-pact-stub-service)

## Versions
9.X updates internal dependencies and libraries. This results in dropping PHP 7.4
9.X updates internal dependencies and libraries + adds support for pact specification 3.X & 4.X via Pact FFI. This results in dropping PHP 7.4

8.X updates internal dependencies and libraries. This results in dropping PHP 7.3

Expand All @@ -59,7 +59,13 @@ If you wish to stick with the 2.X implementation, you can continue to pull from

## Specifications

The 3.X version is the version of Pact-PHP, not the pact specification version that it supports. Pact-Php 3.X supports [Pact-Specification 2.X](https://github.com/pact-foundation/pact-specification/tree/version-2).
The 3.X version is the version of Pact-PHP, not the pact specification version that it supports.

Pact-Php 3.X -> 8.X supports [Pact-Specification 2.X](https://github.com/pact-foundation/pact-specification/tree/version-2).
Pact-Php 9.X supports:
* [Pact-Specification 2.X](https://github.com/pact-foundation/pact-specification/tree/version-2)
* [Pact-Specification 3.X](https://github.com/pact-foundation/pact-specification/tree/version-3).
* [Pact-Specification 4.X](https://github.com/pact-foundation/pact-specification/tree/version-4).

## Installation

Expand All @@ -75,39 +81,12 @@ Composer hosts older versions under `mattersight/phppact`, which is abandoned. P

All of the following code will be used exclusively for the Consumer.

### Start and Stop the Mock Server
### Publish Contracts To Pact Broker

This library contains a wrapper for the [Ruby Standalone Mock Service](https://github.com/pact-foundation/pact-mock_service).
When all tests in test suite are passed, you may want to publish generated contract files to pact broker automatically.

The easiest way to configure this is to use a [PHPUnit Listener](https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.test-listeners). A default listener is included in this project, see [PactTestListener.php](/src/PhpPact/Consumer/Listener/PactTestListener.php). This utilizes environmental variables for configurations. These env variables can either be added to the system or to the phpunit.xml configuration file. Here is an example [phpunit.xml](/example/phpunit.consumer.xml) file configured to use the default. Keep in mind that both the test suite and the arguments array must be the same value.

Alternatively, you can start and stop as in whatever means you would like by following this example:

```php
<?php
use PhpPact\Standalone\MockService\MockServer;
use PhpPact\Standalone\MockService\MockServerConfig;

// Create your basic configuration. The host and port will need to match
// whatever your Http Service will be using to access the providers data.
$config = new MockServerConfig();
$config->setHost('localhost');
$config->setPort(7200);
$config->setConsumer('someConsumer');
$config->setProvider('someProvider');
$config->setCors(true);

// Instantiate the mock server object with the config. This can be any
// instance of MockServerConfigInterface.
$server = new MockServer($config);

// Create the process.
$server->start();

// Stop the process.
$server->stop();
```

### Create Consumer Unit Test

Create a standard PHPUnit test case class and function.
Expand Down Expand Up @@ -193,10 +172,10 @@ Now that we have the request and response, we need to build the interaction and
$config = new MockServerEnvConfig();
$builder = new InteractionBuilder($config);
$builder
->given('a person exists')
->given('a person exists', ['name' => 'Bob'])
->uponReceiving('a get request to /hello/{name}')
->with($request)
->willRespondWith($response); // This has to be last. This is what makes an API request to the Mock Server to set the interaction.
->willRespondWith($response); // This has to be last. This is what makes FFI calls to register the interaction and start the mock server.
```

### Make the Request
Expand All @@ -212,7 +191,8 @@ Verify that all interactions took place that were registered.
This typically should be in each test, that way the test that failed to verify is marked correctly.

```php
$builder->verify();
$verifyResult = $verifier->verify();
$this->assertTrue($verifyResult);
```

### Make Assertions
Expand Down Expand Up @@ -250,51 +230,62 @@ $config = new VerifierConfig();
$config
->setProviderName('someProvider') // Providers name to fetch.
->setProviderVersion('1.0.0') // Providers version.
->setProviderBranch('main') // Providers git branch name.
->setProviderBaseUrl(new Uri('http://localhost:58000')) // URL of the Provider.
->setBrokerUri(new Uri('http://localhost')) // URL of the Pact Broker to publish results.
->setPublishResults(true) // Flag the verifier service to publish the results to the Pact Broker.
->setProcessTimeout(60) // Set process timeout (optional) - default 60
->setProcessIdleTimeout(10) // Set process idle timeout (optional) - default 10
->setEnablePending(true) // Flag to enable pending pacts feature (check pact docs for further info)
->setIncludeWipPactSince('2020-01-30') //Start date of WIP Pacts (check pact docs for further info)
->setRequestFilter(
function (RequestInterface $r) {
return $r->withHeader('MY_SPECIAL_HEADER', 'my special value');
}
);
// Verify that the Consumer 'someConsumer' that is tagged with 'master' is valid.
->setProviderTags('prod' ,'dev')
->setProviderBranch('main')
->setScheme('http')
->setHost('localhost')
->setPort(58000)
->setBasePath('/')
->setStateChangeUrl(new Uri('http://localhost:58000/change-state'))
->setBuildUrl(new Uri('http://build.domain.com'))
->setFilterConsumerNames('someConsumer', 'otherConsumer')
->setFilterDescription('Send POST to create')
->setFilterNoState(true)
->setFilterState('state')
->setPublishResults(true)
->setDisableSslVerification(true)
->setStateChangeAsBody(false)
->setStateChangeTeardown(true)
->setRequestTimeout(500);

$verifier = new Verifier($config);
$verifier->verify('someConsumer', 'master'); // The tag is option. If no tag is set it will just grab the latest.

// This will not be reached if the PACT verifier throws an error, otherwise it was successful.
$this->assertTrue(true, 'Pact Verification has failed.');
$selectors = (new ConsumerVersionSelectors())
->addSelector('{"tag":"foo","latest":true}')
->addSelector('{"tag":"bar","latest":true}');

$broker = new Broker();
$broker
->setUrl(new Uri('http://localhost'))
->setUsername('user')
->setPassword('pass')
->setToken('token')
->setEnablePending(true)
->setIncludeWipPactSince('2020-01-30')
->setProviderTags(['prod'])
->setProviderBranch('main')
->setConsumerVersionSelectors($selectors)
->setConsumerVersionTags(['dev']);

$verifier->addBroker($broker);

$verifyResult = $verifier->verify();

$this->assertTrue($verifyResult);
```

##### Verify All from Pact Broker
##### Verify Files in Directory

This will grab every Pact file associated with the given provider.
This allows local Pact file testing.

```php
public function testPactVerifyAll()
public function testPactVerifyFilesInDirectory()
{
$config = new VerifierConfig();
$config
->setProviderName('someProvider') // Providers name to fetch.
->setProviderVersion('1.0.0') // Providers version.
->setProviderBranch('main') // Providers git branch name.
->setProviderBaseUrl(new Uri('http://localhost:58000')) // URL of the Provider.
->setBrokerUri(new Uri('http://localhost')) // URL of the Pact Broker to publish results.
->setPublishResults(true) // Flag the verifier service to publish the results to the Pact Broker.
->setEnablePending(true) // Flag to enable pending pacts feature (check pact docs for further info)
->setIncludeWipPactSince('2020-01-30') //Start date of WIP Pacts (check pact docs for further info)

// Verify that all consumers of 'someProvider' are valid.
$verifier = new Verifier($config);
$verifier->verifyAll();

// This will not be reached if the PACT verifier throws an error, otherwise it was successful.
$this->assertTrue(true, 'Pact Verification has failed.');
$verifier->addDirectory('C:\SomePath');

$verifyResult = $verifier->verify();

$this->assertTrue($verifyResult);
}
```

Expand All @@ -303,25 +294,13 @@ public function testPactVerifyAll()
This allows local Pact file testing.

```php
public function testPactVerifyAll()
public function testPactVerifyFiles()
{
$config = new VerifierConfig();
$config
->setProviderName('someProvider') // Providers name to fetch.
->setProviderVersion('1.0.0') // Providers version.
->setProviderBranch('main') // Providers git branch name.
->setProviderBaseUrl(new Uri('http://localhost:58000')) // URL of the Provider.
->setBrokerUri(new Uri('http://localhost')) // URL of the Pact Broker to publish results.
->setPublishResults(true); // Flag the verifier service to publish the results to the Pact Broker.
->setEnablePending(true) // Flag to enable pending pacts feature (check pact docs for further info)
->setIncludeWipPactSince('2020-01-30') //Start date of WIP Pacts (check pact docs for further info)

// Verify that the files in the array are valid.
$verifier = new Verifier($config);
$verifier->verifyFiles(['C:\SomePath\consumer-provider.json']);

// This will not be reached if the PACT verifier throws an error, otherwise it was successful.
$this->assertTrue(true, 'Pact Verification has failed.');
$verifier->addFile('C:\SomePath\consumer-provider.json');

$verifyResult = $verifier->verify();

$this->assertTrue($verifyResult);
}
```

Expand Down Expand Up @@ -353,7 +332,6 @@ There is a separate repository with an end to end example for both the 2.X and 3
- [2.2.1 tag](https://github.com/mattermack/pact-php-example/tree/2.2.1) for 2.X examples

## Message support
This feature is preliminary as the Pact community as a whole is flushing this out.
The goal is not to test the transmission of an object over a bus but instead vet the contents of the message.
While examples included focus on a Rabbit MQ, the exact message queue is irrelevant. Initial comparisons require a certain
object type to be created by the Publisher/Producer and the Consumer of the message. This includes a metadata set where you
Expand Down Expand Up @@ -393,68 +371,41 @@ $consumerMessage = new ExampleMessageConsumer();
$callback = [$consumerMessage, 'ProcessSong'];
$builder->setCallback($callback);

$builder->verify();
$verifyResult = $builder->verify();

$this->assertTrue($verifyResult);
```


### Provider Side Message Validation
This may evolve as we work through this implementation. The provider relies heavily on callbacks.
Some of the complexity lies in a consumer and provider having many messages and states between the each other in a single pact.

For each message, one needs to provide a single provider state. The name of this provider state must be the key to run
a particular message callback on the provider side. See example\tests\MessageProvider

1. Create your callbacks and states wrapped in a callable object
1. The array key is a provider state / given() on the consumer side
1. It is helpful to wrap the whole thing in a lambda if you need to customize paramaters to be passed in
1. Choose your verification method
1. If nothing explodes, #winning

```php
Handle these requests on your provider:

$callbacks = array();
1. POST /pact-change-state
1. Set up your database to meet the expectations of the request
2. Reset the database to its original state.
2. POST /pact-messages
1. Return message's content in body
2. Return message's metadata in header `PACT-MESSAGE-METADATA`

// a hello message is a provider state / given() on the consumer side
$callbacks["a hello message"] = function() {
$content = new \stdClass();
$content->text ="Hello Mary";

$metadata = array();
$metadata['queue'] = "myKey";

$provider = (new ExampleMessageProvider())
->setContents($content)
->setMetadata($metadata);

return $provider->Build();
};

$verifier = (new MessageVerifier($config))
->setCallbacks($callbacks)
->verifyFiles([__DIR__ . '/../../output/test_consumer-test_provider.json']);

```
[Click here](/example/src/Provider/public/index.php) to see the full sample file.

## Usage for the optional `pact-stub-service`

If you would like to test with fixtures, you can use the `pact-stub-service` like this:

```php
$pactLocation = __DIR__ . '/someconsumer-someprovider.json';
$host = 'localhost';
$port = 7201;
$endpoint = 'test';
$files = [__DIR__ . '/someconsumer-someprovider.json'];
$port = 7201;
$endpoint = 'test';

$config = (new StubServerConfig())
->setPactLocation($pactLocation)
->setHost($host)
->setPort($port)
->setEndpoint($endpoint);
->setFiles($files)
->setPort($port);

$stubServer = new StubServer($config);
$stubServer->start();

$service = new StubServerHttpService(new GuzzleClient(), $config);

echo $service->getJson(); // output: {"results":[{"name":"Games"}]}
echo $service->getJson($endpoint); // output: {"results":[{"name":"Games"}]}
```
Loading