From 897b1645bbecf7496077554192ffe8f4095bbf88 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 1 Dec 2014 05:01:29 +0000 Subject: [PATCH] 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 --- tests/phpunit/includes/testcase.php | 11 +- tests/phpunit/tests/includes/helpers.php | 162 +++++++++++++++++++++++ 2 files changed, 171 insertions(+), 2 deletions(-) create mode 100755 tests/phpunit/tests/includes/helpers.php diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 2a2abf381e..933e66f76a 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -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 ) { diff --git a/tests/phpunit/tests/includes/helpers.php b/tests/phpunit/tests/includes/helpers.php new file mode 100755 index 0000000000..c589022e8c --- /dev/null +++ b/tests/phpunit/tests/includes/helpers.php @@ -0,0 +1,162 @@ +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 ); + } + } +}