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

Add integration and unit tests #402

Merged
merged 7 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"require-dev": {
"brain/monkey": "^2",
"squizlabs/php_codesniffer": "^3.4"
"squizlabs/php_codesniffer": "^3.4",
"wp-coding-standards/wpcs": "2.1.1"
}
}
2 changes: 1 addition & 1 deletion js/blocks/loader/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Gets the attributes for a block, based on given fields.
*
* @param {Object} fields The fields to get the attributes from.
* @return {Array} attributes The attributes for the fields.
* @return {Object} attributes The attributes for the fields.
*/
const getBlockAttributes = ( fields ) => {

Expand Down
8 changes: 5 additions & 3 deletions js/blocks/loader/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
* @return {number} Either -1, 0, or 1, depending on the comparison.
*/
const compare = ( a, b ) => {
if ( a.order < b.order )
if ( a.order < b.order ) {
return -1;
if ( a.order > b.order )
}
if ( a.order > b.order ) {
return 1;
}
return 0;
}

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

let fieldList = []
let fieldList = [];

for ( let fieldName in fields ) {
if ( '' === fieldName ) {
Expand Down
3 changes: 1 addition & 2 deletions js/blocks/loader/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -56,6 +55,6 @@ const registerBlocks = () => {
},
} )
}
}
};

export default registerBlocks();
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
},
"scripts": {
"dev": "cross-env BABEL_ENV=default webpack --watch",
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack"
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
"lint:php": "vendor/bin/phpcs",
"lint:php:fix": "vendor/bin/phpcbf"
},
"author": "Block Lab",
"license": "GPL-2.0+",
Expand Down
2 changes: 1 addition & 1 deletion php/blocks/class-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function get_attributes_from_field( $attributes, $field_name, $field ) {
/**
* Renders the block provided a template is provided.
*
* @param array $block The block to render.
* @param Block $block The block to render.
* @param array $attributes Attributes to render.
*
* @return mixed
Expand Down
1 change: 0 additions & 1 deletion php/class-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace Block_Lab;

use Block_Lab\Blocks;
use Block_Lab\Component_Abstract;

/**
* Class Util
Expand Down
5 changes: 4 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

<rule ref="WordPress" />

<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<config name="testVersion" value="5.4-"/>

<arg value="s"/>
Expand All @@ -12,7 +16,6 @@
<file>.</file>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/package/*</exclude-pattern>
<exclude-pattern>*.js</exclude-pattern>
<exclude-pattern>*.css</exclude-pattern>
Expand Down
8 changes: 6 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

<testsuites>
<testsuite name="unit">
<directory suffix=".php">./tests/php/helpers/</directory>
<directory prefix="test-class-" suffix=".php">./tests/</directory>
<directory suffix=".php">./tests/php/unit/helpers/</directory>
<directory prefix="test-" suffix=".php">./tests/php/unit/</directory>
</testsuite>

<testsuite name="integration">
<directory prefix="test-" suffix=".php">./tests/php/integration/</directory>
</testsuite>
</testsuites>

Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}

if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL;
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
exit( 1 );
}

Expand All @@ -27,7 +27,7 @@ function _manually_load_plugin() {
$plugin_root = dirname( __DIR__ );
require $plugin_root . '/vendor/autoload.php';
require $plugin_root . '/vendor/antecedent/patchwork/Patchwork.php';
require $plugin_root . '/tests/php/trait-testing-helper.php';
require $plugin_root . '/tests/php/unit/helpers/trait-testing-helper.php';
require $plugin_root . '/block-lab.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
Expand Down
36 changes: 0 additions & 36 deletions tests/class-test-the-plugin.php

This file was deleted.

75 changes: 75 additions & 0 deletions tests/php/integration/fixtures/all-fields-except-repeater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* A mock template for a block, testing all fields except the repeater.
*
* @package Block_Lab
*/

// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping could interfere with testing block_value().

$non_object_fields = array(
'text',
'textarea',
'url',
'email',
'number',
'color',
'image',
'select',
'toggle',
'range',
'checkbox',
'radio',
'rich-text',
);

foreach ( $non_object_fields as $field ) :
?>
<p class="<?php block_field( 'className' ); ?>">
<?php
/* translators: %s is the field name */
printf(
'Here is the result of block_field() for %s: ',
$field
);
block_field( $field );
?>
</p>

<p>
<?php
/* translators: %s is the field name, %s is the result of calling block_value() */
printf(
'And here is the result of calling block_value() for %s: %s',
$field,
block_value( $field )
);
?>
</p>
<?php
endforeach;

$non_string_fields = array(
'post' => array( 'ID', 'post_name' ),
'taxonomy' => array( 'term_id', 'name' ),
'user' => array( 'ID', 'first_name' ),
);

foreach ( $non_string_fields as $name => $value ) :
/* translators: %s is the field name */
printf(
'Here is the result of block_field() for %s: ',
$name
);
block_field( $name );

$block_value = block_value( $name );
foreach ( $value as $block_value_property ) :
printf(
'Here is the result of passing %s to block_value() with the property %s: %s',
$name,
$block_value_property,
$block_value->$block_value_property
);
endforeach;
endforeach;
Loading