diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index aeb3001d27d..486561fc488 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -62,14 +62,14 @@ public static function assertArrayHasKey($key, $array, $message = '') */ public static function assertArraySubset($subset, $array, $strict = false, $message = '') { - if (!is_array($subset)) { + if (!(is_array($subset) || $subset instanceof ArrayAccess)) { throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'array or ArrayAccess' ); } - if (!is_array($array)) { + if (!(is_array($array) || $array instanceof ArrayAccess)) { throw PHPUnit_Util_InvalidArgumentHelper::factory( 2, 'array or ArrayAccess' diff --git a/src/Framework/Constraint/ArraySubset.php b/src/Framework/Constraint/ArraySubset.php index c3cb0950b1e..aa67238fcba 100644 --- a/src/Framework/Constraint/ArraySubset.php +++ b/src/Framework/Constraint/ArraySubset.php @@ -49,6 +49,16 @@ public function __construct($subset, $strict = false) */ protected function matches($other) { + //type cast $other & $this->subset as an array to allow + //support in standard array functions. + if($other instanceof ArrayAccess) { + $other = (array) $other; + } + + if($this->subset instanceof ArrayAccess) { + $this->subset = (array) $this->subset; + } + $patched = array_replace_recursive($other, $this->subset); if ($this->strict) { diff --git a/tests/Framework/AssertTest.php b/tests/Framework/AssertTest.php index cb1c069bd68..232d4aecb63 100644 --- a/tests/Framework/AssertTest.php +++ b/tests/Framework/AssertTest.php @@ -182,6 +182,11 @@ public function testAssertArraySubset() $this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $array); $this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $array); + $arrayAccessData = new ArrayObject($array); + + $this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $arrayAccessData); + $this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $arrayAccessData); + try { $this->assertArraySubset(['a' => 'bad value'], $array); } catch (PHPUnit_Framework_AssertionFailedError $e) {