Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A very (very, very) simple unittest for WordPress
<?php
/*
Plugin Name: WordPressTests
Plugin URI:
Description: Automated testing of WordPress
Version: 0.0.1-dev01
Author: Ralf Albert
Author URI: http://yoda.neun12.de
*/
if( ! class_exists( 'TestPlugin' ) ){
add_action( 'plugins_loaded', array( 'TestPlugin', 'plugin_start' ) );
class TestPlugin
{
private static $plugin_self = null;
private $errors = array();
public $tests = array( 'TestScriptsEnqueued', 'StylesPrinted' );
public function plugin_start(){
if( null === self::$plugin_self )
self::$plugin_self = new self;
return self::$plugin_self;
}
public function __construct(){
// allow to add&modify the list of tests to run
$this->tests = apply_filters( 'tests_to_run', $this->tests );
// Captain Hook
add_action( 'shutdown', array( &$this, 'run_tests' ) );
add_action( 'finish_tests', array( &$this, '_output' ) );
self::$plugin_self = &$this;
}
public function run_tests(){
if( empty( $this->tests ) )
return false;
foreach( $this->tests as $test ){
if( class_exists( $test ) ){
$errors = array();
$e = new $test();
$errors = $e->get_results();
if( ! empty( $errors ) )
$this->errors = array_merge( $this->errors, $errors );
}
}
do_action( 'finish_tests' );
}
public function _output(){
if( empty( $this->errors ) )
return false;
echo '<div class="testoutput">';
echo '<h1>Errors from Tests</h1>';
echo 'Beginn output<hr />';
foreach( $this->errors as $error )
printf( '%s<br />', $error );
echo 'End output<hr />';
echo '</div>';
return true;
}
} // end_class_TestPlugin
/* ---------------------------------------------------------------------- *
* ----- test classes --------------------------------------------------- *
* ---------------------------------------------------------------------- */
class TestScriptsEnqueued
{
protected $errors = array();
public function __construct(){
$test_class = new MyClass_To_Test();
$test_class->enqueue_scripts();
$this->check_if_scripts_are_enqueued( array( 'myJavaScript' ) );
}
public function check_if_scripts_are_enqueued( array $scripts_to_test ){
$scripts_to_test = apply_filters( 'scripts_to_test', $scripts_to_test );
global $wp_scripts;
if( ! is_a( $wp_scripts, 'WP_Scripts' ) )
$this->wp_scripts = new WP_Scripts();
else
$this->wp_scripts = $wp_scripts;
$registered_scripts = $this->wp_scripts->registered;
// check if script was registered (enqueued)
foreach( $scripts_to_test as $test_script ){
if( key_exists( $test_script, $registered_scripts ) ){
$this->check_if_file_exists( $registered_scripts[$test_script] );
}
else{
$this->errors[] = sprintf( '<strong>%s</strong> was not registered', $test_script );
}
}
}
public function get_results(){
return $this->errors;
}
protected function check_if_file_exists( $script ){
// check file exists
$src = $script->src;
$f = @fopen( $src, 'r' );
if( ! $f )
$this->errors[] = sprintf( 'Can not open <strong>%s</strong>', $src );
else
fclose( $f );
}
}
/* ---------------------------------------------------------------------- *
* ----- class to test -------------------------------------------------- *
* ---------------------------------------------------------------------- */
class MyClass_To_Test
{
public function __construct(){
// do some init
}
public function enqueue_scripts(){
wp_enqueue_script( 'myJavaScript', plugins_url( 'js/myjs.js', __FILE__ ), false, false, true );
}
}
} // end_if_class_exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.