You will need:
- A local install of WordPress
- PHPUnit installed
- WordPress Test Suite installed
- This repo cloned to your plugins folder
I recommend using Vagrant and Varying Vagrant Vagrants as this will take care of a local WordPress install, PHPUnit and the WordPress test suite.
Alternatively, you can follow these instructions on WordPress.Org
To test that you have everything ready to go, cd into this plugin's directory, on my local install this would be:
cd /srv/www/wordpress-default/public_html/wp-content/plugins/wordcamp-tdd
and run:
phpunit tests/class-test-install.php
All being well you will see a cheesy message from me and: OK (1 test, 1 assertion)
$user_id = $this->factory->user->create();
You can insert a user with defined values. See WordPress Codex for available params.
$user_id = $this->factory->user->create( [
'display_name' => 'Matt',
] );
$post_id = $this->factory->post->create();
See WordPress Codex for available params.
$this->factory->post->create( [
'post_status' => 'publish',
'post_author' => $user_id,
] );
See WordPress Codex for available params.
$this->factory->post->update_object( $post_id, [
'post_status' => 'publish',
'post_author' => $user_id,
] );
See WP Unit Factories Source Code for more examples.
$this->assertEquals(
$expected_value,
$actual_value,
'A relevant message about the test being run that displays if the test fails'
);
/**
* DataProvider for test_value_match.
*
* @return array $value_one, $value_two, $expects.
*/
public function our_data_provider() {
return [
[ 1, 1, true ],
[ 1, 2, false ],
[ 1, '1', false ],
];
}
/**
* Tests with a DataProvider.
*
* @param mixed $value_one First value
* @param mixed $value_two Value to compare with first value.
* @param boolean $expects If values match.
* @dataProvider our_data_provider
*/
public function test_value_match( $value_one, $value_two, $expects ) {
$this->assertSame(
$expects,
$value_one === $value_two,
'Values not being correctly evaluated.'
);
}
// Create a DateTime object.
$datetime = new \DateTime( '2018-10-31' );
// Double class.
$example_two_completed = $this->getMockBuilder( __NAMESPACE__ . '\\Example_Two_Completed' )
->setMethods( [ 'get_datetime' ] )
->disableOriginalConstructor()
->getMock();
// Return our date object.
$example_two_completed->expects( $this->once() )
->method( 'get_datetime' )
->will( $this->returnValue( $datetime ) );