Unit Tests: The `assertEqualSets()` helper was returning true for some sets that were not equal. `assertEqualSets()` now behaves correctly, and the new `assertEqualSetsWithIndex()` helper also checks that the array indexes are the same.

Fixes #30522.


git-svn-id: https://develop.svn.wordpress.org/trunk@30687 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2014-12-01 05:01:29 +00:00
parent fe4b0c7f14
commit 897b1645bb
2 changed files with 171 additions and 2 deletions

View File

@ -262,8 +262,15 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
function assertEqualSets( $expected, $actual ) {
$this->assertEquals( array(), array_diff( $expected, $actual ) );
$this->assertEquals( array(), array_diff( $actual, $expected ) );
sort( $expected );
sort( $actual );
$this->assertEquals( $expected, $actual );
}
function assertEqualSetsWithIndex( $expected, $actual ) {
ksort( $expected );
ksort( $actual );
$this->assertEquals( $expected, $actual );
}
function go_to( $url ) {

View File

@ -0,0 +1,162 @@
<?php
/**
* @group phpunit
*/
class Tests_TestHelpers extends WP_UnitTestCase {
/**
* @ticket 30522
*/
function data_assertEqualSets() {
return array(
array(
array( 1, 2, 3 ), // test expected
array( 1, 2, 3 ), // test actual
false // exception expected
),
array(
array( 1, 2, 3 ),
array( 2, 3, 1 ),
false
),
array(
array( 1, 2, 3 ),
array( 1, 2, 3, 4 ),
true
),
array(
array( 1, 2, 3, 4 ),
array( 1, 2, 3 ),
true
),
array(
array( 1, 2, 3 ),
array( 3, 4, 2, 1 ),
true
),
array(
array( 1, 2, 3 ),
array( 1, 2, 3, 3 ),
true
),
array(
array( 1, 2, 3 ),
array( 2, 3, 1, 3 ),
true
),
);
}
/**
* @dataProvider data_assertEqualSets
* @ticket 30522
*/
function test_assertEqualSets( $expected, $actual, $exception ) {
if ( $exception ) {
try {
$this->assertEqualSets( $expected, $actual );
} catch ( PHPUnit_Framework_ExpectationFailedException $ex ) {
return;
}
$this->fail();
} else {
$this->assertEqualSets( $expected, $actual );
}
}
/**
* @ticket 30522
*/
function data_assertEqualSetsWithIndex() {
return array(
array(
array( 1, 2, 3 ), // test expected
array( 1, 2, 3 ), // test actual
false // exception expected
),
array(
array( 'a' => 1, 'b' => 2, 'c' => 3 ),
array( 'a' => 1, 'b' => 2, 'c' => 3 ),
false
),
array(
array( 1, 2, 3 ),
array( 2, 3, 1 ),
true
),
array(
array( 'a' => 1, 'b' => 2, 'c' => 3 ),
array( 'b' => 2, 'c' => 3, 'a' => 1 ),
false
),
array(
array( 1, 2, 3 ),
array( 1, 2, 3, 4 ),
true
),
array(
array( 1, 2, 3, 4 ),
array( 1, 2, 3 ),
true
),
array(
array( 'a' => 1, 'b' => 2, 'c' => 3 ),
array( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ),
true
),
array(
array( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ),
array( 'a' => 1, 'b' => 2, 'c' => 3 ),
true
),
array(
array( 1, 2, 3 ),
array( 3, 4, 2, 1 ),
true
),
array(
array( 'a' => 1, 'b' => 2, 'c' => 3 ),
array( 'c' => 3, 'b' => 2, 'd' => 4, 'a' => 1 ),
true
),
array(
array( 1, 2, 3 ),
array( 1, 2, 3, 3 ),
true
),
array(
array( 'a' => 1, 'b' => 2, 'c' => 3 ),
array( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 3 ),
true
),
array(
array( 1, 2, 3 ),
array( 2, 3, 1, 3 ),
true
),
array(
array( 'a' => 1, 'b' => 2, 'c' => 3 ),
array( 'c' => 3, 'b' => 2, 'd' => 3, 'a' => 1 ),
true
),
);
}
/**
* @dataProvider data_assertEqualSetsWithIndex
* @ticket 30522
*/
function test_assertEqualSetsWithIndex( $expected, $actual, $exception ) {
if ( $exception ) {
try {
$this->assertEqualSetsWithIndex( $expected, $actual );
} catch ( PHPUnit_Framework_ExpectationFailedException $ex ) {
return;
}
$this->fail();
} else {
$this->assertEqualSetsWithIndex( $expected, $actual );
}
}
}