Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #2431 - ArrayAccess support missing in assertArraySubset #2438

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions src/Framework/Constraint/ArraySubset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Framework/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down