Skip to content

Commit 78da8c4

Browse files
authored
Merge pull request #359 from wp-cli/copilot/set-up-behat-linting
2 parents 584b52d + d731b5d commit 78da8c4

7 files changed

Lines changed: 113 additions & 18 deletions

features/scaffold-block.feature

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ Feature: WordPress block code scaffolding
5959
"""
6060
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
6161
"""
62-
register_block_type( 'movies/the-green-mile', [
62+
register_block_type(
63+
"""
64+
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
65+
"""
66+
'movies/the-green-mile',
6367
"""
6468
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
6569
"""

features/scaffold-lint.feature

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Feature: Lint scaffolded code
2+
3+
Background:
4+
Given a WP install
5+
And I run `wp plugin path`
6+
And save STDOUT as {PLUGIN_DIR}
7+
8+
# Create a helper plugin to install phpcs once for all scenarios
9+
When I run `wp scaffold plugin phpcs-helper --skip-tests`
10+
Then the return code should be 0
11+
12+
# Install coding standards
13+
When I run `composer config --working-dir={PLUGIN_DIR}/phpcs-helper allow-plugins.dealerdirect/phpcodesniffer-composer-installer true`
14+
Then the return code should be 0
15+
16+
When I run `composer require --dev --working-dir={PLUGIN_DIR}/phpcs-helper dealerdirect/phpcodesniffer-composer-installer wp-coding-standards/wpcs --no-interaction --quiet`
17+
Then the return code should be 0
18+
19+
Scenario: Scaffold plugin and lint it
20+
When I run `wp scaffold plugin test-plugin`
21+
Then STDOUT should not be empty
22+
And the {PLUGIN_DIR}/test-plugin/test-plugin.php file should exist
23+
And the {PLUGIN_DIR}/test-plugin/.phpcs.xml.dist file should exist
24+
25+
When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {PLUGIN_DIR}/test-plugin/test-plugin.php`
26+
Then the return code should be 0
27+
28+
Scenario: Scaffold post-type and lint it
29+
When I run `wp theme install twentytwentyone --activate`
30+
And I run `wp eval 'echo STYLESHEETPATH;'`
31+
And save STDOUT as {STYLESHEETPATH}
32+
33+
And I run `wp scaffold post-type movie --theme`
34+
Then STDOUT should not be empty
35+
And the {STYLESHEETPATH}/post-types/movie.php file should exist
36+
37+
When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {STYLESHEETPATH}/post-types/movie.php`
38+
Then the return code should be 0
39+
40+
Scenario: Scaffold taxonomy and lint it
41+
When I run `wp theme install twentytwentyone --activate`
42+
And I run `wp eval 'echo STYLESHEETPATH;'`
43+
And save STDOUT as {STYLESHEETPATH}
44+
45+
And I run `wp scaffold taxonomy genre --theme`
46+
Then STDOUT should not be empty
47+
And the {STYLESHEETPATH}/taxonomies/genre.php file should exist
48+
49+
When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {STYLESHEETPATH}/taxonomies/genre.php`
50+
Then the return code should be 0
51+
52+
Scenario: Scaffold plugin tests and lint them
53+
When I run `wp scaffold plugin test-plugin`
54+
Then STDOUT should not be empty
55+
And the {PLUGIN_DIR}/test-plugin/tests directory should exist
56+
And the {PLUGIN_DIR}/test-plugin/tests/bootstrap.php file should exist
57+
And the {PLUGIN_DIR}/test-plugin/tests/test-sample.php file should exist
58+
59+
# Run phpcs on the test files
60+
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`
61+
Then the return code should be 0
62+
63+
Scenario: Scaffold child theme and lint it
64+
When I run `wp theme install twentytwentyone --activate`
65+
And I run `wp theme path`
66+
And save STDOUT as {THEME_DIR}
67+
68+
And I run `wp scaffold child-theme test-child --parent_theme=twentytwentyone`
69+
Then STDOUT should not be empty
70+
And the {THEME_DIR}/test-child/functions.php file should exist
71+
72+
When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {THEME_DIR}/test-child/functions.php`
73+
Then the return code should be 0
74+
75+
Scenario: Scaffold block and lint it
76+
When I run `wp scaffold plugin movies`
77+
And I run `wp plugin path movies --dir`
78+
And save STDOUT as {MOVIES_DIR}
79+
80+
And I run `wp scaffold block the-green-mile --plugin=movies`
81+
Then STDOUT should not be empty
82+
And the {MOVIES_DIR}/blocks/the-green-mile.php file should exist
83+
84+
When I run `{PLUGIN_DIR}/phpcs-helper/vendor/bin/phpcs --standard=WordPress {MOVIES_DIR}/blocks/the-green-mile.php`
85+
Then the return code should be 0

features/scaffold-plugin-tests.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Feature: Scaffold plugin unit tests
2121
"""
2222
And the {PLUGIN_DIR}/hello-world/tests/bootstrap.php file should contain:
2323
"""
24-
require dirname( dirname( __FILE__ ) ) . '/hello-world.php';
24+
require dirname( __DIR__ ) . '/hello-world.php';
2525
"""
2626
And the {PLUGIN_DIR}/hello-world/tests/bootstrap.php file should contain:
2727
"""
@@ -302,7 +302,7 @@ Feature: Scaffold plugin unit tests
302302
When I run `wp scaffold plugin-tests foo`
303303
Then the wp-content/plugins/foo/tests/bootstrap.php file should contain:
304304
"""
305-
require dirname( dirname( __FILE__ ) ) . '/bar.php';
305+
require dirname( __DIR__ ) . '/bar.php';
306306
"""
307307

308308
Scenario: Accept bitbucket as valid CI in plugin scaffold

features/scaffold.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ Feature: WordPress code scaffolding
432432
And the wp-content/mu-plugins/custom-plugin/tests/bootstrap.php file should exist
433433
And the wp-content/mu-plugins/custom-plugin/tests/bootstrap.php file should contain:
434434
"""
435-
require dirname( dirname( __FILE__ ) ) . '/custom-plugin.php';
435+
require dirname( __DIR__ ) . '/custom-plugin.php';
436436
"""
437437
438438
Scenario: Scaffold tests for a plugin with a different slug than plugin directory
@@ -456,7 +456,7 @@ Feature: WordPress code scaffolding
456456
And the wp-content/mu-plugins/custom-plugin2/tests/bootstrap.php file should exist
457457
And the wp-content/mu-plugins/custom-plugin2/tests/bootstrap.php file should contain:
458458
"""
459-
require dirname( dirname( __FILE__ ) ) . '/custom-plugin-slug.php';
459+
require dirname( __DIR__ ) . '/custom-plugin-slug.php';
460460
"""
461461
462462
Scenario: Scaffold tests parses plugin readme.txt

src/Scaffold_Command.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ private function scaffold( $slug, $assoc_args, $defaults, $subdir, $templates )
182182

183183
$target_slug = '';
184184

185-
if ( false !== $control_args['theme'] ) {
185+
if ( true === $control_args['theme'] ) {
186+
$target_slug = get_stylesheet();
187+
} elseif ( false !== $control_args['theme'] ) {
186188
$target_slug = $control_args['theme'];
187189
} elseif ( false !== $control_args['plugin'] ) {
188190
$target_slug = $control_args['plugin'];

templates/block-php.mustache

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function {{machine_name}}_block_init() {
1818
return;
1919
}
2020
{{#plugin}}
21-
$dir = dirname( __FILE__ );
21+
$dir = __DIR__;
2222
{{/plugin}}
2323
{{#theme}}
2424
$dir = get_stylesheet_directory() . '/blocks';
@@ -33,12 +33,13 @@ function {{machine_name}}_block_init() {
3333
{{#theme}}
3434
get_stylesheet_directory_uri() . "/blocks/{$index_js}",
3535
{{/theme}}
36-
[
36+
array(
3737
'wp-blocks',
3838
'wp-i18n',
3939
'wp-element',
40-
],
41-
filemtime( "{$dir}/{$index_js}" )
40+
),
41+
filemtime( "{$dir}/{$index_js}" ),
42+
false
4243
);
4344

4445
$editor_css = '{{slug}}/editor.css';
@@ -50,7 +51,7 @@ function {{machine_name}}_block_init() {
5051
{{#theme}}
5152
get_stylesheet_directory_uri() . "/blocks/{$editor_css}",
5253
{{/theme}}
53-
[],
54+
array(),
5455
filemtime( "{$dir}/{$editor_css}" )
5556
);
5657

@@ -63,15 +64,18 @@ function {{machine_name}}_block_init() {
6364
{{#theme}}
6465
get_stylesheet_directory_uri() . "/blocks/{$style_css}",
6566
{{/theme}}
66-
[],
67+
array(),
6768
filemtime( "{$dir}/{$style_css}" )
6869
);
6970

70-
register_block_type( '{{namespace}}/{{slug}}', [
71-
'editor_script' => '{{slug}}-block-editor',
72-
'editor_style' => '{{slug}}-block-editor',
73-
'style' => '{{slug}}-block',
74-
] );
71+
register_block_type(
72+
'{{namespace}}/{{slug}}',
73+
array(
74+
'editor_script' => '{{slug}}-block-editor',
75+
'editor_style' => '{{slug}}-block-editor',
76+
'style' => '{{slug}}-block',
77+
)
78+
);
7579
}
7680

7781
add_action( 'init', '{{machine_name}}_block_init' );

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_main_file}}';
32+
require dirname( __DIR__ ) . '/{{plugin_main_file}}';
3333
}
3434

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

0 commit comments

Comments
 (0)