SlideShare a Scribd company logo
Introduction to
PHPUnit with
Magento
Hello!
I am Hoang Ngoc Tu
This presentation will introduce
fundamental concepts of unit testing in
PHP and Magento ecommerce
platform.
April 2015
@hoangngoctu
Main Content
◎ 1. Unit Testing
◎ 2. PHPUnit
◎ 3. PHPUnit with Magento
1.
Unit Testing
What is it? Why should I need to
know?
“
All code is guilty, until proven
innocent.
What is Unit Testing?
◎ Is a software testing method by which
individual units of source code are tested
to determine whether they are fit for use.
◎ A unit is the smallest testable part of an
application.
Unit Test Writing
◎ The general aim is to make sure every possible path
through your code is executed at least once in a test.
◎ This means you need to write tests that exercise error
conditions too.
◎ One test method per expected outcome per method
tested.
Unit Test
is an automated tool for
programmers
Unit Test
can’t replace
integration testing
Unit Test
the core of TDD - Test
Driven Development
Test Driven Development
◎ Step 1: Write a test that expresses how you’ll use the
code and what you need it to do.
◎ Step 2: Write enough code to get the test to pass, but
no more. If you need more code, for example, to check
for errors, first write another test to demonstrate that
feature.
◎ Step 3: Refactor to clean up the code to remove
redundancy and improve the design. Then re-run the
tests to make sure you didn’t break anything.
PHPUnit with Magento
At first sight of sceptical developers
◎ Time consuming
◎ Awkward, not intuitive approach
◎ Change the way we thought, the methods we worked
◎ It’s probably not in our “comfort zone”
In our experience
◎ Unit testing is generally not worth to
bother for small, short term, one-and-done
projects.
◎ Unit testing is crucial for enterprise system
maintenance and long term projects.
Why should I know it?
◎ Unit tests prove that your code actually
works.
◎ Find problems early
Why should I know it?
◎ You get a low-level regression-test suite.
◎ It allows you to make big changes to code
quickly.
Why should I know it?
◎ Test-first forces you to plan before you
code.
Why should I know it?
◎ In many cases, it’s faster than writing code
without tests.
◎ Most of the effort we spend on code, we
spend fixing it.
PHPUnit with Magento
TDD for Legacy Code
◎ Capture behaviour of current code by adding black-
box tests
◎ Refactor and add unit tests to legacy code bit-by-bit
◎ Increase test coverage over time
◎ All your new coding can be TDD
2.
PHPUnit
de-facto standard unit testing
framework for PHP
PHPUnit
◎ Created by Sebastian Bergmann
◎ Homepage: https://phpunit.de/
◎ GitHub: https://github.
com/sebastianbergmann/phpunit
PHPUnit Installation
# Manually way:
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
# Ubuntu way:
sudo apt-get install phpunit
# Composer way: check documentation for details
php composer.phar install
# Verify
phpunit --version
PHPUnit Fundamental Techniques
◎ Assertions
◎ Mocking
○ Please note that final, private and static methods
cannot be mocked.
DEMO TIME
Please check my
github for code
https://github.com/tuhn/phpunit_demo
PHPUnit Plugins
◎ phpunit-clever-and-smart reorders your tests so that
tests that failed in a previous run are executed first.
◎ phpunit-speedtrap reports slow-running tests right in
your console.
◎ paratest can execute tests in parallel.
◎ phpunit-randomizer allows you to execute your tests
in a random order.
PHPUnit Packages
◎ PHP_Invoker: a utility class for invoking callables
with a timeout.
◎ DbUnit: support database interaction testing.
◎ PHPUnit_Selenium: lets you use the WebDriver API
PHPUnit and enterprise PHP
PHPUnit helps to
transform PHP from a
language for web
projects into a serious
and reliable enterprise
platform
3.
PHPUnit with Magento
We will need an extension
Magento PHPUnit Integration
◎ Magento does not have built in unit test suite.
◎ Ecomdev_PHPUnit is an Magento extension to let us
create unit test suite based on PHPUnit.
◎ It is basically a wrapper, or layer between Magento
and PHPUnit.
◎ It also added its own specific methods for Magento
testing.
Ecomdev_PHPUnit Installation
◎ GitHub: https://github.com/EcomDev/EcomDev_PHPUnit
◎ Obtain the extension: composer
◎ Download from GitHub:
○ phpunit.xml
○ app/etc/local.xml.phpunit
Ecomdev_PHPUnit Installation
# Shell scripts needs to be run from this directory
cd $YOUR_MAGENTO_DIRECTORY/shell
# Specify your test database name and base url
php ecomdev-phpunit.php -a magento-config --db-name $DB_NAME --
base-url http://your.magento.url/
# Execute
bin/phpunit
DEMO TIME
and some
exercises
Thanks!
◎ This presentation is a very quick introduction
to Magento unit testing.
◎ Most content and practices are included in
Demo section. Sorry slide readers.
◎ More in-depth presentations are coming.
Credits
Special thanks to all the people who made and released
these awesome resources for free:
◎ Simple line icons by Mirko Monti
◎ E-commerce icons by Virgil Pana
◎ Streamline iconset by Webalys
◎ Presentation template by SlidesCarnival
◎ Photographs by Unsplash & Death to the Stock Photo
(license)

More Related Content

What's hot (20)

PPT
Code Review
Ravi Raj
 
ODP
Tdd in php a brief example
Jeremy Kendall
 
PDF
Performance profiling and testing of symfony application 2
Andrew Yatsenko
 
ODP
TDD in PHP - Memphis PHP 2011-08-25
Jeremy Kendall
 
PPTX
Automatic Test 2019-07-25
FedericoGuerinoni
 
PDF
TDD for joomla extensions
Roberto Segura
 
PDF
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Anatoliy Okhotnikov
 
PDF
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
DicodingEvent
 
PPTX
Autotools, Design Patterns and more
Vicente Bolea
 
PDF
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
VincitOy
 
ODP
Behaviour Driven Development Hands-on
Hemmerling
 
PDF
Android Devops : Master Continuous Integration and Delivery
mahmoud ramadan
 
PPTX
Workshop: Unit Testing in Python
David Tan
 
ODP
Test Driven Development (TDD) with Windows PowerShell
Hemmerling
 
PPSX
The new way to extend VSTS Build and Release
Jesse Houwing
 
PDF
Tdd 왜 배우기 어려운가
Jaehoon Oh
 
PPTX
Android tdd
Nhan Cao
 
ODP
Lighning Talk: PHP build process
Bryan Agee
 
PDF
Why and how to keep your code quality
Krešimir Antolić
 
PPTX
AspectMock
Bryce Embry
 
Code Review
Ravi Raj
 
Tdd in php a brief example
Jeremy Kendall
 
Performance profiling and testing of symfony application 2
Andrew Yatsenko
 
TDD in PHP - Memphis PHP 2011-08-25
Jeremy Kendall
 
Automatic Test 2019-07-25
FedericoGuerinoni
 
TDD for joomla extensions
Roberto Segura
 
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Anatoliy Okhotnikov
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
DicodingEvent
 
Autotools, Design Patterns and more
Vicente Bolea
 
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
VincitOy
 
Behaviour Driven Development Hands-on
Hemmerling
 
Android Devops : Master Continuous Integration and Delivery
mahmoud ramadan
 
Workshop: Unit Testing in Python
David Tan
 
Test Driven Development (TDD) with Windows PowerShell
Hemmerling
 
The new way to extend VSTS Build and Release
Jesse Houwing
 
Tdd 왜 배우기 어려운가
Jaehoon Oh
 
Android tdd
Nhan Cao
 
Lighning Talk: PHP build process
Bryan Agee
 
Why and how to keep your code quality
Krešimir Antolić
 
AspectMock
Bryce Embry
 

Similar to PHPUnit with Magento (20)

PPTX
Indy meetup#7 effective unit-testing-mule
ikram_ahamed
 
PPTX
Test Drive Dirven Driver HAHAahhaha.pptx
findwaytocom
 
PDF
Unit testing (eng)
Anatoliy Okhotnikov
 
PDF
Writing Tests with the Unity Test Framework
Peter Kofler
 
PDF
Android Test Driven Development & Android Unit Testing
mahmoud ramadan
 
PDF
Python and test
Micron Technology
 
PDF
Cursus phpunit
Nick Belhomme
 
PPTX
Dot all 2019 | Testing with Craft | Giel Tettelar
Giel Tettelaar
 
PDF
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
mCloud
 
PDF
UPC Plone Testing Talk
Timo Stollenwerk
 
PDF
PMI-ACP Lesson 06 Quality
Thanh Nguyen
 
PPTX
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
 
PPTX
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Elad Elrom
 
PPT
Test Driven Development - Overview and Adoption
Pyxis Technologies
 
PDF
Codeception: introduction to php testing
Engineor
 
PPTX
Test Driven Development
bhochhi
 
PDF
Back to basics - PHPUnit
Sebastian Marek
 
PPTX
TDD - Agile
harinderpisces
 
PPT
Unit testing
dubbu
 
Indy meetup#7 effective unit-testing-mule
ikram_ahamed
 
Test Drive Dirven Driver HAHAahhaha.pptx
findwaytocom
 
Unit testing (eng)
Anatoliy Okhotnikov
 
Writing Tests with the Unity Test Framework
Peter Kofler
 
Android Test Driven Development & Android Unit Testing
mahmoud ramadan
 
Python and test
Micron Technology
 
Cursus phpunit
Nick Belhomme
 
Dot all 2019 | Testing with Craft | Giel Tettelar
Giel Tettelaar
 
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
mCloud
 
UPC Plone Testing Talk
Timo Stollenwerk
 
PMI-ACP Lesson 06 Quality
Thanh Nguyen
 
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
 
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Elad Elrom
 
Test Driven Development - Overview and Adoption
Pyxis Technologies
 
Codeception: introduction to php testing
Engineor
 
Test Driven Development
bhochhi
 
Back to basics - PHPUnit
Sebastian Marek
 
TDD - Agile
harinderpisces
 
Unit testing
dubbu
 
Ad

Recently uploaded (20)

PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
Next level data operations using Power Automate magic
Andries den Haan
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Kubernetes - Architecture & Components.pdf
geethak285
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
Practical Applications of AI in Local Government
OnBoard
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
Next level data operations using Power Automate magic
Andries den Haan
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Ad

PHPUnit with Magento

  • 2. Hello! I am Hoang Ngoc Tu This presentation will introduce fundamental concepts of unit testing in PHP and Magento ecommerce platform. April 2015 @hoangngoctu
  • 3. Main Content ◎ 1. Unit Testing ◎ 2. PHPUnit ◎ 3. PHPUnit with Magento
  • 4. 1. Unit Testing What is it? Why should I need to know?
  • 5. “ All code is guilty, until proven innocent.
  • 6. What is Unit Testing? ◎ Is a software testing method by which individual units of source code are tested to determine whether they are fit for use. ◎ A unit is the smallest testable part of an application.
  • 7. Unit Test Writing ◎ The general aim is to make sure every possible path through your code is executed at least once in a test. ◎ This means you need to write tests that exercise error conditions too. ◎ One test method per expected outcome per method tested.
  • 8. Unit Test is an automated tool for programmers
  • 10. Unit Test the core of TDD - Test Driven Development
  • 11. Test Driven Development ◎ Step 1: Write a test that expresses how you’ll use the code and what you need it to do. ◎ Step 2: Write enough code to get the test to pass, but no more. If you need more code, for example, to check for errors, first write another test to demonstrate that feature. ◎ Step 3: Refactor to clean up the code to remove redundancy and improve the design. Then re-run the tests to make sure you didn’t break anything.
  • 13. At first sight of sceptical developers ◎ Time consuming ◎ Awkward, not intuitive approach ◎ Change the way we thought, the methods we worked ◎ It’s probably not in our “comfort zone”
  • 14. In our experience ◎ Unit testing is generally not worth to bother for small, short term, one-and-done projects. ◎ Unit testing is crucial for enterprise system maintenance and long term projects.
  • 15. Why should I know it? ◎ Unit tests prove that your code actually works. ◎ Find problems early
  • 16. Why should I know it? ◎ You get a low-level regression-test suite. ◎ It allows you to make big changes to code quickly.
  • 17. Why should I know it? ◎ Test-first forces you to plan before you code.
  • 18. Why should I know it? ◎ In many cases, it’s faster than writing code without tests. ◎ Most of the effort we spend on code, we spend fixing it.
  • 20. TDD for Legacy Code ◎ Capture behaviour of current code by adding black- box tests ◎ Refactor and add unit tests to legacy code bit-by-bit ◎ Increase test coverage over time ◎ All your new coding can be TDD
  • 21. 2. PHPUnit de-facto standard unit testing framework for PHP
  • 22. PHPUnit ◎ Created by Sebastian Bergmann ◎ Homepage: https://phpunit.de/ ◎ GitHub: https://github. com/sebastianbergmann/phpunit
  • 23. PHPUnit Installation # Manually way: wget https://phar.phpunit.de/phpunit.phar chmod +x phpunit.phar mv phpunit.phar /usr/local/bin/phpunit # Ubuntu way: sudo apt-get install phpunit # Composer way: check documentation for details php composer.phar install # Verify phpunit --version
  • 24. PHPUnit Fundamental Techniques ◎ Assertions ◎ Mocking ○ Please note that final, private and static methods cannot be mocked.
  • 25. DEMO TIME Please check my github for code https://github.com/tuhn/phpunit_demo
  • 26. PHPUnit Plugins ◎ phpunit-clever-and-smart reorders your tests so that tests that failed in a previous run are executed first. ◎ phpunit-speedtrap reports slow-running tests right in your console. ◎ paratest can execute tests in parallel. ◎ phpunit-randomizer allows you to execute your tests in a random order.
  • 27. PHPUnit Packages ◎ PHP_Invoker: a utility class for invoking callables with a timeout. ◎ DbUnit: support database interaction testing. ◎ PHPUnit_Selenium: lets you use the WebDriver API
  • 28. PHPUnit and enterprise PHP PHPUnit helps to transform PHP from a language for web projects into a serious and reliable enterprise platform
  • 29. 3. PHPUnit with Magento We will need an extension
  • 30. Magento PHPUnit Integration ◎ Magento does not have built in unit test suite. ◎ Ecomdev_PHPUnit is an Magento extension to let us create unit test suite based on PHPUnit. ◎ It is basically a wrapper, or layer between Magento and PHPUnit. ◎ It also added its own specific methods for Magento testing.
  • 31. Ecomdev_PHPUnit Installation ◎ GitHub: https://github.com/EcomDev/EcomDev_PHPUnit ◎ Obtain the extension: composer ◎ Download from GitHub: ○ phpunit.xml ○ app/etc/local.xml.phpunit
  • 32. Ecomdev_PHPUnit Installation # Shell scripts needs to be run from this directory cd $YOUR_MAGENTO_DIRECTORY/shell # Specify your test database name and base url php ecomdev-phpunit.php -a magento-config --db-name $DB_NAME -- base-url http://your.magento.url/ # Execute bin/phpunit
  • 34. Thanks! ◎ This presentation is a very quick introduction to Magento unit testing. ◎ Most content and practices are included in Demo section. Sorry slide readers. ◎ More in-depth presentations are coming.
  • 35. Credits Special thanks to all the people who made and released these awesome resources for free: ◎ Simple line icons by Mirko Monti ◎ E-commerce icons by Virgil Pana ◎ Streamline iconset by Webalys ◎ Presentation template by SlidesCarnival ◎ Photographs by Unsplash & Death to the Stock Photo (license)