Tests: Test custom assertions parameter data type in WP_UnitTestCase_Base.

The following changes improve tests stability.

The `assertEqualFields()` method expects an object and a fields array as inputs and subsequently approaches the received parameters as such, but did not verify whether the received parameters are of the expected types.

Along the same lines, the `assertSameSets()`, `assertEqualSets()`, `assertSameSetsWithIndex()` and the `assertEqualSetsWithIndex()` methods all expect arrays for both the actual as well as the expected values and uses the array function `[k]sort()` on both, but never verified that the received inputs were actually arrays, which could lead to PHP errors on the sorting function calls.

Follow-up to [30687], [42343], [48937], [48939], [51480], [51481].

Props jrf.
See #53363.

git-svn-id: https://develop.svn.wordpress.org/trunk@51697 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork 2021-08-30 20:18:31 +00:00
parent da835d6627
commit 877c39bb7f

View File

@ -685,6 +685,10 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertEqualFields( $object, $fields, $message = '' ) {
$this->assertIsObject( $object, $message . ' Passed $object is not an object.' );
$this->assertIsArray( $fields, $message . ' Passed $fields is not an array.' );
$this->assertNotEmpty( $fields, $message . ' Fields array is empty.' );
foreach ( $fields as $field_name => $field_value ) {
$this->assertObjectHasAttribute( $field_name, $object, $message . " Property $field_name does not exist on the object." );
$this->assertSame( $field_value, $object->$field_name, $message . " Value of property $field_name is not $field_value." );
@ -760,6 +764,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertSameSets( $expected, $actual, $message = '' ) {
$this->assertIsArray( $expected, $message . ' Expected value must be an array.' );
$this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
sort( $expected );
sort( $actual );
$this->assertSame( $expected, $actual, $message );
@ -776,6 +783,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertEqualSets( $expected, $actual, $message = '' ) {
$this->assertIsArray( $expected, $message . ' Expected value must be an array.' );
$this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
sort( $expected );
sort( $actual );
$this->assertEquals( $expected, $actual, $message );
@ -792,6 +802,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertSameSetsWithIndex( $expected, $actual, $message = '' ) {
$this->assertIsArray( $expected, $message . ' Expected value must be an array.' );
$this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
ksort( $expected );
ksort( $actual );
$this->assertSame( $expected, $actual, $message );
@ -808,6 +821,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertEqualSetsWithIndex( $expected, $actual, $message = '' ) {
$this->assertIsArray( $expected, $message . ' Expected value must be an array.' );
$this->assertIsArray( $actual, $message . ' Value under test is not an array.' );
ksort( $expected );
ksort( $actual );
$this->assertEquals( $expected, $actual, $message );