Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Commit a93d9a7

Browse files
authored
Merge pull request #402 from getblocklab/add/unit-tests-repeater
Add integration and unit tests
2 parents 0a4cf4d + 8cc9205 commit a93d9a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+911
-280
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"require-dev": {
1111
"brain/monkey": "^2",
12-
"squizlabs/php_codesniffer": "^3.4"
12+
"squizlabs/php_codesniffer": "^3.4",
13+
"wp-coding-standards/wpcs": "2.1.1"
1314
}
1415
}

js/blocks/loader/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Gets the attributes for a block, based on given fields.
33
*
44
* @param {Object} fields The fields to get the attributes from.
5-
* @return {Array} attributes The attributes for the fields.
5+
* @return {Object} attributes The attributes for the fields.
66
*/
77
const getBlockAttributes = ( fields ) => {
88

js/blocks/loader/fields.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
* @return {number} Either -1, 0, or 1, depending on the comparison.
77
*/
88
const compare = ( a, b ) => {
9-
if ( a.order < b.order )
9+
if ( a.order < b.order ) {
1010
return -1;
11-
if ( a.order > b.order )
11+
}
12+
if ( a.order > b.order ) {
1213
return 1;
14+
}
1315
return 0;
1416
}
1517

@@ -21,7 +23,7 @@ const compare = ( a, b ) => {
2123
*/
2224
const simplifiedFields = ( fields ) => {
2325

24-
let fieldList = []
26+
let fieldList = [];
2527

2628
for ( let fieldName in fields ) {
2729
if ( '' === fieldName ) {

js/blocks/loader/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* WordPress dependencies
43
*/
@@ -56,6 +55,6 @@ const registerBlocks = () => {
5655
},
5756
} )
5857
}
59-
}
58+
};
6059

6160
export default registerBlocks();

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
},
1010
"scripts": {
1111
"dev": "cross-env BABEL_ENV=default webpack --watch",
12-
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack"
12+
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
13+
"lint:php": "vendor/bin/phpcs",
14+
"lint:php:fix": "vendor/bin/phpcbf"
1315
},
1416
"author": "Block Lab",
1517
"license": "GPL-2.0+",

php/blocks/class-loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public function get_attributes_from_field( $attributes, $field_name, $field ) {
275275
/**
276276
* Renders the block provided a template is provided.
277277
*
278-
* @param array $block The block to render.
278+
* @param Block $block The block to render.
279279
* @param array $attributes Attributes to render.
280280
*
281281
* @return mixed

php/class-util.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace Block_Lab;
1212

1313
use Block_Lab\Blocks;
14-
use Block_Lab\Component_Abstract;
1514

1615
/**
1716
* Class Util

phpcs.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
<rule ref="WordPress" />
66

7+
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
8+
<exclude-pattern>tests/*</exclude-pattern>
9+
</rule>
10+
711
<config name="testVersion" value="5.4-"/>
812

913
<arg value="s"/>
@@ -12,7 +16,6 @@
1216
<file>.</file>
1317
<exclude-pattern>*/node_modules/*</exclude-pattern>
1418
<exclude-pattern>*/vendor/*</exclude-pattern>
15-
<exclude-pattern>*/tests/*</exclude-pattern>
1619
<exclude-pattern>*/package/*</exclude-pattern>
1720
<exclude-pattern>*.js</exclude-pattern>
1821
<exclude-pattern>*.css</exclude-pattern>

phpunit.xml.dist

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010

1111
<testsuites>
1212
<testsuite name="unit">
13-
<directory suffix=".php">./tests/php/helpers/</directory>
14-
<directory prefix="test-class-" suffix=".php">./tests/</directory>
13+
<directory suffix=".php">./tests/php/unit/helpers/</directory>
14+
<directory prefix="test-" suffix=".php">./tests/php/unit/</directory>
15+
</testsuite>
16+
17+
<testsuite name="integration">
18+
<directory prefix="test-" suffix=".php">./tests/php/integration/</directory>
1519
</testsuite>
1620
</testsuites>
1721

tests/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414

1515
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
16-
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL;
16+
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1717
exit( 1 );
1818
}
1919

@@ -27,7 +27,7 @@ function _manually_load_plugin() {
2727
$plugin_root = dirname( __DIR__ );
2828
require $plugin_root . '/vendor/autoload.php';
2929
require $plugin_root . '/vendor/antecedent/patchwork/Patchwork.php';
30-
require $plugin_root . '/tests/php/trait-testing-helper.php';
30+
require $plugin_root . '/tests/php/unit/helpers/trait-testing-helper.php';
3131
require $plugin_root . '/block-lab.php';
3232
}
3333
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

tests/class-test-the-plugin.php

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* A mock template for a block, testing all fields except the repeater.
4+
*
5+
* @package Block_Lab
6+
*/
7+
8+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping could interfere with testing block_value().
9+
10+
$non_object_fields = array(
11+
'text',
12+
'textarea',
13+
'url',
14+
'email',
15+
'number',
16+
'color',
17+
'image',
18+
'select',
19+
'toggle',
20+
'range',
21+
'checkbox',
22+
'radio',
23+
'rich-text',
24+
);
25+
26+
foreach ( $non_object_fields as $field ) :
27+
?>
28+
<p class="<?php block_field( 'className' ); ?>">
29+
<?php
30+
/* translators: %s is the field name */
31+
printf(
32+
'Here is the result of block_field() for %s: ',
33+
$field
34+
);
35+
block_field( $field );
36+
?>
37+
</p>
38+
39+
<p>
40+
<?php
41+
/* translators: %s is the field name, %s is the result of calling block_value() */
42+
printf(
43+
'And here is the result of calling block_value() for %s: %s',
44+
$field,
45+
block_value( $field )
46+
);
47+
?>
48+
</p>
49+
<?php
50+
endforeach;
51+
52+
$non_string_fields = array(
53+
'post' => array( 'ID', 'post_name' ),
54+
'taxonomy' => array( 'term_id', 'name' ),
55+
'user' => array( 'ID', 'first_name' ),
56+
);
57+
58+
foreach ( $non_string_fields as $name => $value ) :
59+
/* translators: %s is the field name */
60+
printf(
61+
'Here is the result of block_field() for %s: ',
62+
$name
63+
);
64+
block_field( $name );
65+
66+
$block_value = block_value( $name );
67+
foreach ( $value as $block_value_property ) :
68+
printf(
69+
'Here is the result of passing %s to block_value() with the property %s: %s',
70+
$name,
71+
$block_value_property,
72+
$block_value->$block_value_property
73+
);
74+
endforeach;
75+
endforeach;

0 commit comments

Comments
 (0)