SlideShare a Scribd company logo
@andykelk | #SydPHP
Consumer-driven
Contracts with Pact and
PHP
@andykelk | #SydPHP
provider
consumer 1
consumer 2
consumer 3
{
status: “ok”,
count: 20,
items: [
{
id: 10000,
name: ...
Providers and consumers
@andykelk | #SydPHP
Consumer-driven contracts
• In a service oriented architecture, a service provider
typically has many consumers.
• Each of those consumers has expectations about the
service.
• Over time, consumer expectations change and the
provider must evolve to meet them.
• Standard approaches to this introduce
interdependence into the relationship.
• The Consumer-Driven Contracts pattern solves that.
@andykelk | #SydPHP
Consumer-driven contracts
provider
consumer 1
consumer 2
consumer 3
{
status: “ok”,
count: 20,
items: [
{
id: 10000,
name: ...
@andykelk | #SydPHP
Pact
• Ruby gem which implements automated testing for
consumer-driven contracts
• There are also implementations for
• .Net (Provider, Consumer)
• JVM (Provider, Consumer)
• JavaScript (Provider, Consumer)
• Swift (Consumer)
@andykelk | #SydPHP
Pact
https://github.com/realestate-com-au/pact
@andykelk | #SydPHP
Pact
• BUT… no PHP implementation 😞
• Luckily we can use the Provider Proxy to help us test a
PHP service provider
@andykelk | #SydPHP
JavaScript Consumer
Jasmine
PhantomJS
PAC
T
Karma
Test framework/
Test runner
Browser
(headless)
Mock/contract
endpoint
App code
(HTTP client,
model)
Production code
exercise
s
invokes via
socket
creates
pact json file
@andykelk | #SydPHP
beforeEach(function() {
client = ZooClient.createClient('http://localhost:1234');
alligatorProvider = Pact.mockService({
consumer: 'Alligator Consumer',
provider: 'Alligator Provider',
port: 1234,
done: function (error) {
expect(error).toBe(null);
}
});
});
Setting up a JavaScript consumer
@andykelk | #SydPHP
Setting up a JavaScript consumer
it("should return an alligator", function(done) {
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(done, function(runComplete) {
expect(client.getAlligatorByName("Mary")).toEqual(new Alligator("Mary"));
runComplete();
});
});
@andykelk | #SydPHP
Now we have a pact
{
"consumer": { "name": "Alligator Consumer" },
"provider": { "name": "Alligator Provider" },
"interactions": [
{
"description": "a request for an alligator",
"provider_state": "an alligator with the name Mary exists",
"request": {
"method": "get",
"path": "/alligators/Mary",
"headers": { "Accept": "application/json" }
},
"response": {
"status": 200,
"headers": { "Content-Type": "application/json" },
"body": { "name": "Mary" }
}
}
],
"metadata": { "pactSpecificationVersion": "1.0.0" }
}
@andykelk | #SydPHP
PHP Provider
Rake
PAC
T
prox
y
Test framework/
Test runner
Mock/contract
endpoint
App code
(service
provider)
Production code
invokes
pact json file
reads
@andykelk | #SydPHP
Create the API provider
<?php
require 'vendor/autoload.php';
$app = new SlimSlim();
$app->get('/alligators/:name', function ($name) use ($app) {
$app->response->setStatus(200);
$app->response()->headers->set('Content-Type', 'application/json');
echo json_encode(array('name' => $name));
});
$app->run();
@andykelk | #SydPHP
Use rake and pact provider proxy to test
require 'pact/provider/proxy/tasks'
Pact::ProxyVerificationTask.new :alligator do | task |
task.pact_url './spec/pacts/alligator_consumer-alligator_provider.json',
:pact_helper => './spec/support/alligator_pact_helper'
task.provider_base_url 'http://localhost:8000'
end
Pact.provider_states_for "Alligator Consumer" do
provider_state "an alligator with the name Mary exists" do
set_up do
# Set-up the provider state (e.g. create fixture) here
end
end
end
@andykelk | #SydPHP
Demo Time!
@andykelk | #SydPHP
What’s next?
• PHP implementation of the pact specification
• Use pact broker to share pact files and provide:
• Auto-generated documentation
• Dynamically generated network diagrams
• The ability to tag a pact (ie. "prod") so a provider
can verify itself against a fixed version of a pact to
ensure backwards compatibility.
• Webhooks to trigger provider builds when a pact is
published.
@andykelk | #SydPHP
Questions?
• Further reading:
• http://martinfowler.com/articles/consumerDrivenContracts.html
• https://github.com/realestate-com-au/pact
• http://techblog.realestate.com.au/testing-interactions-with-web-services-
without-integration-tests-in-ruby/
• http://techblog.realestate.com.au/enter-the-pact-matrix-or-how-to-
decouple-the-release-cycles-of-your-microservices/
• Code from the demo:
• https://github.com/mopoke/pact-php-demo
• Relive this talk in blog form:
• http://www.andykelk.net/tech/consumer-driven-contracts-with-pact-and-
php

More Related Content

PDF
Microservices: Consumer Driven Contracts in Practice
PDF
Consumer Driven Contracts and Your Microservice Architecture
PPTX
Consumer-driven contracts: avoid microservices integration hell! (LondonCD - ...
PDF
Consumer contract testing
PDF
Contract testing and Pact
PPTX
Contract testing. Isolated testing of microservices with pact.io - Evgeniy Ku...
PPTX
Consumer-Driven Contract Testing PACT
PPTX
Consumer Driven Contracts for microservices
Microservices: Consumer Driven Contracts in Practice
Consumer Driven Contracts and Your Microservice Architecture
Consumer-driven contracts: avoid microservices integration hell! (LondonCD - ...
Consumer contract testing
Contract testing and Pact
Contract testing. Isolated testing of microservices with pact.io - Evgeniy Ku...
Consumer-Driven Contract Testing PACT
Consumer Driven Contracts for microservices

What's hot (20)

PDF
Consumer-Driven Contract Testing
PPTX
vodQA(Pune) 2018 - Consumer driven contract testing using pact
PDF
Consumer-Driven Contract Testing - Workshop - January 2021
PPTX
Collaborative Contract Driven Development
PDF
TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlin...
PPTX
Consumer Driven Contracts
PPT
Super slideshow 2
PDF
Microservices. Test smarter, not harder. Voxxed Days 2019
PDF
Fed London - January 2015
PDF
Contract testing | Евгений Кузьмин | CODEiD
PPTX
Design and Evolution of APIs in Microservice Architecture
PPTX
Lightening Talk: Auckland Continuous Delivery Meetup [Sept 2014] - Pact: Cons...
PDF
Agile Business Conference 2018: Procurement on Disruption by Mirko Kleiner
PDF
Contract Testing
PDF
API Design Collaboration
PDF
CI/CD non-breaking changes exercise - Cork Software Crafters - February 2020
PPTX
Gengo Jaws Days Tokyo 2014 Presentation
PDF
OpenAPI Spec at Google (Open API Initiative Meetup on 2016-09-15)
PDF
apidays LIVE Paris 2021 - Building an Accessible API Spec with Traditional En...
PDF
[API World 2021 ] - Understanding Cloud Native Deployment
Consumer-Driven Contract Testing
vodQA(Pune) 2018 - Consumer driven contract testing using pact
Consumer-Driven Contract Testing - Workshop - January 2021
Collaborative Contract Driven Development
TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlin...
Consumer Driven Contracts
Super slideshow 2
Microservices. Test smarter, not harder. Voxxed Days 2019
Fed London - January 2015
Contract testing | Евгений Кузьмин | CODEiD
Design and Evolution of APIs in Microservice Architecture
Lightening Talk: Auckland Continuous Delivery Meetup [Sept 2014] - Pact: Cons...
Agile Business Conference 2018: Procurement on Disruption by Mirko Kleiner
Contract Testing
API Design Collaboration
CI/CD non-breaking changes exercise - Cork Software Crafters - February 2020
Gengo Jaws Days Tokyo 2014 Presentation
OpenAPI Spec at Google (Open API Initiative Meetup on 2016-09-15)
apidays LIVE Paris 2021 - Building an Accessible API Spec with Traditional En...
[API World 2021 ] - Understanding Cloud Native Deployment
Ad

Similar to Consumer-driven contracts with Pact and PHP (20)

PPTX
Contract testing: Beyond API functional testing
PDF
Software contracts - Global Enterprise Agile 2023.pdf
PDF
CDC Tests - Integration Tests cant be made simpler than this!
PDF
Micro-service delivery - without the pitfalls
PDF
Move fast and consumer driven contract test things
PDF
Php and-web-services-24402
PDF
Consumer driven contracts
PPTX
Consumer Driven Contracts - A Deep Dive
PPTX
Contract testing - isolated testing of microservices - Symfony Camp 2018, Evg...
PDF
When symfony met promises
PDF
Json Rpc Proxy Generation With Php
PDF
Php and webservices
PPTX
Coherent REST API design
PDF
Contract testing symfony camp 2018
PDF
Php And Web Services
PDF
PHP and Web Services
PPTX
"Asynchronous" Integration Tests for Microservices - RootConf 2017
PDF
PHP is the King, nodejs is the Prince and Lua is the fool
ODP
Soap Toolkit Dcphp
PDF
The case for consumer-driven contracts
Contract testing: Beyond API functional testing
Software contracts - Global Enterprise Agile 2023.pdf
CDC Tests - Integration Tests cant be made simpler than this!
Micro-service delivery - without the pitfalls
Move fast and consumer driven contract test things
Php and-web-services-24402
Consumer driven contracts
Consumer Driven Contracts - A Deep Dive
Contract testing - isolated testing of microservices - Symfony Camp 2018, Evg...
When symfony met promises
Json Rpc Proxy Generation With Php
Php and webservices
Coherent REST API design
Contract testing symfony camp 2018
Php And Web Services
PHP and Web Services
"Asynchronous" Integration Tests for Microservices - RootConf 2017
PHP is the King, nodejs is the Prince and Lua is the fool
Soap Toolkit Dcphp
The case for consumer-driven contracts
Ad

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Cloud computing and distributed systems.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
Digital-Transformation-Roadmap-for-Companies.pptx
Empathic Computing: Creating Shared Understanding
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine Learning_overview_presentation.pptx
Cloud computing and distributed systems.
“AI and Expert System Decision Support & Business Intelligence Systems”
gpt5_lecture_notes_comprehensive_20250812015547.pdf
The AUB Centre for AI in Media Proposal.docx
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
Mobile App Security Testing_ A Comprehensive Guide.pdf

Consumer-driven contracts with Pact and PHP

  • 2. @andykelk | #SydPHP provider consumer 1 consumer 2 consumer 3 { status: “ok”, count: 20, items: [ { id: 10000, name: ... Providers and consumers
  • 3. @andykelk | #SydPHP Consumer-driven contracts • In a service oriented architecture, a service provider typically has many consumers. • Each of those consumers has expectations about the service. • Over time, consumer expectations change and the provider must evolve to meet them. • Standard approaches to this introduce interdependence into the relationship. • The Consumer-Driven Contracts pattern solves that.
  • 4. @andykelk | #SydPHP Consumer-driven contracts provider consumer 1 consumer 2 consumer 3 { status: “ok”, count: 20, items: [ { id: 10000, name: ...
  • 5. @andykelk | #SydPHP Pact • Ruby gem which implements automated testing for consumer-driven contracts • There are also implementations for • .Net (Provider, Consumer) • JVM (Provider, Consumer) • JavaScript (Provider, Consumer) • Swift (Consumer)
  • 7. @andykelk | #SydPHP Pact • BUT… no PHP implementation 😞 • Luckily we can use the Provider Proxy to help us test a PHP service provider
  • 8. @andykelk | #SydPHP JavaScript Consumer Jasmine PhantomJS PAC T Karma Test framework/ Test runner Browser (headless) Mock/contract endpoint App code (HTTP client, model) Production code exercise s invokes via socket creates pact json file
  • 9. @andykelk | #SydPHP beforeEach(function() { client = ZooClient.createClient('http://localhost:1234'); alligatorProvider = Pact.mockService({ consumer: 'Alligator Consumer', provider: 'Alligator Provider', port: 1234, done: function (error) { expect(error).toBe(null); } }); }); Setting up a JavaScript consumer
  • 10. @andykelk | #SydPHP Setting up a JavaScript consumer it("should return an alligator", function(done) { 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(done, function(runComplete) { expect(client.getAlligatorByName("Mary")).toEqual(new Alligator("Mary")); runComplete(); }); });
  • 11. @andykelk | #SydPHP Now we have a pact { "consumer": { "name": "Alligator Consumer" }, "provider": { "name": "Alligator Provider" }, "interactions": [ { "description": "a request for an alligator", "provider_state": "an alligator with the name Mary exists", "request": { "method": "get", "path": "/alligators/Mary", "headers": { "Accept": "application/json" } }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "body": { "name": "Mary" } } } ], "metadata": { "pactSpecificationVersion": "1.0.0" } }
  • 12. @andykelk | #SydPHP PHP Provider Rake PAC T prox y Test framework/ Test runner Mock/contract endpoint App code (service provider) Production code invokes pact json file reads
  • 13. @andykelk | #SydPHP Create the API provider <?php require 'vendor/autoload.php'; $app = new SlimSlim(); $app->get('/alligators/:name', function ($name) use ($app) { $app->response->setStatus(200); $app->response()->headers->set('Content-Type', 'application/json'); echo json_encode(array('name' => $name)); }); $app->run();
  • 14. @andykelk | #SydPHP Use rake and pact provider proxy to test require 'pact/provider/proxy/tasks' Pact::ProxyVerificationTask.new :alligator do | task | task.pact_url './spec/pacts/alligator_consumer-alligator_provider.json', :pact_helper => './spec/support/alligator_pact_helper' task.provider_base_url 'http://localhost:8000' end Pact.provider_states_for "Alligator Consumer" do provider_state "an alligator with the name Mary exists" do set_up do # Set-up the provider state (e.g. create fixture) here end end end
  • 16. @andykelk | #SydPHP What’s next? • PHP implementation of the pact specification • Use pact broker to share pact files and provide: • Auto-generated documentation • Dynamically generated network diagrams • The ability to tag a pact (ie. "prod") so a provider can verify itself against a fixed version of a pact to ensure backwards compatibility. • Webhooks to trigger provider builds when a pact is published.
  • 17. @andykelk | #SydPHP Questions? • Further reading: • http://martinfowler.com/articles/consumerDrivenContracts.html • https://github.com/realestate-com-au/pact • http://techblog.realestate.com.au/testing-interactions-with-web-services- without-integration-tests-in-ruby/ • http://techblog.realestate.com.au/enter-the-pact-matrix-or-how-to- decouple-the-release-cycles-of-your-microservices/ • Code from the demo: • https://github.com/mopoke/pact-php-demo • Relive this talk in blog form: • http://www.andykelk.net/tech/consumer-driven-contracts-with-pact-and- php