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

Stub array interfaces from an existing array. #916

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

courtney-miles
Copy link

This PR is for #905

It provides a convenient way to stub all the methods associated with a class that implements \Iterator, \ArrayAccess or \Countable, by supplying an array as the source for the method values.

$arrOfViolations = [
    \Mockery::mock(\Symfony\Component\Validator\ConstraintViolationInterface::class),
    \Mockery::mock(\Symfony\Component\Validator\ConstraintViolationInterface::class),
];

$mockViolationList = \Mockery::stubTraversable(
    \Symfony\Component\Validator\ConstraintViolationListInterface::class,
    $arrOfViolations
);

echo count($mockViolationList); // 2

foreach ($mockViolationList as $violation) {
    // ...
}

A con with this current implementation is that the array items can't be updated. I.e. it does not stub ArrayAccess::offsetSet() and ArrayAccess::offsetUnset()

@davedevelopment
Copy link
Collaborator

A con with this current implementation is that the array items can't be updated. I.e. it does not stub ArrayAccess::offsetSet() and ArrayAccess::offsetUnset()

Is this a limitation of Mockery or just this specific implementation?

Copy link
Collaborator

@davedevelopment davedevelopment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if most of the logic here could be pulled in to it's own class? The Container class is quite a monster as is and I can't see that the code needs to live there.

@courtney-miles
Copy link
Author

Is this a limitation of Mockery or just this specific implementation?

There was an issue with my implementation. I don't recall exactly what the issue was, but I wanted to see if there was interest in this before I invested in finding a solution.

I wonder if most of the logic here could be pulled in to it's own class?

I'm happy to shift it.

Stay tuned for the next update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants