Skip to content

Commit 7a7d145

Browse files
authored
Merge pull request #335 from ernilambar/190-plugin-bootstrap
2 parents e4be9fe + f9a0de9 commit 7a7d145

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

features/scaffold-plugin-tests.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,28 @@ Feature: Scaffold plugin unit tests
284284
"""
285285
bootstrap.php
286286
"""
287+
288+
Scenario: Scaffold plugin tests with custom main file
289+
Given a WP install
290+
And a wp-content/plugins/foo/bar.php file:
291+
"""
292+
<?php
293+
/**
294+
* Plugin Name: Foo
295+
* Plugin URI: https://example.com
296+
* Description: Foo desctiption
297+
* Author: John Doe
298+
* Author URI: https://example.com
299+
* Text Domain: foo
300+
* Domain Path: /languages
301+
* Version: 0.1.0
302+
*
303+
* @package Foo
304+
*/
305+
"""
306+
307+
When I run `wp scaffold plugin-tests foo`
308+
Then the wp-content/plugins/foo/tests/bootstrap.php file should contain:
309+
"""
310+
require dirname( dirname( __FILE__ ) ) . '/bar.php';
311+
"""

src/Scaffold_Command.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,33 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) {
869869
$wp_versions_to_test[] = 'latest';
870870
$wp_versions_to_test[] = 'trunk';
871871

872+
$main_file = "{$slug}.php";
873+
874+
if ( 'plugin' === $type ) {
875+
if ( ! function_exists( 'get_plugins' ) ) {
876+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
877+
}
878+
879+
$all_plugins = get_plugins();
880+
881+
if ( ! empty( $all_plugins ) ) {
882+
$filtered = array_filter(
883+
array_keys( $all_plugins ),
884+
static function ( $item ) use ( $slug ) {
885+
return ( false !== strpos( $item, "{$slug}/" ) );
886+
}
887+
);
888+
889+
if ( ! empty( $filtered ) ) {
890+
$main_file = basename( reset( $filtered ) );
891+
}
892+
}
893+
}
894+
872895
$template_data = [
873-
"{$type}_slug" => $slug,
874-
"{$type}_package" => $package,
896+
"{$type}_slug" => $slug,
897+
"{$type}_package" => $package,
898+
"{$type}_main_file" => $main_file,
875899
];
876900

877901
$force = Utils\get_flag_value( $assoc_args, 'force' );

templates/plugin-bootstrap.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ require_once "{$_tests_dir}/includes/functions.php";
2929
* Manually load the plugin being tested.
3030
*/
3131
function _manually_load_plugin() {
32-
require dirname( dirname( __FILE__ ) ) . '/{{plugin_slug}}.php';
32+
require dirname( dirname( __FILE__ ) ) . '/{{plugin_main_file}}';
3333
}
3434

3535
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

0 commit comments

Comments
 (0)