pact/pact

Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.

Maintainers

Package info

github.com/mopoke/pact-php

Issues

pkg:composer/pact/pact

Statistics

Installs: 34 667

Dependents: 0

Suggesters: 0

Stars: 8

0.2.1 2015-09-21 19:12 UTC

This package is not auto-updated.

Last update: 2026-04-26 02:17:59 UTC


README

Define a pact between service consumers and providers, enabling "consumer driven contract" testing.

CI

How to use

  1. Install pact mock service:

    1. Install Ruby and Ruby Gems

    2. Write a Gemfile:

      source 'https://rubygems.org'
      gem 'pact-mock_service', '~> 0.7.0'
    3. Run bundler to get the gems: gem install bundler && bundle install

  2. Install composer: https://getcomposer.org/download/

  3. Install dependencies: composer install

  4. Write a phpunit test similar to the following:

    use Pact\Pact;
    
    class MyProviderTest extends PHPUnit\Framework\TestCase
    {
        public function testMyProvider(): void
        {
            $client = new ZooClient('http://localhost:1234');
    
            $alligatorProvider = Pact::mockService([
                'consumer' => 'Alligator Consumer',
                'provider' => 'Alligator Provider',
                'port' => 1234
            ]);
    
            $alligatorProvider
                ->given("an alligator with the name Mary exists")
                ->uponReceiving("a request for an alligator")
                ->withRequest("get", "/alligators/Mary", [
                    "Accept" => "application/json"
                ])->willRespondWith(200, [
                    "Content-Type" => "application/json"
                ], [
                    "name" => "Mary"
                ]);
    
            $alligatorProvider->run(function () use ($client) {
                $alligator = $client->getAlligatorByName('Mary');
                $this->assertInstanceOf("Alligator", $alligator);
                $this->assertEquals("Mary", $alligator->getName());
            });
        }
    }
  5. Start the mock server: bundle exec pact-mock-service -p 1234 --pact-specification-version 2.0.0 -l log/pact.logs --pact-dir tmp/pacts

  6. Run phpunit: ./vendor/bin/phpunit

  7. Inspect the pact file in tmp/pacts