Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

[Dont Merge Yet] Fix PHP unit errors with wp-env phpunit container removal #182

Merged
merged 5 commits into from
May 22, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"test:js": "cd ../../wpps-scripts; sh test-js.sh $npm_package_wpps_options -p \"${OLDPWD}\";",
"zip": "cd ../../wpps-scripts; sh zip.sh $npm_package_wpps_options -p \"${OLDPWD}\";"
}
}
}
4 changes: 2 additions & 2 deletions wp-modules/api-data/tests/ApiDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ApiDataTest extends WP_UnitTestCase {
/**
* @inheritDoc
*/
public function setUp() {
public function setUp() : void {
parent::setUp();
add_filter( 'request_filesystem_credentials', '__return_true' );
add_filter( 'stylesheet_directory', [ $this, 'get_fixtures_directory' ] );
Expand All @@ -31,7 +31,7 @@ public function setUp() {
/**
* @inheritDoc
*/
public function tearDown() {
public function tearDown() : void {
unset( $GLOBALS['wp_rest_server'] );
remove_filter( 'request_filesystem_credentials', '__return_true' );
remove_filter( 'stylesheet_directory', [ $this, 'get_fixtures_directory' ] );
Expand Down
46 changes: 38 additions & 8 deletions wp-modules/editor/tests/EditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,30 @@ public function test_register_pattern_post_type() {
public function test_register_pattern_post_type_meta_types() {
register_pattern_post_type();

foreach ( array_diff( get_pattern_defaults(), [ 'title' => null ] ) as $meta_key => $default_value ) {
$expected_type = get_registered_meta_keys( 'post', 'pm_pattern' )[ $meta_key ]['type'];
$pattern_default_keys = get_pattern_defaults();
$registered_keys = get_registered_meta_keys( 'post', 'pm_pattern' );

// Loop through each default key to make sure its type matches what was actually registered by WP.
foreach ( $pattern_default_keys as $meta_key => $default_value ) {

// The title and content pattern defaults are not registered meta keys, so skip them.
if ( 'title' === $meta_key || 'content' === $meta_key ) {
continue;
}

$expected_type = $registered_keys[ $meta_key ]['type'];

// Fix the typing for numbers.
if ( 'number' === $expected_type ) {
$expected_type = 'integer';
}

$actual_type = gettype( $default_value );

$this->assertSame(
'number' === $expected_type ? 'integer' : $expected_type,
gettype( $default_value ),
"the type of the default for {$meta_key} is wrong"
$expected_type,
$actual_type,
"The type of the default for {$meta_key} is wrong. It should be {$expected_type} but it was {$actual_type}"
);
}
}
Expand All @@ -54,11 +72,23 @@ public function test_register_pattern_post_type_meta_types() {
public function test_register_pattern_post_type_meta_defaults() {
register_pattern_post_type();

foreach ( array_diff( get_pattern_defaults(), [ 'title' => null ] ) as $meta_key => $default_value ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I had to change this was because it was failing with the error Fatal error: Array to string conversion. This is apparently the error message if you use array_diff with a multi-dimentional array. See:
https://stackoverflow.com/questions/19830585/array-to-string-conversion-error-when-calling-array-diff-assoc-with-a-multid

See lines 41 and 57:
Screenshot 2023-05-19 at 4 41 44 PM

I broke it out to be a bit easier for me to understand as well. I had a hard time fully understanding what was happening without breaking it out a bit.

Copy link
Contributor

@kienstra kienstra May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience with this.

My obsession with terse code gave you some nasty debugging.

$pattern_default_keys = get_pattern_defaults();
$registered_keys = get_registered_meta_keys( 'post', 'pm_pattern' );

// Loop through each default key to make sure its default value matches what was actually registered by WP.
foreach ( $pattern_default_keys as $meta_key => $default_value ) {

// The title and content pattern defaults are not registered meta keys, so skip them.
if ( 'title' === $meta_key || 'content' === $meta_key ) {
continue;
}

$actual_value = $registered_keys[ $meta_key ]['default'];

$this->assertSame(
$default_value,
get_registered_meta_keys( 'post', 'pm_pattern' )[ $meta_key ]['default'],
"the default value of {$meta_key} is wrong"
$actual_value,
"The value of the default for {$meta_key} is wrong."
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion wp-modules/editor/tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UtilsTest extends WP_UnitTestCase {
/**
* @inheritDoc
*/
public function tearDown() {
public function tearDown() : void {
delete_pattern_posts();
parent::tearDown();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Class GetModuleDataTest
* Class PatternManagerGetModuleDataTest
*
* @package pattern-manager
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GetVersionControlTest extends WP_UnitTestCase {
/**
* @inheritDoc
*/
public function setUp() {
public function setUp() : void {
parent::setUp();

$this->user_id = $this->factory->user->create();
Expand All @@ -33,7 +33,7 @@ public function setUp() {
/**
* @inheritDoc
*/
public function tearDown() {
public function tearDown() : void {
delete_user_meta( $this->user_id, get_version_control_meta_key() );
wp_delete_user( $this->user_id );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PatternDataHandlersTest extends WP_UnitTestCase {
/**
* @inheritDoc
*/
public function setUp() {
public function setUp() : void {
parent::setUp();
add_filter( 'request_filesystem_credentials', '__return_true' );
add_filter( 'stylesheet_directory', [ $this, 'get_fixtures_directory' ] );
Expand All @@ -30,7 +30,7 @@ public function setUp() {
/**
* @inheritDoc
*/
public function tearDown() {
public function tearDown() : void {
remove_filter( 'request_filesystem_credentials', '__return_true' );
remove_filter( 'stylesheet_directory', [ $this, 'get_fixtures_directory' ] );
remove_filter( 'stylesheet_directory_uri', [ $this, 'get_stylesheet_directory_uri' ] );
Expand Down