Skip to content

Commit

Permalink
Use Rust FFI: Remove pact broker
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed May 10, 2023
1 parent d23599b commit c1a74f5
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 870 deletions.
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ Table of contents
- [Specifications](#specifications)
- [Installation](#installation)
- [Basic Consumer Usage](#basic-consumer-usage)
- [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)
- [Build the Interaction](#build-the-interaction)
- [Make the Request](#make-the-request)
- [Verify Interactions](#verify-interactions)
- [Make Assertions](#make-assertions)
- [Delete Old Pact](#delete-old-pact)
- [Publish Contracts To Pact Broker](#publish-contracts-to-pact-broker)
- [CLI](#cli)
- [Github Actions](#github-actions)
- [Basic Provider Usage](#basic-provider-usage)
- [Create Unit Test](#create-unit-test)
- [Start API](#start-api)
Expand Down Expand Up @@ -81,12 +84,6 @@ Composer hosts older versions under `mattersight/phppact`, which is abandoned. P

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

### Publish Contracts To Pact Broker

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.

### Create Consumer Unit Test

Create a standard PHPUnit test case class and function.
Expand Down Expand Up @@ -203,6 +200,32 @@ Verify that the data you would expect given the response configured is correct.
$this->assertEquals('Hello, Bob', $result); // Make your assertions.
```

### Delete Old Pact

If the value of `PACT_FILE_WRITE_MODE` is `merge`, before running the test, we need to delete the old pact manually:

```shell
rm /path/to/pacts/consumer-provider.json
```

### Publish Contracts To Pact Broker

When all tests in test suite are passed, you may want to publish generated contract files to pact broker.

#### CLI

Run this command using CLI tool:

```shell
pact-broker publish /path/to/pacts/consumer-provider.json --consumer-app-version 1.0.0 --branch main --broker-base-url https://test.pactflow.io --broker-token SomeToken
```

See more at https://docs.pact.io/pact_broker/publishing_and_retrieving_pacts#publish-using-cli-tools

#### Github Actions

See how to use at https://github.com/pactflow/actions/tree/main/publish-pact-files

## Basic Provider Usage

All of the following code will be used exclusively for Providers. This will run the Pacts against the real Provider and either verify or fail validation on the Pact Broker.
Expand Down
13 changes: 13 additions & 0 deletions UPGRADE-9.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,16 @@ UPGRADE FROM 8.x to 9.0

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

* Consumer
* Pact file write mode has been changed from 'overwrite' to 'merge'. Make sure old pact files are removed before running tests.

```shell
rm /path/to/pacts/*.json
```

* Pact files now can ONLY be uploaded to Pact Broker by downloading and running Pact CLI manually.

```shell
pact-broker publish /path/to/pacts/*.json --consumer-app-version 1.0.0 --branch main --broker-base-url https://test.pactflow.io --broker-token SomeToken
```
24 changes: 6 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"composer/semver": "^1.4.0|^3.2.0",
"symfony/process": "^4.4|^5.4|^6.0",
"guzzlehttp/guzzle": "^6.5.8|^7.4.5",
"phpunit/phpunit": ">=8.5.23 <10",
"tienvx/composer-downloads-plugin": "^1.2.0"
},
"require-dev": {
Expand All @@ -32,7 +31,8 @@
"slim/psr7": "^1.2.0",
"friendsofphp/php-cs-fixer": "^3.0",
"php-amqplib/php-amqplib": "^3.0",
"phpstan/phpstan": "^1.9"
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": ">=8.5.23 <10"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -64,25 +64,13 @@
"static-code-analysis": "phpstan",
"lint": "php-cs-fixer fix --dry-run",
"fix": "php-cs-fixer fix",
"test": "phpunit --debug -c example/phpunit.all.xml"
"test": [
"php -r \"array_map('unlink', glob('./example/output/*.json'));\"",
"phpunit --debug -c example/phpunit.all.xml"
]
},
"extra": {
"downloads": {
"pact-ruby-standalone": {
"version": "1.91.0",
"variables": {
"{$os}": "PHP_OS_FAMILY === 'Windows' ? 'win32' : (PHP_OS === 'Darwin' ? 'osx' : 'linux')",
"{$architecture}": "PHP_OS === 'Linux' ? '-x86_64' : ''",
"{$extension}": "PHP_OS_FAMILY === 'Windows' ? 'zip' : 'tar.gz'",
"{$keep}": "PHP_OS_FAMILY === 'Windows' ? 'pact-broker.bat' : 'pact-broker'"
},
"url": "https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v{$version}/pact-{$version}-{$os}{$architecture}.{$extension}",
"path": "bin/pact-ruby-standalone",
"ignore": [
"bin/*",
"!bin/{$keep}"
]
},
"pact-ffi-headers": {
"version": "0.4.4",
"url": "https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v{$version}/pact.h",
Expand Down
13 changes: 0 additions & 13 deletions example/phpunit.all.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,10 @@
<directory>./tests/MessageConsumer</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="PhpPact\Consumer\Listener\PactTestListener">
<arguments>
<array>
<element key="0">
<string>PhpPact Consumer Example Tests</string>
</element>
</array>
</arguments>
</listener>
</listeners>
<php>
<env name="PACT_MOCK_SERVER_HOST" value="localhost"/>
<env name="PACT_MOCK_SERVER_PORT" value="7200"/>
<env name="PACT_CONSUMER_NAME" value="someConsumer"/>
<env name="PACT_CONSUMER_VERSION" value="1.0.0"/>
<env name="PACT_CONSUMER_TAG" value="master"/>
<env name="PACT_PROVIDER_NAME" value="someProvider"/>
<env name="PACT_OUTPUT_DIR" value=".\example\output\\"/>
</php>
Expand Down
14 changes: 0 additions & 14 deletions example/phpunit.consumer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,11 @@
<directory>./tests/Consumer</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="PhpPact\Consumer\Listener\PactTestListener">
<arguments>
<array>
<element key="0">
<string>PhpPact Example Tests</string>
</element>
</array>
</arguments>
</listener>
</listeners>
<php>
<env name="PACT_MOCK_SERVER_HOST" value="localhost"/>
<env name="PACT_MOCK_SERVER_PORT" value="7200"/>
<env name="PACT_CONSUMER_NAME" value="someConsumer"/>
<env name="PACT_CONSUMER_VERSION" value="1.0.0"/>
<env name="PACT_CONSUMER_TAG" value="master"/>
<env name="PACT_PROVIDER_NAME" value="someProvider"/>
<env name="PACT_OUTPUT_DIR" value=".\example\output"/>
<!-- <env name="PACT_BROKER_URI" value="http://localhost"/> -->
</php>
</phpunit>
13 changes: 0 additions & 13 deletions example/phpunit.core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,10 @@
<directory>../tests</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="PhpPact\Consumer\Listener\PactTestListener">
<arguments>
<array>
<element key="0">
<string>PhpPact Consumer Example Tests</string>
</element>
</array>
</arguments>
</listener>
</listeners>
<php>
<env name="PACT_MOCK_SERVER_HOST" value="localhost"/>
<env name="PACT_MOCK_SERVER_PORT" value="7200"/>
<env name="PACT_CONSUMER_NAME" value="someConsumer"/>
<env name="PACT_CONSUMER_VERSION" value="1.0.0"/>
<env name="PACT_CONSUMER_TAG" value="master"/>
<env name="PACT_PROVIDER_NAME" value="someProvider"/>
<env name="PACT_OUTPUT_DIR" value=".\example\output\\"/>
</php>
Expand Down
6 changes: 0 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@ parameters:
level: 7
paths:
- src
ignoreErrors:
-
messages:
- '#Method PhpPact\\Standalone\\Broker\\Broker::[a-zA-Z0-9\\_]+\(\) return type has no value type specified in iterable type array\.#'
- '#Method PhpPact\\Standalone\\Broker\\Broker::[a-zA-Z0-9\\_]+\(\) has parameter \$options with no value type specified in iterable type array\.#'
path: src/PhpPact/Standalone/Broker/Broker.php
3 changes: 0 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
<env name="PACT_MOCK_SERVER_HOST" value="localhost"/>
<env name="PACT_MOCK_SERVER_PORT" value="7200"/>
<env name="PACT_CONSUMER_NAME" value="someConsumer"/>
<env name="PACT_CONSUMER_VERSION" value="1.0.0"/>
<env name="PACT_PROVIDER_NAME" value="someProvider"/>
<env name="PACT_BROKER_URI" value="http://localhost"/>
<env name="PACT_LOGLEVEL" value="DEBUG"/>
<!-- <env name="PACT_BROKER_BEARER_TOKEN" value="someToken"/> -->
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/PhpPact/Config/PactConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PactConfig implements PactConfigInterface
* pact file is deleted before running tests when using this option so that
* interactions deleted from the code are not maintained in the file.
*/
private string $pactFileWriteMode = self::MODE_OVERWRITE;
private string $pactFileWriteMode = self::MODE_MERGE;

/**
* {@inheritdoc}
Expand Down
101 changes: 0 additions & 101 deletions src/PhpPact/Consumer/Listener/PactTestListener.php

This file was deleted.

Loading

0 comments on commit c1a74f5

Please sign in to comment.