Skip to content

Commit

Permalink
Merge pull request #335 from ernilambar/190-plugin-bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Apr 26, 2024
2 parents e4be9fe + f9a0de9 commit 7a7d145
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
25 changes: 25 additions & 0 deletions features/scaffold-plugin-tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,28 @@ Feature: Scaffold plugin unit tests
"""
bootstrap.php
"""

Scenario: Scaffold plugin tests with custom main file
Given a WP install
And a wp-content/plugins/foo/bar.php file:
"""
<?php
/**
* Plugin Name: Foo
* Plugin URI: https://example.com
* Description: Foo desctiption
* Author: John Doe
* Author URI: https://example.com
* Text Domain: foo
* Domain Path: /languages
* Version: 0.1.0
*
* @package Foo
*/
"""

When I run `wp scaffold plugin-tests foo`
Then the wp-content/plugins/foo/tests/bootstrap.php file should contain:
"""
require dirname( dirname( __FILE__ ) ) . '/bar.php';
"""
28 changes: 26 additions & 2 deletions src/Scaffold_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,33 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) {
$wp_versions_to_test[] = 'latest';
$wp_versions_to_test[] = 'trunk';

$main_file = "{$slug}.php";

if ( 'plugin' === $type ) {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$all_plugins = get_plugins();

if ( ! empty( $all_plugins ) ) {
$filtered = array_filter(
array_keys( $all_plugins ),
static function ( $item ) use ( $slug ) {
return ( false !== strpos( $item, "{$slug}/" ) );
}
);

if ( ! empty( $filtered ) ) {
$main_file = basename( reset( $filtered ) );
}
}
}

$template_data = [
"{$type}_slug" => $slug,
"{$type}_package" => $package,
"{$type}_slug" => $slug,
"{$type}_package" => $package,
"{$type}_main_file" => $main_file,
];

$force = Utils\get_flag_value( $assoc_args, 'force' );
Expand Down
2 changes: 1 addition & 1 deletion templates/plugin-bootstrap.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require_once "{$_tests_dir}/includes/functions.php";
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/{{plugin_slug}}.php';
require dirname( dirname( __FILE__ ) ) . '/{{plugin_main_file}}';
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
Expand Down

0 comments on commit 7a7d145

Please sign in to comment.