Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Unit testing for WordPress

4,690 views

Published on

Unit testing for WordPress

Published in: Software
  • Be the first to comment

Unit testing for WordPress

  1. 1. Sudar Muthu | @sudarmuthu #WCPune Unit Testing For WordPress WordCamp Pune, 2015 Sudar Muthu http://sudarmuthu.com https://github.com/sudar
  2. 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. 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. 4. Sudar Muthu | @sudarmuthu #WCPune Unit Testing Credit: https://twitter.com/alistratov/status/599109195548459009
  5. 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. 6. Sudar Muthu | @sudarmuthu #WCPune Finding bugs is not easy
  7. 7. Sudar Muthu | @sudarmuthu #WCPune Write Tests
  8. 8. Sudar Muthu | @sudarmuthu #WCPune Write Tests It sounds obvious but getting started is the hardest part!
  9. 9. Sudar Muthu | @sudarmuthu #WCPune Different types of Testing • Functionality testing • Integration testing • Unit testing
  10. 10. Sudar Muthu | @sudarmuthu #WCPune Different types of Testing • Functionality testing • Integration testing • Unit testing
  11. 11. Sudar Muthu | @sudarmuthu #WCPune Briefly • What is PHPUnit? • Installing PHPUnit • Setting up folder structure • phpunit.xml
  12. 12. Sudar Muthu | @sudarmuthu #WCPune Three steps in test cases • setup • act • verify
  13. 13. Sudar Muthu | @sudarmuthu #WCPune Demo
  14. 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. 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. 16. Sudar Muthu | @sudarmuthu #WCPune Let’s add more tests • Testing decimals • Testing with negative values • Testing it work with zero
  17. 17. Sudar Muthu | @sudarmuthu #WCPune Unit Testing WordPress Code
  18. 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. 19. Sudar Muthu | @sudarmuthu #WCPune Don’t test the WordPress built-in function
  20. 20. Sudar Muthu | @sudarmuthu #WCPune Don’t test the WordPress built-in function Use Mocks instead https://github.com/10up/wp_mock
  21. 21. Sudar Muthu | @sudarmuthu #WCPune Mocking WordPress function WP_Mock::wpFunction( 'get_permalink', array( 'args' => 42, 'times' => 1, 'return' => 'http://example.com/foo' ) );
  22. 22. Sudar Muthu | @sudarmuthu #WCPune Mocking WordPress filter WP_Mock::onFilter( 'special_filter' ) ->with( 'http://example.com/foo' ) ->reply( 'https://example.com/bar' );
  23. 23. Sudar Muthu | @sudarmuthu #WCPune Mocking WordPress action WP_Mock::expectAction( 'special_action', 'https://example.com/bar' );
  24. 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. 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. 26. Sudar Muthu | @sudarmuthu #WCPune WordPress plugin skeleton with Tests Use yo generator https://github.com/10up/generator-wp-make
  27. 27. Sudar Muthu | @sudarmuthu #WCPune Thank You @sudarmuthu http://sudarmuthu.com https://github.com/sudar

×