Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove testing code from plugin #117

Merged
merged 11 commits into from
Dec 21, 2017
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
"autoload-dev": {
"psr-4": {
"Yoast\\AcfAnalysis\\Tests\\": "tests/php/unit"
}
},
"files": [
"tests/js/system/data/test-data-loader.php"
]
},
"scripts": {
"post-install-cmd": [
Expand Down
19 changes: 0 additions & 19 deletions inc/class-ac-yoast-seo-acf-content-analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public function admin_init() {

$this->boot();

if ( defined( 'AC_YOAST_ACF_ANALYSIS_ENVIRONMENT' ) && 'development' === AC_YOAST_ACF_ANALYSIS_ENVIRONMENT ) {
$this->boot_dev();
}

$this->register_config_filters();

$assets = new Yoast_ACF_Analysis_Assets();
Expand Down Expand Up @@ -76,21 +72,6 @@ public function boot() {
$registry->add( 'config', $configuration );
}

/**
* Boots the plugin for dev environment.
*/
public function boot_dev() {
$registry = Yoast_ACF_Analysis_Facade::get_registry();
$configuration = $registry->get( 'config' );

$version = 4;
if ( version_compare( $configuration->get_acf_version(), 5, '>=' ) ) {
$version = 5;
}

require_once AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/tests/js/system/data/acf' . $version . '.php';
}

/**
* Filters the Scraper Configuration to add the headlines configuration for the text scraper.
*/
Expand Down
53 changes: 53 additions & 0 deletions tests/js/system/data/test-data-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* This file is loaded by Composer autoload-dev, and that happens before `add_action` is available.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to manually trigger the Monkey Brains setup method, to avoid having to use the ugly global WordPress mocking.

The following code snippet works as expected and removes all problems we have with the current approach.

// Load Monkey Brains WP mock methods.
Brain\Monkey\setUp();

add_action( 'admin_init', 'yoast_acf_analysis_test_data_loader', 11 );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this code isn't mocking add_action but actually adds a filter by messing with the globals before the plugin API is ready.

But I just realised that that isn't needed anyway because the autoloader only gets loaded after the Plugin API is ready anyway. So I can further simplify this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and that was there for a reason because of course when running Unit Tests the Plugin API isn't ready. Your alternative approach is nice though. I just wrapped it inside a conditional now because otherwise Brain\Monkey\setUp() is also called during normal execution which needlessly loads Patchwork.

* So we "manually" add in global `$wp_filter` the function that loads translations.
*/
global $wp_filter;

$hook_name = 'admin_init';
$function_name = 'yoast_acf_analysis_test_data_loader';

if ( ! function_exists( 'add_action' ) ) {

if ( ! is_array( $wp_filter ) ) {
$wp_filter = array();
}

if ( ! isset( $wp_filter[ $hook_name ] ) ) {
$wp_filter[ $hook_name ] = array();
}

if ( ! isset( $wp_filter[ $hook_name ][11] ) ) {
$wp_filter[ $hook_name ][11] = array();
}

$wp_filter[ $hook_name ][11][ $function_name ] = array(
'function' => $function_name,
'accepted_args' => 1,
);

}else{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS: Missing spaces around the else statement.


add_action( $hook_name, $function_name, 11 );

}

function yoast_acf_analysis_test_data_loader() {

if ( defined( 'AC_YOAST_ACF_ANALYSIS_ENVIRONMENT' ) && 'development' === AC_YOAST_ACF_ANALYSIS_ENVIRONMENT ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer to have a negated condition check and do a return instead of encapsulating this piece of code with the condition indentation.


$registry = Yoast_ACF_Analysis_Facade::get_registry();
$configuration = $registry->get( 'config' );

$version = 4;
if ( version_compare( $configuration->get_acf_version(), 5, '>=' ) ) {
$version = 5;
}

require_once AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/tests/js/system/data/acf' . $version . '.php';

}

}
16 changes: 11 additions & 5 deletions yoast-acf-analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
define( 'AC_SEO_ACF_ANALYSIS_PLUGIN_NAME', untrailingslashit( plugin_basename( __FILE__ ) ) );
}

if ( is_file( AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/vendor/autoload_52.php' ) ) {
require AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/vendor/autoload_52.php';

$ac_yoast_seo_acf_analysis = new AC_Yoast_SEO_ACF_Content_Analysis();
$ac_yoast_seo_acf_analysis->init();
if ( version_compare( PHP_VERSION, '5.3.2', '>=' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be less complicated if you can create a variable to hold the file to be loaded.
Defaulting to vendor/autoload.php and making an exception to overwrite with the autoload_52.php version.

After that we only have to use one is_file and require call with that same variable.

if ( is_file( AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/vendor/autoload.php' ) ) {
require AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/vendor/autoload.php';
}
} else {
if ( is_file( AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/vendor/autoload_52.php' ) ) {
require AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/vendor/autoload_52.php';
}
}

/**
Expand All @@ -57,4 +60,7 @@ function yoast_acf_analysis_load_textdomain() {
'admin_notices',
create_function( '', "echo '<div class=\"error\"><p>$message</p></div>';" )
);
} else {
$ac_yoast_seo_acf_analysis = new AC_Yoast_SEO_ACF_Content_Analysis();
$ac_yoast_seo_acf_analysis->init();
}