Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Last active May 16, 2022 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingo-d/36c5a20ae8992c87fcf6d67165307f0f to your computer and use it in GitHub Desktop.
Save dingo-d/36c5a20ae8992c87fcf6d67165307f0f to your computer and use it in GitHub Desktop.
Example of WP unit tests with Pest. Doesn't include the entire setup, for that you can check https://github.com/dingo-d/wp-pest-integration-test-setup
<?php
use TestPlugin\Cpt;
// Unit test with OOP code.
it('checks that the hook is registered for custom post type registration', function() {
$this->stubTranslationFunctions();
(new RegisterBookCpt())->register();
// We use Brain Monkey lib:
// https://giuseppe-mazzapica.gitbook.io/brain-monkey/wordpress-specific-tools/wordpress-hooks-added
$this->assertNotFalse(has_action('init', [RegisterBookCpt::class, 'test_plugin_register_books_cpt']));
});
<?php
// Unit test with procedural code.
it('checks that the hook is registered for custom post type registration', function() {
$this->stubTranslationFunctions();
// Assume that our tests are in a 'tests' directory in the root of the plugin
// And inside a 'Unit' folder. We need to manually include our code if we don't use autoloader.
require dirname(__DIR__, 2) . '/test-plugin-procedural.php';
// We use Brain Monkey lib:
// https://giuseppe-mazzapica.gitbook.io/brain-monkey/wordpress-specific-tools/wordpress-hooks-added
$this->assertNotFalse(has_action('init', 'test_plugin_register_books_cpt'));
});
<?php
use Yoast\WPTestUtils\BrainMonkey\TestCase;
uses()->group('integration')->in('Integration');
uses()->group('unit')->in('Unit');
uses(TestCase::class)->in('Unit', 'Integration');
<?php
namespace TestPlugin\Cpt;
class RegisterBookCpt {
public function register() {
\add_action( 'init', [$this, 'registerBooksCpt'] );
}
public function registerBooksCpt() {
$args = array(
'label' => esc_html__( 'Books', 'test-plugin' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'book', $args );
}
}
<?php
/**
* Plugin Name: Test plugin
* Desctiption: Test plugin
* Version: 1.0.0
* License: MIT
*/
function test_plugin_register_books_cpt() {
$args = array(
'label' => esc_html__( 'Books', 'test-plugin' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'test_plugin_register_books_cpt' );
<?php
namespace TestPlugin;
use TestPlugin\Cpt;
/**
* Plugin Name: Test plugin class
* Desctiption: Test plugin but with class instead of procedural code
* Version: 1.0.0
* License: MIT
*/
require_once dirname(__FILE__) . '/vendor/autoload.php';
// Same as previous example but in class context. We are using PSR-4 autoloading.
(new RegisterBookCpt())->register();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment