Skip to content
Merged
6 changes: 5 additions & 1 deletion features/scaffold-block.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
85 changes: 85 additions & 0 deletions features/scaffold-lint.feature
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions features/scaffold-plugin-tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions features/scaffold.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Scaffold_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
26 changes: 15 additions & 11 deletions templates/block-php.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function {{machine_name}}_block_init() {
return;
}
{{#plugin}}
$dir = dirname( __FILE__ );
$dir = __DIR__;
{{/plugin}}
{{#theme}}
$dir = get_stylesheet_directory() . '/blocks';
Expand All @@ -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';
Expand All @@ -50,7 +51,7 @@ function {{machine_name}}_block_init() {
{{#theme}}
get_stylesheet_directory_uri() . "/blocks/{$editor_css}",
{{/theme}}
[],
array(),
filemtime( "{$dir}/{$editor_css}" )
);

Expand All @@ -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' );
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_main_file}}';
require dirname( __DIR__ ) . '/{{plugin_main_file}}';
}

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