Top Banner
Sudar Muthu | @sudarmuthu #WCPune Unit Testing For WordPress WordCamp Pune, 2015 Sudar Muthu http://sudarmuthu.com https://github.com/sudar
27

Unit testing for WordPress

Jan 13, 2017

Download

Software

Sudar Muthu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Unit Testing For WordPress

WordCamp Pune, 2015Sudar Muthu

http://sudarmuthu.comhttps://github.com/sudar

Page 2: Unit testing for WordPress

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 :) )

Page 3: Unit testing for WordPress

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?

Page 4: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Unit TestingCredit: https://twitter.com/alistratov/status/599109195548459009

Page 5: Unit testing for WordPress

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();

Page 6: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Finding bugs is not easy

Page 7: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Write Tests

Page 8: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Write TestsIt sounds obvious but getting started is the hardest part!

Page 9: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Different types of Testing

• Functionality testing

• Integration testing

• Unit testing

Page 10: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Different types of Testing

• Functionality testing

• Integration testing

• Unit testing

Page 11: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Briefly

• What is PHPUnit?

• Installing PHPUnit

• Setting up folder structure

• phpunit.xml

Page 12: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Three steps in test cases

• setup

• act

• verify

Page 13: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Demo

Page 14: Unit testing for WordPress

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);}

Page 15: Unit testing for WordPress

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}

Page 16: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Let’s add more tests

• Testing decimals

• Testing with negative values

• Testing it work with zero

Page 17: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Unit Testing WordPress Code

Page 18: Unit testing for WordPress

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;}

Page 19: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Don’t test the WordPress built-in function

Page 20: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Don’t test the WordPress built-in function

Use Mocks instead https://github.com/10up/wp_mock

Page 21: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Mocking WordPress function

\WP_Mock::wpFunction( 'get_permalink', array( 'args' => 42, 'times' => 1, 'return' => 'http://example.com/foo') );

Page 22: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Mocking WordPress filter

\WP_Mock::onFilter( 'special_filter' ) ->with( 'http://example.com/foo' ) ->reply( 'https://example.com/bar' );

Page 23: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Mocking WordPress action

\WP_Mock::expectAction( 'special_action', 'https://example.com/bar');

Page 24: Unit testing for WordPress

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 );}

Page 25: Unit testing for WordPress

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

Page 26: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

WordPress plugin skeleton with Tests

Use yo generator https://github.com/10up/generator-wp-make

Page 27: Unit testing for WordPress

Sudar Muthu | @sudarmuthu #WCPune

Thank You@sudarmuthu

http://sudarmuthu.com https://github.com/sudar