diff --git a/tests/Framework/AssertTest.php b/tests/Framework/AssertTest.php index b8b8a4e3192..c4e923d6455 100644 --- a/tests/Framework/AssertTest.php +++ b/tests/Framework/AssertTest.php @@ -159,13 +159,17 @@ public function testAssertArraySubset(): void 'd' => ['a2' => ['a3' => 'item a3', 'b3' => 'item b3']] ]; + $this->assertArraySubset(['a' => 'item a'], $array); $this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $array); $this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $array); + $this->assertArraySubset(['b' => 'item b', 'd' => ['a2' => ['b3' => 'item b3']]], $array); $arrayAccessData = new \ArrayObject($array); + $this->assertArraySubset(['a' => 'item a'], $arrayAccessData); $this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $arrayAccessData); $this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $arrayAccessData); + $this->assertArraySubset(['b' => 'item b', 'd' => ['a2' => ['b3' => 'item b3']]], $arrayAccessData); try { $this->assertArraySubset(['a' => 'bad value'], $array); @@ -181,6 +185,39 @@ public function testAssertArraySubset(): void $this->fail(); } + public function testAssertArraySubsetWithIndexedArrays(): void + { + $array = [ + 'item a', + 'item b', + ['a2' => 'item a2', 'b2' => 'item b2'], + ['a2' => ['a3' => 'item a3', 'b3' => 'item b3']] + ]; + + $this->assertArraySubset(['item a', ['a2' => 'item a2']], $array); + $this->assertArraySubset(['item a', ['a2' => ['b3' => 'item b3']]], $array); + $this->assertArraySubset(['item b', ['a2' => ['b3' => 'item b3']]], $array); + + $arrayAccessData = new \ArrayObject($array); + + $this->assertArraySubset(['item a', ['a2' => 'item a2']], $arrayAccessData); + $this->assertArraySubset(['item a', ['a2' => ['b3' => 'item b3']]], $arrayAccessData); + $this->assertArraySubset(['item b', ['a2' => ['b3' => 'item b3']]], $arrayAccessData); + + try { + $this->assertArraySubset(['bad value'], $array); + } catch (AssertionFailedError $e) { + } + + try { + $this->assertArraySubset([['a2' => ['bad index' => 'item b3']]], $array); + } catch (AssertionFailedError $e) { + return; + } + + $this->fail(); + } + public function testAssertArraySubsetWithDeepNestedArrays(): void { $array = [ diff --git a/tests/Framework/Constraint/ArraySubsetTest.php b/tests/Framework/Constraint/ArraySubsetTest.php index 9cf24ae337a..afd3f6f60de 100644 --- a/tests/Framework/Constraint/ArraySubsetTest.php +++ b/tests/Framework/Constraint/ArraySubsetTest.php @@ -17,30 +17,146 @@ class ArraySubsetTest extends ConstraintTestCase public static function evaluateDataProvider() { return [ - 'loose array subset and array other' => [ + 'loose associative array subset and array other' => [ 'expected' => true, 'subset' => ['bar' => 0], 'other' => ['foo' => '', 'bar' => '0'], 'strict' => false ], - 'strict array subset and array other' => [ + 'strict associative array subset and array other' => [ 'expected' => false, 'subset' => ['bar' => 0], 'other' => ['foo' => '', 'bar' => '0'], 'strict' => true ], - 'loose array subset and ArrayObject other' => [ + 'loose associative array subset and ArrayObject other' => [ 'expected' => true, 'subset' => ['bar' => 0], 'other' => new \ArrayObject(['foo' => '', 'bar' => '0']), 'strict' => false ], - 'strict ArrayObject subset and array other' => [ + 'strict associative ArrayObject subset and array other' => [ 'expected' => true, 'subset' => new \ArrayObject(['bar' => 0]), 'other' => ['foo' => '', 'bar' => 0], 'strict' => true ], + 'loose indexed array subset and array other' => [ + 'expected' => true, + 'subset' => [0], + 'other' => ['', '0'], + 'strict' => false + ], + 'strict indexed array subset and array other' => [ + 'expected' => false, + 'subset' => [0], + 'other' => ['', '0'], + 'strict' => true + ], + 'loose indexed array subset and ArrayObject other' => [ + 'expected' => true, + 'subset' => [0], + 'other' => new \ArrayObject(['', '0']), + 'strict' => false + ], + 'strict indexed ArrayObject subset and array other' => [ + 'expected' => true, + 'subset' => new \ArrayObject([0]), + 'other' => ['', 0], + 'strict' => true + ], + 'loose unordered indexed array subset and array other' => [ + 'expected' => true, + 'subset' => [0, '1'], + 'other' => ['1', '2', '0'], + 'strict' => false + ], + 'strict unordered indexed array subset and array other' => [ + 'expected' => false, + 'subset' => [0, '1'], + 'other' => ['1', '2', '0'], + 'strict' => true + ], + 'loose unordered indexed array subset and ArrayObject other' => [ + 'expected' => true, + 'subset' => [0, '1'], + 'other' => new \ArrayObject(['1', '2', '0']), + 'strict' => false + ], + 'strict unordered indexed ArrayObject subset and array other' => [ + 'expected' => true, + 'subset' => new \ArrayObject([0, '1']), + 'other' => ['1', '2', 0], + 'strict' => true + ], + 'loose unordered multidimensional indexed array subset and array other' => [ + 'expected' => true, + 'subset' => [ + [[3, 4], 2], + '10', + ], + 'other' => [ + 0 => '1', + 'a' => [ + 'aa' => '2', + 'ab' => [5, 4, 3], + 'ac' => 10, + ], + 'b' => '10', + ], + 'strict' => false + ], + 'strict unordered multidimensional indexed array subset and array other' => [ + 'expected' => false, + 'subset' => [ + [[3, 4], 2], + '10', + ], + 'other' => [ + 0 => '1', + 'a' => [ + 'aa' => '2', + 'ab' => [5, 4, 3], + 'ac' => 10, + ], + 'b' => '10', + ], + 'strict' => true + ], + 'loose unordered multidimensional indexed array subset and ArrayObject other' => [ + 'expected' => true, + 'subset' => [ + [[3, 4], 2], + '10', + ], + 'other' => new \ArrayObject([ + 0 => '1', + 'a' => [ + 'aa' => '2', + 'ab' => [5, 4, 3], + 'ac' => 10, + ], + 'b' => '10', + ]), + 'strict' => false + ], + 'strict unordered multidimensional indexed ArrayObject subset and array other' => [ + 'expected' => true, + 'subset' => new \ArrayObject([ + [[3, 4], '2'], + '10', + ]), + 'other' => [ + 0 => '1', + 'a' => [ + 'aa' => '2', + 'ab' => [5, 4, 3], + 'ac' => 10, + ], + 'b' => '10', + ], + 'strict' => true + ], ]; }