pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: toMatchObject is not able to accept basic objects (not Traversable)

Open evankford opened this issue 1 year ago • 0 comments

What Happened

I would expect the ->toMatchObject() expectation to work with objects, but it seems to only work when you pass in an array/iterable.

I am very happy to put in support for this in the Expectation mixin and update the tests, but I don't know the rationale here and would like confirmation that would be okay.

If we change the type definition of $object on toMatchObject (from iterable to object, it seems to work fine. I'm just not sure of the consequences/why this is implemented this way.

How to Reproduce

From a working pest setup (install laravel, pest, setup test case). The following code fails:


class PestObject
{
    public function __construct(
        public string $someString,
        public int $someInt
    ) {
    }
};

it('doesnt allow objects', function () {
    $object1 = new PestObject('string1', 1);
    $object2 = new PestObject('string1', 1);

    expect($object1)->toMatchObject($object2); // This fails
});

I'm hoping there's some way to support anything that's castable to an array. I'm happy to PR/Test that if that's what you'd like!

Sample Repository

No response

Pest Version

2.35.0

PHP Version

8.2.21

Operation System

macOS

Notes

No response

evankford avatar Aug 14 '24 04:08 evankford