SlideShare une entreprise Scribd logo
1  sur  76
Télécharger pour lire hors ligne
TDD with phpspec2
Who I am
• Anton Serdyuk
• email/jabber: anton.serdyuk@gmail.com
• skype: serdyuk.anton
• twitter: @anton_serdyuk
• facebook: anton.serdyuk
Overview
Auto-tests forms
• Functional tests (+BDD)
• Integration tests
• Unit tests
Functional tests
• Use full deployed application instance
• Test business expectations
• Use the same UI interactions as real user
Functional tests with phpunit example
BDD (or story BDD)
• Story-driven functional tests
• BDD ⊂ Functional tests
BDD with behat example
Integration tests
• Use integrated parts of applications
• Can use database, filesystem etc.
• Can test API, Doctrine repositories etc.
Integration tests with PHPUnit example
Unit tests
• Use small parts of code
• Can not use database
• Can not use filesystem
• Extremely fast
Consider using appropriate
tools for tests
phpunit behat phpspec codeception
Functional + + +
Integration +
+
(sometimes)
+
Unit + + +
My personal preference
• behat for functional and API integration tests
• PHPUnit for integration tests
• phpspec for unittest
phpspec basics
Installation
Create test
for class WowHero
Run tests
phpspec will create classes for you
Run tests
generated class WowHero
Run tests
Run tests again, now with generated class
Matchers (assertions)
Every hero has level
Matchers
phpspec will generate code for you
Matchers
phpspec will generate code for you
Matchers
Red
Matchers
Green
Matchers
Refactor
Matchers
• Identity (return, be, equal, beEqualTo) - ===
• Comparison (beLike) - ==
• Throw (throw -> during) - exceptions
• Type (beAnInstanceOf etc.) - type
• Custom matchers
• http://www.phpspec.net/cookbook/matchers.html
Matchers
Use source code for more matchers
Exception Matchers
Generate
Exception Matchers
Generate
Exception Matchers
Generate
Exception Matchers
Red
Exception Matchers
Green
Mocks
Every Hero has race and class
Mocks
Generate
Mocks
Generate
Mocks
Generate
Mocks
Red
Mocks
Green
Mocks
Green (whoops!)
Mocks
Green
Mocks
Refactor - let() / letgo()
Mocks
Refactor - let() / letgo()
Mocks
Refactor
Predictions
Predictions
Red
Predictions
Green
Predictions
Red
Predictions
Green
Advanced topics
Nested Object Matchers
Public Property Mock
Sometimes you want to mock public properties
Promises and Predictions Order
1) Promises -> 2) Predictions -> 3) Call
Promises and Predictions Order
1) Promises -> 2) Predictions -> 3) Call
Promises and Predictions Order
1) Promises -> 2) Predictions -> 3) Call
Predictions Argument Wildcarding
Predictions Argument
Wildcarding
• Argument::exact($value)
• Argument::type($typeOrClass)
• Argument::which($method, $value)
• Argument::that(callback)
• Argument::any()
• Argument::cetera()
• https://github.com/phpspec/prophecy#arguments-wildcarding
Array Mocks
Generate
Array Mocks
Generate
Array Mocks
Red
Array Mocks
Green (whoops!)
Array Mocks
Green
Get Mock Object
Get Mock Object
Get Mock Object
Generate
Get Mock Object
Generate (whoops!)
Get Mock Object
This is PHP, baby
???
Get Mock Object
Right way
Best Practices
IMHOs
Do not use TDD in simple
CRUD applications (IMHO)
• High cost
• Slow (in terms of development time)
• Zero profit
• Use functional and integration tests instead
Do not mock 3rd party code
• Large complicated tests
• You actually test not how your code works with this
library, but how your code works with your
understanding of this library
Do not mock 3rd party code
Use interfaces instead of 3rd party dependent code
Consider using appropriate
DI library
• symfony/di and Pimple are not well suited for TDD
because of large amount of small classes
• you must define rule for every class, which is very
boring
• There are some DI containers which will create
most objects without explicitly defined rules by just
reading constructor arguments type hints through
reflection
Consider using appropriate
DI library
• zend-di
• Laravel IoC Container - http://laravel.com/docs/ioc
• PHP-DI - http://php-di.org/
phpspec at CI
phpspec junit formatter
Thank you!
Questions?

Contenu connexe

Tendances

Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScript
pamselle
 

Tendances (20)

RSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodRSpec 3: The new, the old, the good
RSpec 3: The new, the old, the good
 
RSpec: What, How and Why
RSpec: What, How and WhyRSpec: What, How and Why
RSpec: What, How and Why
 
Rspec presentation
Rspec presentationRspec presentation
Rspec presentation
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
 
Automated testing with RSpec
Automated testing with RSpecAutomated testing with RSpec
Automated testing with RSpec
 
Create a web-app with Cgi Appplication
Create a web-app with Cgi AppplicationCreate a web-app with Cgi Appplication
Create a web-app with Cgi Appplication
 
Dart Workshop
Dart WorkshopDart Workshop
Dart Workshop
 
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScript
 
Unit Testing in SilverStripe
Unit Testing in SilverStripeUnit Testing in SilverStripe
Unit Testing in SilverStripe
 
Perl Tidy Perl Critic
Perl Tidy Perl CriticPerl Tidy Perl Critic
Perl Tidy Perl Critic
 
RSpec and Rails
RSpec and RailsRSpec and Rails
RSpec and Rails
 
Introduction to testing in Rails
Introduction to testing in RailsIntroduction to testing in Rails
Introduction to testing in Rails
 
MidwestJS Zero to Testing
MidwestJS Zero to TestingMidwestJS Zero to Testing
MidwestJS Zero to Testing
 
Rspec 101
Rspec 101Rspec 101
Rspec 101
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning C
 

En vedette

BDD: Descubriendo qué requiere realmente tu cliente
BDD: Descubriendo qué requiere realmente tu clienteBDD: Descubriendo qué requiere realmente tu cliente
BDD: Descubriendo qué requiere realmente tu cliente
Jorge Gamba
 
Introducción a Ganglia
Introducción a GangliaIntroducción a Ganglia
Introducción a Ganglia
Dardo Valdez
 
Conferencia Rails: Integracion Continua Y Rails
Conferencia Rails: Integracion Continua Y RailsConferencia Rails: Integracion Continua Y Rails
Conferencia Rails: Integracion Continua Y Rails
David Calavera
 
Introducción a DDD
Introducción a DDDIntroducción a DDD
Introducción a DDD
sergiopolo
 

En vedette (20)

DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 
Contao Open Source CMS -- 新しいメジャーリリースとLTSリリース
Contao Open Source CMS -- 新しいメジャーリリースとLTSリリースContao Open Source CMS -- 新しいメジャーリリースとLTSリリース
Contao Open Source CMS -- 新しいメジャーリリースとLTSリリース
 
Yet Another Continuous Integration Story
Yet Another Continuous Integration StoryYet Another Continuous Integration Story
Yet Another Continuous Integration Story
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
BDD: Descubriendo qué requiere realmente tu cliente
BDD: Descubriendo qué requiere realmente tu clienteBDD: Descubriendo qué requiere realmente tu cliente
BDD: Descubriendo qué requiere realmente tu cliente
 
Introducción a Ganglia
Introducción a GangliaIntroducción a Ganglia
Introducción a Ganglia
 
Conferencia Rails: Integracion Continua Y Rails
Conferencia Rails: Integracion Continua Y RailsConferencia Rails: Integracion Continua Y Rails
Conferencia Rails: Integracion Continua Y Rails
 
Introducción a LDAP
Introducción a LDAPIntroducción a LDAP
Introducción a LDAP
 
Software Debt: Qué Es y Cómo Gestionarlo Holísticamente
Software Debt: Qué Es y Cómo Gestionarlo HolísticamenteSoftware Debt: Qué Es y Cómo Gestionarlo Holísticamente
Software Debt: Qué Es y Cómo Gestionarlo Holísticamente
 
Alta disponibilidad con MySQL
Alta disponibilidad con MySQLAlta disponibilidad con MySQL
Alta disponibilidad con MySQL
 
Escalabilidad y alto rendimiento con Symfony2
Escalabilidad y alto rendimiento con Symfony2Escalabilidad y alto rendimiento con Symfony2
Escalabilidad y alto rendimiento con Symfony2
 
OAUTH introducción y entretenida explicación.
OAUTH introducción y entretenida explicación.OAUTH introducción y entretenida explicación.
OAUTH introducción y entretenida explicación.
 
Conferencia Monitoreo de Servidores con Nagios
Conferencia Monitoreo de Servidores con NagiosConferencia Monitoreo de Servidores con Nagios
Conferencia Monitoreo de Servidores con Nagios
 
Применение DDD подхода в Symfony 2 приложении
Применение DDD подхода в Symfony 2 приложенииПрименение DDD подхода в Symfony 2 приложении
Применение DDD подхода в Symfony 2 приложении
 
Continous Delivering a PHP application
Continous Delivering a PHP applicationContinous Delivering a PHP application
Continous Delivering a PHP application
 
Maven Overview
Maven OverviewMaven Overview
Maven Overview
 
Automatizacion de proyectos con gradle
Automatizacion de proyectos con gradleAutomatizacion de proyectos con gradle
Automatizacion de proyectos con gradle
 
Integrando sonar
Integrando sonarIntegrando sonar
Integrando sonar
 
PhpSpec extension points
PhpSpec extension pointsPhpSpec extension points
PhpSpec extension points
 
Introducción a DDD
Introducción a DDDIntroducción a DDD
Introducción a DDD
 

Similaire à TDD with phpspec2

Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTT
kevinvw
 
GDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineGDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App Engine
Yared Ayalew
 

Similaire à TDD with phpspec2 (20)

Developer testing 201: When to Mock and When to Integrate
Developer testing 201: When to Mock and When to IntegrateDeveloper testing 201: When to Mock and When to Integrate
Developer testing 201: When to Mock and When to Integrate
 
Developer testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing FanaticDeveloper testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing Fanatic
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTT
 
Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and Django
 
Django Overview
Django OverviewDjango Overview
Django Overview
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
How to Reverse Engineer Web Applications
How to Reverse Engineer Web ApplicationsHow to Reverse Engineer Web Applications
How to Reverse Engineer Web Applications
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)
 
GDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineGDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App Engine
 
Green dao
Green daoGreen dao
Green dao
 
TDD and mock objects
TDD and mock objectsTDD and mock objects
TDD and mock objects
 
Gaej For Beginners
Gaej For BeginnersGaej For Beginners
Gaej For Beginners
 
10 Ways To Improve Your Code
10 Ways To Improve Your Code10 Ways To Improve Your Code
10 Ways To Improve Your Code
 
Protect Your Payloads: Modern Keying Techniques
Protect Your Payloads: Modern Keying TechniquesProtect Your Payloads: Modern Keying Techniques
Protect Your Payloads: Modern Keying Techniques
 
Our Puppet Story (GUUG FFG 2015)
Our Puppet Story (GUUG FFG 2015)Our Puppet Story (GUUG FFG 2015)
Our Puppet Story (GUUG FFG 2015)
 
Static Code Analysis PHP[tek] 2023
Static Code Analysis PHP[tek] 2023Static Code Analysis PHP[tek] 2023
Static Code Analysis PHP[tek] 2023
 
Java scriptforjavadev part2a
Java scriptforjavadev part2aJava scriptforjavadev part2a
Java scriptforjavadev part2a
 
Building XWiki
Building XWikiBuilding XWiki
Building XWiki
 
Property Based Testing in PHP
Property Based Testing in PHPProperty Based Testing in PHP
Property Based Testing in PHP
 
Celery: The Distributed Task Queue
Celery: The Distributed Task QueueCelery: The Distributed Task Queue
Celery: The Distributed Task Queue
 

Dernier

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

TDD with phpspec2