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

Mocked object being sorted raises PHP warning #161

Open
braddle opened this issue Feb 13, 2015 · 6 comments
Open

Mocked object being sorted raises PHP warning #161

braddle opened this issue Feb 13, 2015 · 6 comments

Comments

@braddle
Copy link

braddle commented Feb 13, 2015

This bug occurs when calling a mocked function inside a usort function.

The warning is cause by CallCenter::makeCall() calling debug_backtrace() and changing the reference count (http://stackoverflow.com/questions/3235387/usort-array-was-modified-by-the-user-comparison-function)

it is possble to sort mocked objects
      warning: uasort(): Array was modified by the user comparison function in /home/mark/Anobii/pricing_models/src/Sorter.php
      line 20
class Int
{
    public function get()
    {

    }
}
class Sorter
{
    private $prices = [];
    public function addInt(Int $argument1)
    {
        $this->prices[] = $argument1;
    }

    public function sort()
    {
        uasort(
            $this->prices,
            function (Int $a, Int $b) {
                return $a->get() - $b->get();
            }
        );
    }
}
class SorterSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType('Ents\Pricing\Model\Sorter');
    }

    function it_is_possble_to_sort_mocked_objects(Int $a, Int $b, Int $c)
    {
        $a->get()->willReturn(1);
        $b->get()->willReturn(4);
        $c->get()->willReturn(2);

        $this->addInt($a);
        $this->addInt($b);
        $this->addInt($c);

        $this->sort();
    }
}

PHP Version: 5.5.21
prophecy Versions: 1.3.1

@stof
Copy link
Member

stof commented Feb 13, 2015

The fact that usort forbids using any debug call or even instantiating exception makes it hardly usable with any other code (especially given that this code cannot know that it is called inside a usort callback, unless it uses one of the features triggering the issue). I suggest you to vote on https://bugs.php.net/bug.php?id=50688. I don't see how we could fix it in Prophecy actually (except by removing features from the library)

@everzet
Copy link
Member

everzet commented Feb 13, 2015

Vote on the issue and use @usort in the meantime :/

@mbrodala
Copy link

Apparently this was fixed with PHP 7 but not on older versions. I can confirm that @ at least suppresses the warning.

@aik099
Copy link
Member

aik099 commented Dec 16, 2015

Unfortunately no way to solve this before PHP7, because the mock objects itself tracks how many times each expectation was called and call as part of sort operation increases that counter hence the warning.

I ended up sorting array of object by key, where key value is mocked method value obtained upfront.

@aik099
Copy link
Member

aik099 commented Dec 16, 2015

Related issue: #215

@mbrodala
Copy link

I fell back to plain old PHPUnit mock objects which do not track method calls.

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

No branches or pull requests

5 participants