diff --git a/features/scaffold-block.feature b/features/scaffold-block.feature index 27874f47..7e2aa9c6 100644 --- a/features/scaffold-block.feature +++ b/features/scaffold-block.feature @@ -59,7 +59,11 @@ Feature: WordPress block code scaffolding """ And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain: """ - register_block_type( 'movies/the-green-mile', [ + register_block_type( + """ + And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain: + """ + 'movies/the-green-mile', """ And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain: """ diff --git a/features/scaffold-lint.feature b/features/scaffold-lint.feature new file mode 100644 index 00000000..9eff6057 --- /dev/null +++ b/features/scaffold-lint.feature @@ -0,0 +1,85 @@ +Feature: Lint scaffolded code + + Background: + Given a WP install + And I run `wp plugin path` + And save STDOUT as {PLUGIN_DIR} + + # Create a helper plugin to install phpcs once for all scenarios + When I run `wp scaffold plugin phpcs-helper --skip-tests` + Then the return code should be 0 + + # Install coding standards + When I run `composer config --working-dir={PLUGIN_DIR}/phpcs-helper allow-plugins.dealerdirect/phpcodesniffer-composer-installer true` + Then the return code should be 0 + + When I run `composer require --dev --working-dir={PLUGIN_DIR}/phpcs-helper dealerdirect/phpcodesniffer-composer-installer wp-coding-standards/wpcs --no-interaction --quiet` + Then the return code should be 0 + + Scenario: Scaffold plugin and lint it + When I run `wp scaffold plugin test-plugin` + Then STDOUT should not be empty + And the {PLUGIN_DIR}/test-plugin/test-plugin.php file should exist + And the {PLUGIN_DIR}/test-plugin/.phpcs.xml.dist file should exist + + When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {PLUGIN_DIR}/test-plugin/test-plugin.php` + Then the return code should be 0 + + Scenario: Scaffold post-type and lint it + When I run `wp theme install twentytwentyone --activate` + And I run `wp eval 'echo STYLESHEETPATH;'` + And save STDOUT as {STYLESHEETPATH} + + And I run `wp scaffold post-type movie --theme` + Then STDOUT should not be empty + And the {STYLESHEETPATH}/post-types/movie.php file should exist + + When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {STYLESHEETPATH}/post-types/movie.php` + Then the return code should be 0 + + Scenario: Scaffold taxonomy and lint it + When I run `wp theme install twentytwentyone --activate` + And I run `wp eval 'echo STYLESHEETPATH;'` + And save STDOUT as {STYLESHEETPATH} + + And I run `wp scaffold taxonomy genre --theme` + Then STDOUT should not be empty + And the {STYLESHEETPATH}/taxonomies/genre.php file should exist + + When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {STYLESHEETPATH}/taxonomies/genre.php` + Then the return code should be 0 + + Scenario: Scaffold plugin tests and lint them + When I run `wp scaffold plugin test-plugin` + Then STDOUT should not be empty + And the {PLUGIN_DIR}/test-plugin/tests directory should exist + And the {PLUGIN_DIR}/test-plugin/tests/bootstrap.php file should exist + And the {PLUGIN_DIR}/test-plugin/tests/test-sample.php file should exist + + # Run phpcs on the test files + When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {PLUGIN_DIR}/test-plugin/tests/bootstrap.php {PLUGIN_DIR}/test-plugin/tests/test-sample.php` + Then the return code should be 0 + + Scenario: Scaffold child theme and lint it + When I run `wp theme install twentytwentyone --activate` + And I run `wp theme path` + And save STDOUT as {THEME_DIR} + + And I run `wp scaffold child-theme test-child --parent_theme=twentytwentyone` + Then STDOUT should not be empty + And the {THEME_DIR}/test-child/functions.php file should exist + + When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {THEME_DIR}/test-child/functions.php` + Then the return code should be 0 + + Scenario: Scaffold block and lint it + When I run `wp scaffold plugin movies` + And I run `wp plugin path movies --dir` + And save STDOUT as {MOVIES_DIR} + + And I run `wp scaffold block the-green-mile --plugin=movies` + Then STDOUT should not be empty + And the {MOVIES_DIR}/blocks/the-green-mile.php file should exist + + When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {MOVIES_DIR}/blocks/the-green-mile.php` + Then the return code should be 0 diff --git a/features/scaffold-plugin-tests.feature b/features/scaffold-plugin-tests.feature index 95fee4ca..8b3e7a28 100644 --- a/features/scaffold-plugin-tests.feature +++ b/features/scaffold-plugin-tests.feature @@ -21,7 +21,7 @@ Feature: Scaffold plugin unit tests """ And the {PLUGIN_DIR}/hello-world/tests/bootstrap.php file should contain: """ - require dirname( dirname( __FILE__ ) ) . '/hello-world.php'; + require dirname( __DIR__ ) . '/hello-world.php'; """ And the {PLUGIN_DIR}/hello-world/tests/bootstrap.php file should contain: """ @@ -302,7 +302,7 @@ Feature: Scaffold plugin unit tests 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'; + require dirname( __DIR__ ) . '/bar.php'; """ Scenario: Accept bitbucket as valid CI in plugin scaffold diff --git a/features/scaffold.feature b/features/scaffold.feature index 57ee8f88..272e625b 100644 --- a/features/scaffold.feature +++ b/features/scaffold.feature @@ -432,7 +432,7 @@ Feature: WordPress code scaffolding And the wp-content/mu-plugins/custom-plugin/tests/bootstrap.php file should exist And the wp-content/mu-plugins/custom-plugin/tests/bootstrap.php file should contain: """ - require dirname( dirname( __FILE__ ) ) . '/custom-plugin.php'; + require dirname( __DIR__ ) . '/custom-plugin.php'; """ Scenario: Scaffold tests for a plugin with a different slug than plugin directory @@ -456,7 +456,7 @@ Feature: WordPress code scaffolding And the wp-content/mu-plugins/custom-plugin2/tests/bootstrap.php file should exist And the wp-content/mu-plugins/custom-plugin2/tests/bootstrap.php file should contain: """ - require dirname( dirname( __FILE__ ) ) . '/custom-plugin-slug.php'; + require dirname( __DIR__ ) . '/custom-plugin-slug.php'; """ Scenario: Scaffold tests parses plugin readme.txt diff --git a/src/Scaffold_Command.php b/src/Scaffold_Command.php index e92a032e..6ff95389 100644 --- a/src/Scaffold_Command.php +++ b/src/Scaffold_Command.php @@ -182,7 +182,9 @@ private function scaffold( $slug, $assoc_args, $defaults, $subdir, $templates ) $target_slug = ''; - if ( false !== $control_args['theme'] ) { + if ( true === $control_args['theme'] ) { + $target_slug = get_stylesheet(); + } elseif ( false !== $control_args['theme'] ) { $target_slug = $control_args['theme']; } elseif ( false !== $control_args['plugin'] ) { $target_slug = $control_args['plugin']; diff --git a/templates/block-php.mustache b/templates/block-php.mustache index ac589105..f8581380 100644 --- a/templates/block-php.mustache +++ b/templates/block-php.mustache @@ -18,7 +18,7 @@ function {{machine_name}}_block_init() { return; } {{#plugin}} - $dir = dirname( __FILE__ ); + $dir = __DIR__; {{/plugin}} {{#theme}} $dir = get_stylesheet_directory() . '/blocks'; @@ -33,12 +33,13 @@ function {{machine_name}}_block_init() { {{#theme}} get_stylesheet_directory_uri() . "/blocks/{$index_js}", {{/theme}} - [ + array( 'wp-blocks', 'wp-i18n', 'wp-element', - ], - filemtime( "{$dir}/{$index_js}" ) + ), + filemtime( "{$dir}/{$index_js}" ), + false ); $editor_css = '{{slug}}/editor.css'; @@ -50,7 +51,7 @@ function {{machine_name}}_block_init() { {{#theme}} get_stylesheet_directory_uri() . "/blocks/{$editor_css}", {{/theme}} - [], + array(), filemtime( "{$dir}/{$editor_css}" ) ); @@ -63,15 +64,18 @@ function {{machine_name}}_block_init() { {{#theme}} get_stylesheet_directory_uri() . "/blocks/{$style_css}", {{/theme}} - [], + array(), filemtime( "{$dir}/{$style_css}" ) ); - register_block_type( '{{namespace}}/{{slug}}', [ - 'editor_script' => '{{slug}}-block-editor', - 'editor_style' => '{{slug}}-block-editor', - 'style' => '{{slug}}-block', - ] ); + register_block_type( + '{{namespace}}/{{slug}}', + array( + 'editor_script' => '{{slug}}-block-editor', + 'editor_style' => '{{slug}}-block-editor', + 'style' => '{{slug}}-block', + ) + ); } add_action( 'init', '{{machine_name}}_block_init' ); diff --git a/templates/plugin-bootstrap.mustache b/templates/plugin-bootstrap.mustache index e35163a9..0b21e2e9 100644 --- a/templates/plugin-bootstrap.mustache +++ b/templates/plugin-bootstrap.mustache @@ -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_main_file}}'; + require dirname( __DIR__ ) . '/{{plugin_main_file}}'; } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );