Skip to content

Commit

Permalink
PHPUnit: turns on PHP notices and deprecations (#43102)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonya Mork authored and hellofromtonya committed Aug 18, 2022
1 parent cb4b5ea commit c181423
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 68 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
>
<php>
<env name="WORDPRESS_TABLE_PREFIX" value="wptests_" />
Expand Down
41 changes: 33 additions & 8 deletions phpunit/blocks/render-last-posts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class Tests_Blocks_RenderLastPosts extends WP_UnitTestCase {
* @var array
*/
protected static $attachment_ids;
/**
* @var array|null
*/
private $original_block_supports;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$sticky_post = $factory->post->create_and_get(
Expand All @@ -49,17 +53,35 @@ public static function wpTearDownAfterClass() {
}
}

public function set_up() {
parent::set_up();

$this->original_block_supports = WP_Block_Supports::$block_to_render;
WP_Block_Supports::$block_to_render = array(
'attrs' => array(),
'blockName' => '',
);
}

public function tear_down() {
WP_Block_Supports::$block_to_render = $this->original_block_supports;
parent::tear_down();
}

/**
* @covers ::render_block_core_latest_posts
*/
public function test_render_block_core_latest_posts() {
$action = new MockAction();
add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 );
$attributes = array(
'displayFeaturedImage' => true,
'postsToShow' => 5,
'orderBy' => 'date',
'order' => 'DESC',
'displayFeaturedImage' => true,
'postsToShow' => 5,
'orderBy' => 'date',
'order' => 'DESC',
'excerptLength' => 0,
'featuredImageSizeSlug' => '',
'addLinkToFeaturedImage' => false,
);

gutenberg_render_block_core_latest_posts( $attributes );
Expand All @@ -75,10 +97,13 @@ public function test_render_block_core_latest_posts_no_priming() {
$action = new MockAction();
add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 );
$attributes = array(
'displayFeaturedImage' => false,
'postsToShow' => 5,
'orderBy' => 'date',
'order' => 'DESC',
'displayFeaturedImage' => false,
'postsToShow' => 5,
'orderBy' => 'date',
'order' => 'DESC',
'excerptLength' => 0,
'featuredImageSizeSlug' => '',
'addLinkToFeaturedImage' => false,
);

gutenberg_render_block_core_latest_posts( $attributes );
Expand Down
13 changes: 13 additions & 0 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@
* @package Gutenberg
*/

// Debug settings for parity with WordPress Core's PHPUnit tests.
if ( ! defined( 'WP_DEBUG' ) ) {
define( 'WP_DEBUG', true );
}
if ( ! defined( 'LOCAL_WP_DEBUG_LOG' ) ) {
define( 'LOCAL_WP_DEBUG_LOG', true );
}
if ( ! defined( 'LOCAL_WP_DEBUG_DISPLAY' ) ) {
define( 'LOCAL_WP_DEBUG_DISPLAY', true );
}
if ( ! defined( 'LOCAL_SCRIPT_DEBUG' ) ) {
define( 'LOCAL_SCRIPT_DEBUG', true );
}
if ( ! defined( 'LOCAL_WP_ENVIRONMENT_TYPE' ) ) {
define( 'LOCAL_WP_ENVIRONMENT_TYPE', 'local' );
}

// Require composer dependencies.
require_once dirname( __DIR__ ) . '/vendor/autoload.php';
Expand Down
19 changes: 19 additions & 0 deletions phpunit/class-block-library-navigation-link-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Block_Library_Navigation_Link_Test extends WP_UnitTestCase {

private static $pages;
private static $terms;
/**
* @var array|null
*/
private $original_block_supports;

public static function wpSetUpBeforeClass() {

Expand Down Expand Up @@ -90,6 +94,21 @@ public static function wpTearDownAfterClass() {
}
}

public function set_up() {
parent::set_up();

$this->original_block_supports = WP_Block_Supports::$block_to_render;
WP_Block_Supports::$block_to_render = array(
'attrs' => array(),
'blockName' => '',
);
}

public function tear_down() {
WP_Block_Supports::$block_to_render = $this->original_block_supports;
parent::tear_down();
}

function test_returns_link_when_post_is_published() {
$page_id = self::$page->ID;

Expand Down
149 changes: 89 additions & 60 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,66 +736,7 @@ function test_set_spacing_sizes( $spacing_scale, $expected_output ) {
*/
function data_generate_spacing_scale_fixtures() {
return array(
'empty_spacing_scale' => array(
'spacing_scale' => array(),
'expected_output' => null,
),

'invalid_spacing_scale_values_missing_operator' => array(
'spacingScale' => array(
'operator' => '',
'increment' => 1.5,
'steps' => 1,
'mediumStep' => 4,
'unit' => 'rem',
),
'expected_output' => null,
),

'invalid_spacing_scale_values_non_numeric_increment' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 'add two to previous value',
'steps' => 1,
'mediumStep' => 4,
'unit' => 'rem',
),
'expected_output' => null,
),

'invalid_spacing_scale_values_non_numeric_steps' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 1.5,
'steps' => 'spiral staircase preferred',
'mediumStep' => 4,
'unit' => 'rem',
),
'expected_output' => null,
),

'invalid_spacing_scale_values_non_numeric_medium_step' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 1.5,
'steps' => 5,
'mediumStep' => 'That which is just right',
'unit' => 'rem',
),
'expected_output' => null,
),

'invalid_spacing_scale_values_missing_unit' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 1.5,
'steps' => 5,
'mediumStep' => 4,
),
'expected_output' => null,
),

'one_step_spacing_scale' => array(
'one_step_spacing_scale' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 1.5,
Expand Down Expand Up @@ -1006,6 +947,94 @@ function data_generate_spacing_scale_fixtures() {
);
}

/**
* Tests generating the spacing presets array based on the spacing scale provided.
*
* @dataProvider data_set_spacing_sizes_when_invalid
*/
public function test_set_spacing_sizes_when_invalid( $spacing_scale, $expected_output ) {
$this->expectNotice();
$this->expectNoticeMessage( 'Some of the theme.json settings.spacing.spacingScale values are invalid' );

$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => 2,
'settings' => array(
'spacing' => array(
'spacingScale' => $spacing_scale,
),
),
)
);

$theme_json->set_spacing_sizes();
$this->assertSame( $expected_output, _wp_array_get( $theme_json->get_raw_data(), array( 'settings', 'spacing', 'spacingSizes', 'default' ) ) );
}

/**
* Data provider for spacing scale tests.
*
* @return array
*/
function data_set_spacing_sizes_when_invalid() {
return array(

'invalid_spacing_scale_values_missing_operator' => array(
'spacingScale' => array(
'operator' => '',
'increment' => 1.5,
'steps' => 1,
'mediumStep' => 4,
'unit' => 'rem',
),
'expected_output' => null,
),

'invalid_spacing_scale_values_non_numeric_increment' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 'add two to previous value',
'steps' => 1,
'mediumStep' => 4,
'unit' => 'rem',
),
'expected_output' => null,
),

'invalid_spacing_scale_values_non_numeric_steps' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 1.5,
'steps' => 'spiral staircase preferred',
'mediumStep' => 4,
'unit' => 'rem',
),
'expected_output' => null,
),

'invalid_spacing_scale_values_non_numeric_medium_step' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 1.5,
'steps' => 5,
'mediumStep' => 'That which is just right',
'unit' => 'rem',
),
'expected_output' => null,
),

'invalid_spacing_scale_values_missing_unit' => array(
'spacingScale' => array(
'operator' => '+',
'increment' => 1.5,
'steps' => 5,
'mediumStep' => 4,
),
'expected_output' => null,
),
);
}

function test_get_styles_for_block_with_padding_aware_alignments() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
Expand Down

0 comments on commit c181423

Please sign in to comment.