SlideShare a Scribd company logo
1 of 35
Download to read offline
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.
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.
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

Code Review
Code ReviewCode Review
Code ReviewRavi Raj
 
Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief exampleJeremy Kendall
 
Performance profiling and testing of symfony application 2
Performance profiling and testing of symfony application 2Performance profiling and testing of symfony application 2
Performance profiling and testing of symfony application 2Andrew Yatsenko
 
TDD in PHP - Memphis PHP 2011-08-25
TDD in PHP - Memphis PHP 2011-08-25TDD in PHP - Memphis PHP 2011-08-25
TDD in PHP - Memphis PHP 2011-08-25Jeremy Kendall
 
TDD for joomla extensions
TDD for joomla extensionsTDD for joomla extensions
TDD for joomla extensionsRoberto Segura
 
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)
Project Management: Burn-Down Chart / OrangeHRM Project MOD (eng)Anatoliy Okhotnikov
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...DicodingEvent
 
Autotools, Design Patterns and more
Autotools, Design Patterns and moreAutotools, Design Patterns and more
Autotools, Design Patterns and moreVicente Bolea
 
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...VincitOy
 
Behaviour Driven Development Hands-on
Behaviour Driven Development Hands-onBehaviour Driven Development Hands-on
Behaviour Driven Development Hands-onHemmerling
 
Android Devops : Master Continuous Integration and Delivery
Android Devops : Master Continuous Integration and DeliveryAndroid Devops : Master Continuous Integration and Delivery
Android Devops : Master Continuous Integration and Deliverymahmoud ramadan
 
Workshop: Unit Testing in Python
Workshop: Unit Testing in PythonWorkshop: Unit Testing in Python
Workshop: Unit Testing in PythonDavid Tan
 
Test Driven Development (TDD) with Windows PowerShell
Test Driven Development (TDD) with Windows PowerShellTest Driven Development (TDD) with Windows PowerShell
Test Driven Development (TDD) with Windows PowerShellHemmerling
 
The new way to extend VSTS Build and Release
The new way to extend VSTS Build and ReleaseThe new way to extend VSTS Build and Release
The new way to extend VSTS Build and ReleaseJesse Houwing
 
Tdd 왜 배우기 어려운가
Tdd 왜 배우기 어려운가Tdd 왜 배우기 어려운가
Tdd 왜 배우기 어려운가Jaehoon Oh
 
Android tdd
Android tddAndroid tdd
Android tddNhan Cao
 
Lighning Talk: PHP build process
Lighning Talk: PHP build processLighning Talk: PHP build process
Lighning Talk: PHP build processBryan Agee
 
Why and how to keep your code quality
Why and how to keep your code quality Why and how to keep your code quality
Why and how to keep your code quality Krešimir Antolić
 

What's hot (20)

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

Similar to PHPUnit with Magento

Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleikram_ahamed
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkPeter Kofler
 
Android Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit TestingAndroid Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit Testingmahmoud ramadan
 
Dot all 2019 | Testing with Craft | Giel Tettelar
Dot all 2019 | Testing with Craft | Giel TettelarDot all 2019 | Testing with Craft | Giel Tettelar
Dot all 2019 | Testing with Craft | Giel TettelarGiel Tettelaar
 
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...mCloud
 
PMI-ACP Lesson 06 Quality
PMI-ACP Lesson 06 QualityPMI-ACP Lesson 06 Quality
PMI-ACP Lesson 06 QualityThanh Nguyen
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010guest5639fa9
 
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoTest Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoElad Elrom
 
Test Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionTest Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionPyxis Technologies
 
Codeception: introduction to php testing
Codeception: introduction to php testingCodeception: introduction to php testing
Codeception: introduction to php testingEngineor
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentbhochhi
 
Back to basics - PHPUnit
Back to basics - PHPUnitBack to basics - PHPUnit
Back to basics - PHPUnitSebastian Marek
 
Unit testing
Unit testing Unit testing
Unit testing dubbu
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 

Similar to PHPUnit with Magento (20)

Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Android Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit TestingAndroid Test Driven Development & Android Unit Testing
Android Test Driven Development & Android Unit Testing
 
Python and test
Python and testPython and test
Python and test
 
Cursus phpunit
Cursus phpunitCursus phpunit
Cursus phpunit
 
Dot all 2019 | Testing with Craft | Giel Tettelar
Dot all 2019 | Testing with Craft | Giel TettelarDot all 2019 | Testing with Craft | Giel Tettelar
Dot all 2019 | Testing with Craft | Giel Tettelar
 
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
 
UPC Plone Testing Talk
UPC Plone Testing TalkUPC Plone Testing Talk
UPC Plone Testing Talk
 
PMI-ACP Lesson 06 Quality
PMI-ACP Lesson 06 QualityPMI-ACP Lesson 06 Quality
PMI-ACP Lesson 06 Quality
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoTest Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
 
Test Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionTest Driven Development - Overview and Adoption
Test Driven Development - Overview and Adoption
 
Codeception: introduction to php testing
Codeception: introduction to php testingCodeception: introduction to php testing
Codeception: introduction to php testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Back to basics - PHPUnit
Back to basics - PHPUnitBack to basics - PHPUnit
Back to basics - PHPUnit
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
Unit testing
Unit testing Unit testing
Unit testing
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 

Recently uploaded

Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Recently uploaded (20)

Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 

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.
  • 12.
  • 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.
  • 19.
  • 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)