phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

API of `InvokedRecorder` / `InvokedCount` has BC - `getInvocations()` is missing

Open ddinchev opened this issue 7 years ago • 6 comments

Q A
PHPUnit version 8.4.1
PHP version 7.3.7
Installation Method Composer

Summary

Up to version 8.4.0 (inclusive), there was a public method in PHPUnit\Framework\MockObject\Matcher\InvokedRecorder called getInvocations(). It was useful to intercept parameters of actual calls as the example below demonstrates.

In this refactoring, getInvocations() has been silently removed, causing some of our tests to start failing. The change has not been documented. https://github.com/sebastianbergmann/phpunit/commit/7f664ffd558ba68031963f63d3e23a951ba441fc#diff-97c3754e481a4ef04940669da64638a9L32

Previous Behavior

This behavior is also what comes up as suggestions on stack overflow when looking for how to intercept mock call parameters.

            $this->mockedService
                ->expects($singleInvocationSpy = $this->once())
                ->method('save');
            
            $this->executeCodeThatWouldCallTheMockedService();

            $invocations = $singleInvocationSpy->getInvocations();
            $args = $invocations[0]->getParameters();
            $firstArgumentOfSave = $args[0];
            $secondArgumentSave = $args[1];
            
           $this->assertEquals('expected', $firstArg);
           $this->assertEquals('expected2', $secondArg);

Current Behavior

As of 8.4.1, the tests fail as getInvocations() method no longer exists.

I understand that this likely is not intended usage of the InvokedRecorder class (and it was renamed to reflect that) but it still would be better if the change was mentioned in the changelog.

There are articles and stack overflow posts that suggest the previous behavior.

ddinchev avatar Oct 09 '19 09:10 ddinchev

7f664ffd558ba68031963f63d3e23a951ba441fc is also part of 8.4.0 so I do not understand Up to version 8.4.0 (inclusive).

That being said, your code was relying on private implementation details of PHPUnit. Changes to private implementation details are not documented in the ChangeLog as they are not backward compatibility breaks.

sebastianbergmann avatar Oct 09 '19 11:10 sebastianbergmann

@sebastianbergmann I'm not sure it's fair to call it a "private implementation detail", as the method was public

Jelle-S avatar Nov 06 '19 08:11 Jelle-S

It was annotated with @internal.

sebastianbergmann avatar Nov 06 '19 08:11 sebastianbergmann

I'm affected by this too.

What would be the best way for inspecting the parameters passed to a function without using getInvocations()?

I can implement it and make a PR if needed

omoustaouda avatar Nov 27 '19 15:11 omoustaouda

A possible workaround is using reflection:

        $this->myMock
            ->expects($spy = $this->once())
            ->method('wakeUp');

        $spyParentProperties = TestHelpers::getParentPrivateProperties($spy);
        $invocations = $spyParentProperties['invocations'];
        $invocationParams = $invocations[0]->getParameters();

Where getParentPrivateProperties() can be:

    /**
     * This function determines the parent of the class instance provided, and returns all properties of its parent.
     *
     * Result structure:
     *  Array =>[
     *     'parentPropertyName1' => 'value1'
     *     'parentPropertyName2' => 'value2'
     *     ...
     *  ]
     *
     * @param $instance
     * @return array
     * @throws \ReflectionException
     */
    public static function getParentPrivateProperties($instance): array
    {
        $reflectionClass = new ReflectionClass(get_class($instance));
        $parentReflectionClass = $reflectionClass->getParentClass();

        $parentProperties = [];

        foreach ($parentReflectionClass->getProperties() as $p) {
            $p->setAccessible(true);
            $parentProperties[$p->getName()] = $p->getValue($instance);
        }

        return $parentProperties;
    }

Note: This solution relies on an internal PHPUnit variable (invocations). i.e.: if in the PHPUnit codebase, there are changes on invocations, this workaround might need an update.

omoustaouda avatar Nov 28 '19 14:11 omoustaouda

The original feature has been dropped and is not coming back: https://github.com/sebastianbergmann/phpunit/pull/4447

afilina avatar Sep 08 '20 12:09 afilina