SlideShare a Scribd company logo
Sudar Muthu | @sudarmuthu #WCPune
Unit Testing For
WordPress
WordCamp Pune, 2015
Sudar Muthu
http://sudarmuthu.com
https://github.com/sudar
Sudar Muthu | @sudarmuthu #WCPune
Me
• Programming in PHP for more than a decade and in
WordPress for about 8 years.
• Big fan of automating process and workflows.
• Contributor to a couple of open source projects.
• Remote worker at 10up (and yes 10up is hiring :) )
Sudar Muthu | @sudarmuthu #WCPune
What about you?
• What is your typical development environment?
• What is your experience with PHP and WordPress?
• What is your experience with Unit Testing?
• What are your expectations out of this talk?
Sudar Muthu | @sudarmuthu #WCPune
Unit Testing
Credit: https://twitter.com/alistratov/status/599109195548459009
Sudar Muthu | @sudarmuthu #WCPune
Is there something wrong?
class Sample {
protected $value;
public function initialise($value) {
$this->value = $value;
}
public function execute() {
if (!$this->value) {
throw new Exception("value not set");
}
return $value * 10; // business logic
}
}
$sample = new Sample;
$sample->execute();
Sudar Muthu | @sudarmuthu #WCPune
Finding bugs is not
easy
Sudar Muthu | @sudarmuthu #WCPune
Write Tests
Sudar Muthu | @sudarmuthu #WCPune
Write Tests
It sounds obvious but getting started is the hardest part!
Sudar Muthu | @sudarmuthu #WCPune
Different types of Testing
• Functionality testing
• Integration testing
• Unit testing
Sudar Muthu | @sudarmuthu #WCPune
Different types of Testing
• Functionality testing
• Integration testing
• Unit testing
Sudar Muthu | @sudarmuthu #WCPune
Briefly
• What is PHPUnit?
• Installing PHPUnit
• Setting up folder structure
• phpunit.xml
Sudar Muthu | @sudarmuthu #WCPune
Three steps in test cases
• setup
• act
• verify
Sudar Muthu | @sudarmuthu #WCPune
Demo
Sudar Muthu | @sudarmuthu #WCPune
Writing our first test case
public function test_execute_works_with_initialise() {
// setup
$sample = new Sample();
// act
$sample->initialise(10);
$return = $sample->execute();
// verify
$this->assertEquals(100, $return);
}
Sudar Muthu | @sudarmuthu #WCPune
Testing Exception
/**
* @expectedException Exception
*/
public function test_execute_needs_initialise() {
// setup
$sample = new Sample();
// act
$sample->execute();
// verify
// that it throws and exception
}
Sudar Muthu | @sudarmuthu #WCPune
Let’s add more tests
• Testing decimals
• Testing with negative values
• Testing it work with zero
Sudar Muthu | @sudarmuthu #WCPune
Unit Testing
WordPress Code
Sudar Muthu | @sudarmuthu #WCPune
What should be tested?
function my_permalink_function( $post_id ) {
$permalink = get_permalink( absint( $post_id ) );
$permalink = apply_filters( 'special_filter', $permalink );
do_action( 'special_action', $permalink );
return $permalink;
}
Sudar Muthu | @sudarmuthu #WCPune
Don’t test the WordPress
built-in function
Sudar Muthu | @sudarmuthu #WCPune
Don’t test the WordPress
built-in function
Use Mocks instead
https://github.com/10up/wp_mock
Sudar Muthu | @sudarmuthu #WCPune
Mocking WordPress function
WP_Mock::wpFunction( 'get_permalink', array(
'args' => 42,
'times' => 1,
'return' => 'http://example.com/foo'
) );
Sudar Muthu | @sudarmuthu #WCPune
Mocking WordPress filter
WP_Mock::onFilter( 'special_filter' )
->with( 'http://example.com/foo' )
->reply( 'https://example.com/bar' );
Sudar Muthu | @sudarmuthu #WCPune
Mocking WordPress action
WP_Mock::expectAction(
'special_action',
'https://example.com/bar'
);
Sudar Muthu | @sudarmuthu #WCPune
Putting it all together
public function test_my_permalink_function() {
WP_Mock::wpFunction( 'get_permalink', array(
'args' => 42,
'times' => 1,
'return' => 'http://example.com/foo'
) );
WP_Mock::wpPassthruFunction( 'absint', array( 'times' => 1 ) );
WP_Mock::onFilter( 'special_filter' )
->with( 'http://example.com/foo' )
->reply( 'https://example.com/bar' );
WP_Mock::expectAction( 'special_action', 'https://example.com/bar' );
$result = my_permalink_function( 42 );
$this->assertEquals( 'https://example.com/bar', $result );
}
Sudar Muthu | @sudarmuthu #WCPune
Some PHPUnit Tips
• Have a fast test suite
• Use Composer
• Enable code coverage in reports
• phpunit.xml.dist vs phpunit.xml
• Use specific assertions
• Check out Unit tests in WordPress core
Sudar Muthu | @sudarmuthu #WCPune
WordPress plugin
skeleton with Tests
Use yo generator
https://github.com/10up/generator-wp-make
Sudar Muthu | @sudarmuthu #WCPune
Thank You
@sudarmuthu
http://sudarmuthu.com
https://github.com/sudar

More Related Content

What's hot

JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
Christian Johansen
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
Christian Johansen
 
Agile2016: Exploratory Testing an API
Agile2016: Exploratory Testing an APIAgile2016: Exploratory Testing an API
Agile2016: Exploratory Testing an API
Maaret Pyhäjärvi
 
[부스트캠프 Tech Talk] 진명훈_datasets로 협업하기
[부스트캠프 Tech Talk] 진명훈_datasets로 협업하기[부스트캠프 Tech Talk] 진명훈_datasets로 협업하기
[부스트캠프 Tech Talk] 진명훈_datasets로 협업하기
CONNECT FOUNDATION
 
Rails Testing
Rails TestingRails Testing
Rails Testing
mikeblake
 
Making cross browser tests beautiful
Making cross browser tests beautifulMaking cross browser tests beautiful
Making cross browser tests beautiful
Meaghan Lewis
 
[부스트캠프 Tech Talk] 고지형_내 자식 하나쯤은 있어야죠
[부스트캠프 Tech Talk] 고지형_내 자식 하나쯤은 있어야죠[부스트캠프 Tech Talk] 고지형_내 자식 하나쯤은 있어야죠
[부스트캠프 Tech Talk] 고지형_내 자식 하나쯤은 있어야죠
CONNECT FOUNDATION
 
Making Software Management tools work for you - 2011 PHPBenelux Conference
Making Software Management tools work for you - 2011 PHPBenelux ConferenceMaking Software Management tools work for you - 2011 PHPBenelux Conference
Making Software Management tools work for you - 2011 PHPBenelux Conference
John Mertic
 
Automated tests
Automated testsAutomated tests
Automated tests
Damian Sromek
 
How to Use Selenium, Successfully
How to Use Selenium, SuccessfullyHow to Use Selenium, Successfully
How to Use Selenium, Successfully
Sauce Labs
 
Selenium Testing with TestingBot.com
Selenium Testing with TestingBot.comSelenium Testing with TestingBot.com
Selenium Testing with TestingBot.com
testingbot
 

What's hot (12)

JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
Agile2016: Exploratory Testing an API
Agile2016: Exploratory Testing an APIAgile2016: Exploratory Testing an API
Agile2016: Exploratory Testing an API
 
[부스트캠프 Tech Talk] 진명훈_datasets로 협업하기
[부스트캠프 Tech Talk] 진명훈_datasets로 협업하기[부스트캠프 Tech Talk] 진명훈_datasets로 협업하기
[부스트캠프 Tech Talk] 진명훈_datasets로 협업하기
 
Rails Testing
Rails TestingRails Testing
Rails Testing
 
Making cross browser tests beautiful
Making cross browser tests beautifulMaking cross browser tests beautiful
Making cross browser tests beautiful
 
[부스트캠프 Tech Talk] 고지형_내 자식 하나쯤은 있어야죠
[부스트캠프 Tech Talk] 고지형_내 자식 하나쯤은 있어야죠[부스트캠프 Tech Talk] 고지형_내 자식 하나쯤은 있어야죠
[부스트캠프 Tech Talk] 고지형_내 자식 하나쯤은 있어야죠
 
Js unit testing
Js unit testingJs unit testing
Js unit testing
 
Making Software Management tools work for you - 2011 PHPBenelux Conference
Making Software Management tools work for you - 2011 PHPBenelux ConferenceMaking Software Management tools work for you - 2011 PHPBenelux Conference
Making Software Management tools work for you - 2011 PHPBenelux Conference
 
Automated tests
Automated testsAutomated tests
Automated tests
 
How to Use Selenium, Successfully
How to Use Selenium, SuccessfullyHow to Use Selenium, Successfully
How to Use Selenium, Successfully
 
Selenium Testing with TestingBot.com
Selenium Testing with TestingBot.comSelenium Testing with TestingBot.com
Selenium Testing with TestingBot.com
 

Similar to Unit testing for WordPress

WordPress Developer tools
WordPress Developer toolsWordPress Developer tools
WordPress Developer tools
Sudar Muthu
 
JavaScript code generator with Yeoman
JavaScript code generator with YeomanJavaScript code generator with Yeoman
JavaScript code generator with Yeoman
tomi vanek
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs Life
Peter Gfader
 
Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptx
Tamas Rev
 
Physical web
Physical webPhysical web
Physical web
Jeff Prestes
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
Hands On with Selenium and WebDriver
Hands On with Selenium and WebDriverHands On with Selenium and WebDriver
Hands On with Selenium and WebDriver
TechWell
 
おっぴろげJavaEE DevOps
おっぴろげJavaEE DevOpsおっぴろげJavaEE DevOps
おっぴろげJavaEE DevOpsTaiichilow Nagase
 
Unit testing basics
Unit testing basicsUnit testing basics
Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023
Mark Niebergall
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Unit testing
Unit testingUnit testing
Unit testing
Arthur Purnama
 
Developer Tests - Things to Know
Developer Tests - Things to KnowDeveloper Tests - Things to Know
Developer Tests - Things to Know
Vaidas Pilkauskas
 
Why Your Test Suite Sucks - PHPCon PL 2015
Why Your Test Suite Sucks - PHPCon PL 2015Why Your Test Suite Sucks - PHPCon PL 2015
Why Your Test Suite Sucks - PHPCon PL 2015
CiaranMcNulty
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
Mike Lively
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
vilniusjug
 
Thinking Beyond ORM in JPA
Thinking Beyond ORM in JPAThinking Beyond ORM in JPA
Thinking Beyond ORM in JPA
Patrycja Wegrzynowicz
 
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Mike Schinkel
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
Peter Arato
 

Similar to Unit testing for WordPress (20)

WordPress Developer tools
WordPress Developer toolsWordPress Developer tools
WordPress Developer tools
 
JavaScript code generator with Yeoman
JavaScript code generator with YeomanJavaScript code generator with Yeoman
JavaScript code generator with Yeoman
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs Life
 
Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptx
 
Physical web
Physical webPhysical web
Physical web
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Hands On with Selenium and WebDriver
Hands On with Selenium and WebDriverHands On with Selenium and WebDriver
Hands On with Selenium and WebDriver
 
おっぴろげJavaEE DevOps
おっぴろげJavaEE DevOpsおっぴろげJavaEE DevOps
おっぴろげJavaEE DevOps
 
Unit testing basics
Unit testing basicsUnit testing basics
Unit testing basics
 
Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023Leveling Up With Unit Testing - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Developer Tests - Things to Know
Developer Tests - Things to KnowDeveloper Tests - Things to Know
Developer Tests - Things to Know
 
Why Your Test Suite Sucks - PHPCon PL 2015
Why Your Test Suite Sucks - PHPCon PL 2015Why Your Test Suite Sucks - PHPCon PL 2015
Why Your Test Suite Sucks - PHPCon PL 2015
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
 
Thinking Beyond ORM in JPA
Thinking Beyond ORM in JPAThinking Beyond ORM in JPA
Thinking Beyond ORM in JPA
 
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 

More from Sudar Muthu

A quick preview of WP CLI - Chennai WordPress Meetup
A quick preview of WP CLI - Chennai WordPress MeetupA quick preview of WP CLI - Chennai WordPress Meetup
A quick preview of WP CLI - Chennai WordPress Meetup
Sudar Muthu
 
Using arduino and raspberry pi for internet of things
Using arduino and raspberry pi for internet of thingsUsing arduino and raspberry pi for internet of things
Using arduino and raspberry pi for internet of things
Sudar Muthu
 
How arduino helped me in life
How arduino helped me in lifeHow arduino helped me in life
How arduino helped me in life
Sudar Muthu
 
Having fun with hardware
Having fun with hardwareHaving fun with hardware
Having fun with hardware
Sudar Muthu
 
Getting started with arduino workshop
Getting started with arduino workshopGetting started with arduino workshop
Getting started with arduino workshop
Sudar Muthu
 
Python in raspberry pi
Python in raspberry piPython in raspberry pi
Python in raspberry pi
Sudar Muthu
 
Hack 101 at IIT Kanpur
Hack 101 at IIT KanpurHack 101 at IIT Kanpur
Hack 101 at IIT Kanpur
Sudar Muthu
 
PureCSS open hack 2013
PureCSS open hack 2013PureCSS open hack 2013
PureCSS open hack 2013
Sudar Muthu
 
Pig workshop
Pig workshopPig workshop
Pig workshop
Sudar Muthu
 
Arduino Robotics workshop day2
Arduino Robotics workshop day2Arduino Robotics workshop day2
Arduino Robotics workshop day2
Sudar Muthu
 
Arduino Robotics workshop Day1
Arduino Robotics workshop Day1Arduino Robotics workshop Day1
Arduino Robotics workshop Day1
Sudar Muthu
 
Hands on Hadoop and pig
Hands on Hadoop and pigHands on Hadoop and pig
Hands on Hadoop and pig
Sudar Muthu
 
Lets make robots
Lets make robotsLets make robots
Lets make robots
Sudar Muthu
 
Capabilities of Arduino (including Due)
Capabilities of Arduino (including Due)Capabilities of Arduino (including Due)
Capabilities of Arduino (including Due)Sudar Muthu
 
Controlling robots using javascript
Controlling robots using javascriptControlling robots using javascript
Controlling robots using javascript
Sudar Muthu
 
Picture perfect hacks with flickr API
Picture perfect hacks with flickr APIPicture perfect hacks with flickr API
Picture perfect hacks with flickr API
Sudar Muthu
 
Hacking 101
Hacking 101Hacking 101
Hacking 101
Sudar Muthu
 
Capabilities of Arduino
Capabilities of ArduinoCapabilities of Arduino
Capabilities of Arduino
Sudar Muthu
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDD
Sudar Muthu
 
Using Javascript in today's world
Using Javascript in today's worldUsing Javascript in today's world
Using Javascript in today's world
Sudar Muthu
 

More from Sudar Muthu (20)

A quick preview of WP CLI - Chennai WordPress Meetup
A quick preview of WP CLI - Chennai WordPress MeetupA quick preview of WP CLI - Chennai WordPress Meetup
A quick preview of WP CLI - Chennai WordPress Meetup
 
Using arduino and raspberry pi for internet of things
Using arduino and raspberry pi for internet of thingsUsing arduino and raspberry pi for internet of things
Using arduino and raspberry pi for internet of things
 
How arduino helped me in life
How arduino helped me in lifeHow arduino helped me in life
How arduino helped me in life
 
Having fun with hardware
Having fun with hardwareHaving fun with hardware
Having fun with hardware
 
Getting started with arduino workshop
Getting started with arduino workshopGetting started with arduino workshop
Getting started with arduino workshop
 
Python in raspberry pi
Python in raspberry piPython in raspberry pi
Python in raspberry pi
 
Hack 101 at IIT Kanpur
Hack 101 at IIT KanpurHack 101 at IIT Kanpur
Hack 101 at IIT Kanpur
 
PureCSS open hack 2013
PureCSS open hack 2013PureCSS open hack 2013
PureCSS open hack 2013
 
Pig workshop
Pig workshopPig workshop
Pig workshop
 
Arduino Robotics workshop day2
Arduino Robotics workshop day2Arduino Robotics workshop day2
Arduino Robotics workshop day2
 
Arduino Robotics workshop Day1
Arduino Robotics workshop Day1Arduino Robotics workshop Day1
Arduino Robotics workshop Day1
 
Hands on Hadoop and pig
Hands on Hadoop and pigHands on Hadoop and pig
Hands on Hadoop and pig
 
Lets make robots
Lets make robotsLets make robots
Lets make robots
 
Capabilities of Arduino (including Due)
Capabilities of Arduino (including Due)Capabilities of Arduino (including Due)
Capabilities of Arduino (including Due)
 
Controlling robots using javascript
Controlling robots using javascriptControlling robots using javascript
Controlling robots using javascript
 
Picture perfect hacks with flickr API
Picture perfect hacks with flickr APIPicture perfect hacks with flickr API
Picture perfect hacks with flickr API
 
Hacking 101
Hacking 101Hacking 101
Hacking 101
 
Capabilities of Arduino
Capabilities of ArduinoCapabilities of Arduino
Capabilities of Arduino
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDD
 
Using Javascript in today's world
Using Javascript in today's worldUsing Javascript in today's world
Using Javascript in today's world
 

Recently uploaded

Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
Game Development with Unity3D (Game Development lecture 3)
Game Development  with Unity3D (Game Development lecture 3)Game Development  with Unity3D (Game Development lecture 3)
Game Development with Unity3D (Game Development lecture 3)
abdulrafaychaudhry
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 

Recently uploaded (20)

Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
Game Development with Unity3D (Game Development lecture 3)
Game Development  with Unity3D (Game Development lecture 3)Game Development  with Unity3D (Game Development lecture 3)
Game Development with Unity3D (Game Development lecture 3)
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 

Unit testing for WordPress

  • 1. Sudar Muthu | @sudarmuthu #WCPune Unit Testing For WordPress WordCamp Pune, 2015 Sudar Muthu http://sudarmuthu.com https://github.com/sudar
  • 2. Sudar Muthu | @sudarmuthu #WCPune Me • Programming in PHP for more than a decade and in WordPress for about 8 years. • Big fan of automating process and workflows. • Contributor to a couple of open source projects. • Remote worker at 10up (and yes 10up is hiring :) )
  • 3. Sudar Muthu | @sudarmuthu #WCPune What about you? • What is your typical development environment? • What is your experience with PHP and WordPress? • What is your experience with Unit Testing? • What are your expectations out of this talk?
  • 4. Sudar Muthu | @sudarmuthu #WCPune Unit Testing Credit: https://twitter.com/alistratov/status/599109195548459009
  • 5. Sudar Muthu | @sudarmuthu #WCPune Is there something wrong? class Sample { protected $value; public function initialise($value) { $this->value = $value; } public function execute() { if (!$this->value) { throw new Exception("value not set"); } return $value * 10; // business logic } } $sample = new Sample; $sample->execute();
  • 6. Sudar Muthu | @sudarmuthu #WCPune Finding bugs is not easy
  • 7. Sudar Muthu | @sudarmuthu #WCPune Write Tests
  • 8. Sudar Muthu | @sudarmuthu #WCPune Write Tests It sounds obvious but getting started is the hardest part!
  • 9. Sudar Muthu | @sudarmuthu #WCPune Different types of Testing • Functionality testing • Integration testing • Unit testing
  • 10. Sudar Muthu | @sudarmuthu #WCPune Different types of Testing • Functionality testing • Integration testing • Unit testing
  • 11. Sudar Muthu | @sudarmuthu #WCPune Briefly • What is PHPUnit? • Installing PHPUnit • Setting up folder structure • phpunit.xml
  • 12. Sudar Muthu | @sudarmuthu #WCPune Three steps in test cases • setup • act • verify
  • 13. Sudar Muthu | @sudarmuthu #WCPune Demo
  • 14. Sudar Muthu | @sudarmuthu #WCPune Writing our first test case public function test_execute_works_with_initialise() { // setup $sample = new Sample(); // act $sample->initialise(10); $return = $sample->execute(); // verify $this->assertEquals(100, $return); }
  • 15. Sudar Muthu | @sudarmuthu #WCPune Testing Exception /** * @expectedException Exception */ public function test_execute_needs_initialise() { // setup $sample = new Sample(); // act $sample->execute(); // verify // that it throws and exception }
  • 16. Sudar Muthu | @sudarmuthu #WCPune Let’s add more tests • Testing decimals • Testing with negative values • Testing it work with zero
  • 17. Sudar Muthu | @sudarmuthu #WCPune Unit Testing WordPress Code
  • 18. Sudar Muthu | @sudarmuthu #WCPune What should be tested? function my_permalink_function( $post_id ) { $permalink = get_permalink( absint( $post_id ) ); $permalink = apply_filters( 'special_filter', $permalink ); do_action( 'special_action', $permalink ); return $permalink; }
  • 19. Sudar Muthu | @sudarmuthu #WCPune Don’t test the WordPress built-in function
  • 20. Sudar Muthu | @sudarmuthu #WCPune Don’t test the WordPress built-in function Use Mocks instead https://github.com/10up/wp_mock
  • 21. Sudar Muthu | @sudarmuthu #WCPune Mocking WordPress function WP_Mock::wpFunction( 'get_permalink', array( 'args' => 42, 'times' => 1, 'return' => 'http://example.com/foo' ) );
  • 22. Sudar Muthu | @sudarmuthu #WCPune Mocking WordPress filter WP_Mock::onFilter( 'special_filter' ) ->with( 'http://example.com/foo' ) ->reply( 'https://example.com/bar' );
  • 23. Sudar Muthu | @sudarmuthu #WCPune Mocking WordPress action WP_Mock::expectAction( 'special_action', 'https://example.com/bar' );
  • 24. Sudar Muthu | @sudarmuthu #WCPune Putting it all together public function test_my_permalink_function() { WP_Mock::wpFunction( 'get_permalink', array( 'args' => 42, 'times' => 1, 'return' => 'http://example.com/foo' ) ); WP_Mock::wpPassthruFunction( 'absint', array( 'times' => 1 ) ); WP_Mock::onFilter( 'special_filter' ) ->with( 'http://example.com/foo' ) ->reply( 'https://example.com/bar' ); WP_Mock::expectAction( 'special_action', 'https://example.com/bar' ); $result = my_permalink_function( 42 ); $this->assertEquals( 'https://example.com/bar', $result ); }
  • 25. Sudar Muthu | @sudarmuthu #WCPune Some PHPUnit Tips • Have a fast test suite • Use Composer • Enable code coverage in reports • phpunit.xml.dist vs phpunit.xml • Use specific assertions • Check out Unit tests in WordPress core
  • 26. Sudar Muthu | @sudarmuthu #WCPune WordPress plugin skeleton with Tests Use yo generator https://github.com/10up/generator-wp-make
  • 27. Sudar Muthu | @sudarmuthu #WCPune Thank You @sudarmuthu http://sudarmuthu.com https://github.com/sudar