Last active
February 26, 2016 19:10
-
-
Save ryanwelcher/680e1f767beacd5a2e00 to your computer and use it in GitHub Desktop.
Core Unit Test Example
This file contains 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 | |
/** | |
* | |
* | |
*/ | |
Tests_Component_FunctionInCamelCase extends WP_UnitTestCase { | |
/** | |
* Use this to create items that are needed for every test. | |
* This is run before each test. | |
* | |
* Optional. | |
*/ | |
function setUp() {} | |
/** | |
* Use this to reset the test environment | |
* This is run after each test. | |
* | |
* Optional. | |
*/ | |
function tearDown() {} | |
function test_desciption_of_what_is_expected() { | |
// 1: Do any setup needed for this specific test. | |
$user = $this->factory()->user->create_and_get( array( | |
'display_name' => 'John Doe', | |
) ); | |
$post = $this->factory()->post->create_and_get( array( | |
'post_author' => $user->ID, | |
'post_title' => 'Hello World', | |
)); | |
add_filter( 'the_title', array( $this, 'filter_prepend_to_the_title') ); | |
// 2: Get the data we need to run the assertions. | |
$title = get_the_title( $post->ID ); | |
// 3: Run any cleanup needed so we don't interfere with other tests. | |
remove_filter( 'the_title', array( $this, 'filter_prepend_to_the_title') ); | |
// 4: Run the assertions. We do this after cleanup because the test will stop running if the assertion fails. | |
$this->assertSame( 'Hello World with tests', $title ); | |
} | |
/** | |
* Filter callback for the test above | |
* @param $title | |
* | |
* @return string | |
*/ | |
function filter_prepend_to_the_title( $title ) { | |
return $title . ' with tests'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment