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

wrong filtering in assertArraySubset method of PHPUnit_Framework_Assert class #2108

Closed
xilnick opened this Issue Mar 8, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@xilnick
Copy link

xilnick commented Mar 8, 2016

PHPUnit_Framework_Assert::assertArraySubset documented to accept array and subset arguments as array or ArrayAcces, but actually it only cheks array and subset arguments to be an array and don't do this for ArrayAccess.

@sebastianbergmann

This comment has been minimized.

Copy link
Owner

sebastianbergmann commented Apr 13, 2016

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

@Johannestegner

This comment has been minimized.

Copy link

Johannestegner commented Nov 3, 2016

Adding a "minimal, self-contained, reproducing test case that shows the problem".

<?php

class ExampleArrayAccessTest extends PHPUnit_Framework_TestCase {

    public function testArrayAccessStuff() {

        $arr = new ExampleArrayAccess();
        $arr[0] = 1;
        $arr[1] = 10;

        $this->assertArraySubset([1, 10], $arr);
    }
}

class ExampleArrayAccess implements ArrayAccess {

    private $array = array();

    public function offsetExists($offset) {
        return isset($offset, $this->array);
    }

    public function offsetGet($offset) {
        return $this->array[$offset];
    }

    public function offsetSet($offset, $value) {
        $this->array[$offset] = $value;
    }

    public function offsetUnset($offset) {
        unset($this->array[$offset]);
    }
}

First error that one will hit is:

Argument 2 (No Value) of PHPUnit_Framework_Assert::assertArraySubset() must be a array or ArrayAccess

Which means that is_array($arr); returns false if its not actually a native array.
So I tried a naïve quick fix in the Assert.php file:

// line ~72
if (!is_array($array) && !($array instanceof ArrayAccess)) {
    throw PHPUnit_Util_InvalidArgumentHelper::factory(
        2,
        'array or ArrayAccess'
    );
}

Which then produces the following error:

array_replace_recursive(): Argument 1 is not an array

So the issue is that array_replace_recursive() does not work with ArrayAccess implementations, only with the native array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.