Sudar Muthu | @sudarmuthu #WCPune Unit Testing For WordPress WordCamp Pune, 2015 Sudar Muthu http://sudarmuthu.com https://github.com/sudar
Sudar Muthu | @sudarmuthu #WCPune
Unit Testing For WordPress
WordCamp Pune, 2015Sudar Muthu
http://sudarmuthu.comhttps://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 TestingCredit: 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 TestsIt 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 casepublic 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 togetherpublic 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