Define a pact between service consumers and providers, enabling "consumer driven contract" testing.
-
Install pact mock service:
-
Install Ruby and Ruby Gems
-
Write a Gemfile:
source 'https://rubygems.org' gem 'pact-mock_service', '~> 0.7.0'
-
Run bundler to get the gems:
gem install bundler && bundle install
-
-
Install composer:
curl -sS https://getcomposer.org/installer | php
-
Install dependencies:
php composer.phar install
-
Write a phpunit test similar to the following:
public function testMyProvider() { $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()); }); }
-
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
-
Run phpunit:
./vendor/bin/phpunit
-
Inspect the pact file in
tmp/pacts