Created
September 18, 2014 19:08
-
-
Save matgargano/bebedbacedd7a85d8dc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Unit Testing Demo Plugin | |
Plugin URI: http://example.com | |
Description: Let's get started unit testing | |
Author: Me | |
Version: 0.1 | |
Author URI: http://example.com | |
*/ | |
class Demo_Plugin { | |
protected $listing; | |
protected $slug; | |
protected $term_title; | |
protected $term_object; | |
protected $post_id; | |
protected $taxonomy = 'category'; | |
function init() { | |
$this->attach_hooks(); | |
} | |
function attach_hooks() { | |
add_action( 'save_post', array( $this, 'categorize_post' ), 10, 10 ); | |
} | |
function categorize_post( $post_id ) { | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { | |
return; | |
} | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return; | |
} | |
if ( false !== wp_is_post_revision( $post_id ) ) { | |
return; | |
} | |
$this->post_id = $post_id; | |
$title = get_the_title( $post_id ); | |
$this->get_listing(); | |
foreach ( $this->listing as $term_title => $slug ) { | |
if ( strstr( $title, $term_title ) ) { | |
$this->slug = $slug; | |
$this->term_title = $term_title; | |
$this->process_term(); | |
} | |
} | |
} | |
function process_term() { | |
$this->term_object = get_term_by( 'slug', $this->slug, $this->taxonomy ); | |
if ( ! $this->term_object ) { | |
$this->create_term(); | |
} | |
if ( is_object( $this->term_object ) ) { | |
wp_set_object_terms( $this->post_id, $this->term_object->term_id, $this->taxonomy, true ); | |
} | |
} | |
function create_term() { | |
$this->term_object = wp_insert_term( $this->term_title, $this->taxonomy, array( 'slug' => $this->slug ) ); | |
} | |
function get_listing() { | |
$request = wp_remote_get( 'http://myapi.com/apiendpoint' ); | |
$this->listing = json_decode( wp_remote_retrieve_body( $request ) ); | |
} | |
} | |
if ( ! defined( WP_UNIT_TEST_ENVIRONMENT ) || ! WP_UNIT_TEST_ENVIRONMENT ) { | |
add_action( 'init', array( new Demo_Plugin, 'init' ) ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Demo_Test extends WP_UnitTestCase { | |
function test_init() { | |
$demo_plugin = $this->getMockBuilder( 'Demo_Plugin' ) | |
->disableOriginalConstructor() | |
->setMethods( array( 'attach_hooks' ) ) | |
->getMock(); | |
$demo_plugin->expects( $this->once() ) | |
->method( 'attach_hooks' ); | |
$demo_plugin->init(); | |
} | |
function test_attach_hooks() { | |
$demo_plugin = new Demo_Plugin; | |
$demo_plugin->attach_hooks(); | |
$this->assertEquals( 10, has_action( 'save_post', array( $demo_plugin, 'categorize_post' ) ) ); | |
} | |
function provider_test_categorize_post() { | |
return array( | |
array( 'Matt Harvey plays catch', 1 ), | |
array( 'Everyone Happy that Matt Harvey has successful Bullpen Session', 1 ), | |
array( 'Mets Lose, Daniel Murphy Shoulders the Blame', 1 ), | |
array( 'Daniel Murphy and Matt Harvey Headline Mets Charity', 2 ), | |
array( 'iON offers Mets fans a view never seen before', 0 ), | |
); | |
} | |
/** | |
* @dataProvider provider_test_categorize_post | |
*/ | |
function test_categorize_post( $title, $process_term_expects ) { | |
$post_id = $this->factory->post->create( array( 'post_title' => $title ) ); | |
$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); | |
wp_set_current_user( $user_id ); | |
$demo_plugin = $this->getMockBuilder( 'Demo_Plugin' ) | |
->disableOriginalConstructor() | |
->setMethods( array( 'process_term', 'get_listing' ) ) | |
->getMock(); | |
$reflection = new ReflectionClass( $demo_plugin ); | |
$reflection_property = $reflection->getProperty( 'listing' ); | |
$reflection_property->setAccessible( true ); | |
$reflection_property->setValue( $demo_plugin, array( 'Matt Harvey' => 'matt-harvey', 'Daniel Murphy' => 'daniel-murphy' ) ); | |
$demo_plugin->expects( $this->exactly( $process_term_expects ) ) | |
->method( 'process_term' ); | |
$demo_plugin->expects( $this->once() ) | |
->method( 'get_listing' ) | |
->will( $this->returnValue( array( 'Matt Harvey' => 'matt-harvey', 'Daniel Murphy' => 'daniel-murphy' ) ) ); | |
$demo_plugin->categorize_post( $post_id ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment