2017 © Pedro Pelรกez
 

library pact-php

Enables consumer driven contract testing, following the PACT foundation principles.

image

pact-foundation/pact-php

Enables consumer driven contract testing, following the PACT foundation principles.

  • Tuesday, July 31, 2018
  • by mattermack
  • Repository
  • 15 Watchers
  • 46 Stars
  • 205 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 15 Forks
  • 3 Open issues
  • 24 Versions
  • 0 % Grown

The README.md

, (*1)

logo, (*2)

Pact PHP

Code Analysis & Test Coverage Status Compatibility Suite Packagist, (*3)

Downloads Downloads This Month, (*4)

Fast, easy and reliable testing for your APIs and microservices.

, (*5)


, (*6)

Pact PHP Demo , (*7)


, (*8)

**Pact** is the de-facto API contract testing tool. Replace expensive and brittle end-to-end integration tests with fast, reliable and easy to debug unit tests. - โšก Lightning fast - ๐ŸŽˆ Effortless full-stack integration testing - from the front-end to the back-end - ๐Ÿ”Œ Supports HTTP/REST and event-driven systems - ๐Ÿ› ๏ธ Configurable mock server - ๐Ÿ˜Œ Powerful matching rules prevents brittle tests - ๐Ÿค Integrates with Pact Broker / PactFlow for powerful CI/CD workflows - ๐Ÿ”ก Supports 12+ languages **Why use Pact?** Contract testing with Pact lets you: - โšก Test locally - ๐Ÿš€ Deploy faster - โฌ‡๏ธ Reduce the lead time for change - ๐Ÿ’ฐ Reduce the cost of API integration testing - ๐Ÿ’ฅ Prevent breaking changes - ๐Ÿ”Ž Understand your system usage - ๐Ÿ“ƒ Document your APIs for free - ๐Ÿ—„ Remove the need for complex data fixtures - ๐Ÿคทโ€โ™‚๏ธ Reduce the reliance on complex test environments Watch our [series](https://www.youtube.com/playlist?list=PLwy9Bnco-IpfZ72VQ7hce8GicVZs7nm0i) on the problems with end-to-end integrated tests, and how contract testing can help.

----------, (*9)

Documentation

This readme offers a basic introduction to the library. The full documentation for Pact PHP and the rest of the framework is available at https://docs.pact.io/., (*10)

Need Help

  • Join our community slack workspace.
  • Stack Overflow: https://stackoverflow.com/questions/tagged/pact
  • Say ๐Ÿ‘‹ on Twitter: [@pact_up]

Installation

composer require pact-foundation/pact-php --dev

# ๐Ÿš€ now write some tests!

Looking for the previous stable 9.x.x release?, (*11)

Requirements

PHP 8.1+ as of pact-php v10, (*12)

Do Not Track

In order to get better statistics as to who is using Pact, we have an anonymous tracking event that triggers when Pact installs for the first time. The only things we track are your type of OS, and the version information for the package being installed. No PII data is sent as part of this request. You can disable tracking by setting the environment variable PACT_DO_NOT_TRACK=true:, (*13)

----------, (*14)

Usage

Writing a Consumer test

namespace App\Tests;

use App\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Matcher\Matcher;
use PhpPact\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Standalone\MockService\MockServerConfig;
use PHPUnit\Framework\TestCase;

class ConsumerServiceHelloTest extends TestCase
{
    public function testGetHelloString(): void
    {
        $matcher = new Matcher();

        // Create your expected request from the consumer.
        $request = new ConsumerRequest();
        $request
            ->setMethod('GET')
            ->setPath('/hello/Bob')
            ->addHeader('Content-Type', 'application/json');

        // Create your expected response from the provider.
        $response = new ProviderResponse();
        $response
            ->setStatus(200)
            ->addHeader('Content-Type', 'application/json')
            ->setBody([
                'message' => $matcher->term('Hello, Bob', '(Hello, )[A-Za-z]+')
            ]);

        // Create a configuration that reflects the server that was started. You can create a custom MockServerConfigInterface if needed.
        $config = new MockServerConfig();
        $config
            ->setConsumer('jsonConsumer')
            ->setProvider('jsonProvider')
            ->setPactDir(__DIR__.'/../../../pacts');
        if ($logLevel = \getenv('PACT_LOGLEVEL')) {
            $config->setLogLevel($logLevel);
        }
        $builder = new InteractionBuilder($config);
        $builder
            ->uponReceiving('A get request to /hello/{name}')
            ->with($request)
            ->willRespondWith($response); // This has to be last. This is what makes FFI calls to register the interaction and start the mock server.

        $service = new HttpClientService($config->getBaseUri()); // Pass in the URL to the Mock Server.
        $helloResult = $service->getHelloString('Bob'); // Make the real API request against the Mock Server.
        $verifyResult = $builder->verify(); // This will verify that the interactions took place.

        $this->assertTrue($verifyResult); // Make your assertions.
        $this->assertEquals('Hello, Bob', $helloResult);
    }
}

You can see (and run) the full version of this in ./examples/json, as well as other examples in the parent folder., (*15)

To run the examples, (*16)

  1. Clone the repo git@github.com:pact-foundation/pact-php.git
  2. Go to the repo cd pact-php
  3. Install all dependencies composer install

Run a single example, (*17)

composer run-example:json, (*18)

Run all examples, (*19)

composer run-examples, (*20)

----------, (*21)

Verifying a Provider

A provider test takes one or more pact files (contracts) as input, and Pact verifies that your provider adheres to the contract. In the simplest case, you can verify a provider as per below using a local pact file, although in practice you would usually use a Pact Broker to manage your contracts and CI/CD workflow., (*22)

namespace App\Tests;

use GuzzleHttp\Psr7\Uri;
use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig;
use PhpPact\Standalone\ProviderVerifier\Verifier;
use PhpPactTest\Helper\PhpProcess;
use PHPUnit\Framework\TestCase;

class PactVerifyTest extends TestCase
{
    private PhpProcess $process;

    protected function setUp(): void
    {
        $this->process = new PhpProcess(__DIR__ . '/path/to/public/');
        $this->process->start();
    }

    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('jsonProvider') // Providers name to fetch.
            ->setHost('localhost')
            ->setPort($this->process->getPort());
        $config->getProviderState()
            ->setStateChangeUrl(new Uri(sprintf('http://localhost:%d/pact-change-state', $this->process->getPort())))
        ;
        if ($level = \getenv('PACT_LOGLEVEL')) {
            $config->setLogLevel($level);
        }

        $verifier = new Verifier($config);
        $verifier->addFile(__DIR__ . '/path/to/pacts/jsonConsumer-jsonProvider.json');

        $verifyResult = $verifier->verify();

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

It's best to run Pact verification tests as part of your unit testing suite, so you can readily access stubbing, IaC and other helpful tools., (*23)

----------, (*24)

Compatibility

Versions
, (*25)

Version Status Spec Compatibility PHP Compatibility Install
10.x Stable 1, 1.1, 2, 3, 4 ^8.1 See installation
9.x Stable 1, 1.1, 2, 3* ^8.0 9xx
8.x Deprecated 1, 1.1, 2, 3* ^7.4\ ^8.0 |
7.x Deprecated 1, 1.1, 2, 3* ^7.3
6.x Deprecated 1, 1.1, 2, 3* ^7.2
5.x Deprecated 1, 1.1, 2, 3* ^7.1
4.x Deprecated 1, 1.1, 2 ^7.1
3.x Deprecated 1, 1.1, 2 ^7.0
2.x Deprecated 1, 1.1, 2 >=7
1.x Deprecated 1, 1.1 >=7

* v3 support is limited to the subset of functionality required to enable language inter-operable Message support., (*26)

, (*27)

Supported Platforms
, (*28)

OS Architecture Supported Pact-PHP Version
OSX x86_64 โœ… All
Linux x86_64 โœ… All
OSX arm64 โœ… 9.x +
Linux arm64 โœ… 9.x +
Windows x86_64 โœ… All
Windows x86 โœ… 9.x -
Alpine x86_64 โœ… All *
Alpine arm64 โœ… All *

* For 9.x and below, supported with a workaround Ruby Standalone with Alpine., (*29)

, (*30)

Roadmap

The roadmap for Pact and Pact PHP is outlined on our main website., (*31)

Contributing

See CONTRIBUTING., (*32)


, (*33)

The Versions

06/07 2018

dev-master

9999999-dev https://github.com/pact-foundation/pact-php

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact pact-php

06/07 2018

4.0.2

4.0.2.0 https://github.com/pact-foundation/pact-php

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact pact-php

28/06 2018

4.0.1

4.0.1.0 https://github.com/pact-foundation/pact-php

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact pact-php

27/06 2018

4.0.0

4.0.0.0 https://github.com/Mattersight/php-pact

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact mattersight pact-php

18/05 2018

3.1.1

3.1.1.0 https://github.com/Mattersight/php-pact

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact mattersight pact-php

08/05 2018

3.1.0

3.1.0.0 https://github.com/Mattersight/php-pact

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact mattersight pact-php

18/04 2018

3.0.3

3.0.3.0 https://github.com/Mattersight/php-pact

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact mattersight pact-php

05/03 2018

3.0.2

3.0.2.0 https://github.com/Mattersight/php-pact

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact mattersight pact-php

01/03 2018

3.0.1

3.0.1.0 https://github.com/Mattersight/php-pact

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact mattersight pact-php

28/02 2018

3.0.0

3.0.0.0 https://github.com/Mattersight/php-pact

Enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack
by Nicholas Brink

pact mattersight pact-php

28/10 2017

1.1.3

1.1.3.0 https://github.com/Mattersight/php-pact

Natively enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack

pact mattersight pact-php pact-php-native php-mock

06/10 2017

1.1.0

1.1.0.0 https://github.com/Mattersight/php-pact

Natively enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack

pact mattersight pact-php pact-php-native php-mock

25/07 2017

1.0.5

1.0.5.0 https://github.com/Mattersight/php-pact

Natively enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack

pact mattersight pact-php pact-php-native php-mock

21/07 2017

1.0.3

1.0.3.0 https://github.com/Mattersight/php-pact

Natively enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack

pact mattersight pact-php pact-php-native php-mock

21/07 2017

1.0.4

1.0.4.0 https://github.com/Mattersight/php-pact

Natively enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack

pact mattersight pact-php pact-php-native php-mock

17/07 2017

1.0.1

1.0.1.0 https://github.com/Mattersight/php-pact

Natively enables consumer driven contract testing, following the PACT foundation principles.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar mattermack

pact mattersight pact-php pact-php-native php-mock