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

PDF
Civilized Git Process
PPT
Test Driven Development
PPT
Test-Driven Development
PPT
Test-Driven Development
PDF
User story workflow (eng)
PDF
Test Driven Development Methodology and Philosophy
PDF
Code Review
PDF
Test Driven Development with PHP
Civilized Git Process
Test Driven Development
Test-Driven Development
Test-Driven Development
User story workflow (eng)
Test Driven Development Methodology and Philosophy
Code Review
Test Driven Development with PHP

What's hot (20)

PPT
Code Review
ODP
Tdd in php a brief example
PDF
Performance profiling and testing of symfony application 2
ODP
TDD in PHP - Memphis PHP 2011-08-25
PPTX
Automatic Test 2019-07-25
PDF
TDD for joomla extensions
PDF
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
PDF
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
PPTX
Autotools, Design Patterns and more
PDF
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
ODP
Behaviour Driven Development Hands-on
PDF
Android Devops : Master Continuous Integration and Delivery
PPTX
Workshop: Unit Testing in Python
ODP
Test Driven Development (TDD) with Windows PowerShell
PPSX
The new way to extend VSTS Build and Release
PDF
Tdd 왜 배우기 어려운가
PPTX
Android tdd
ODP
Lighning Talk: PHP build process
PDF
Why and how to keep your code quality
PPTX
AspectMock
Code Review
Tdd in php a brief example
Performance profiling and testing of symfony application 2
TDD in PHP - Memphis PHP 2011-08-25
Automatic Test 2019-07-25
TDD for joomla extensions
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Autotools, Design Patterns and more
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
Behaviour Driven Development Hands-on
Android Devops : Master Continuous Integration and Delivery
Workshop: Unit Testing in Python
Test Driven Development (TDD) with Windows PowerShell
The new way to extend VSTS Build and Release
Tdd 왜 배우기 어려운가
Android tdd
Lighning Talk: PHP build process
Why and how to keep your code quality
AspectMock
Ad

Similar to PHPUnit with Magento (20)

PPTX
PHPUnit: from zero to hero
PPTX
Getting started with PHPUnit
PPTX
Unit Testng with PHP Unit - A Step by Step Training
PPT
Unit testing php-unit - phing - selenium_v2
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
PPT
Automated Unit Testing
PPT
Test Driven Development with PHPUnit
PDF
Leveling Up With Unit Testing - php[tek] 2023
PPTX
Getting started-php unit
PDF
Test Automation
KEY
Php Unit With Zend Framework Zendcon09
PDF
Fighting Fear-Driven-Development With PHPUnit
PPT
Unit testing
PDF
Introduction to Unit Testing with PHPUnit
KEY
Developer testing 201: When to Mock and When to Integrate
ZIP
Test
KEY
PHPUnit testing to Zend_Test
KEY
Developer testing 101: Become a Testing Fanatic
PPTX
PHPUnit: from zero to hero
Getting started with PHPUnit
Unit Testng with PHP Unit - A Step by Step Training
Unit testing php-unit - phing - selenium_v2
Leveling Up With Unit Testing - LonghornPHP 2022
Automated Unit Testing
Test Driven Development with PHPUnit
Leveling Up With Unit Testing - php[tek] 2023
Getting started-php unit
Test Automation
Php Unit With Zend Framework Zendcon09
Fighting Fear-Driven-Development With PHPUnit
Unit testing
Introduction to Unit Testing with PHPUnit
Developer testing 201: When to Mock and When to Integrate
Test
PHPUnit testing to Zend_Test
Developer testing 101: Become a Testing Fanatic
Ad

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
KodekX | Application Modernization Development
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation theory and applications.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Programs and apps: productivity, graphics, security and other tools
Chapter 3 Spatial Domain Image Processing.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Per capita expenditure prediction using model stacking based on satellite ima...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
KodekX | Application Modernization Development
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Reach Out and Touch Someone: Haptics and Empathic Computing
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Network Security Unit 5.pdf for BCA BBA.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

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)