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

Change of behavior when calling parent methods statically #280

Open
theilig opened this issue Nov 7, 2019 · 5 comments
Open

Change of behavior when calling parent methods statically #280

theilig opened this issue Nov 7, 2019 · 5 comments

Comments

@theilig
Copy link

theilig commented Nov 7, 2019

Noticed this while trying to upgrade from 2.1.1 to 3.1.6

class behaviorChange {
    public static function tryB() {
        return static::b();
    }
    public function b() {
        return "here";
    }
}

class behaviorChangeTest {
    public function testTryB() {
        $mock = Phake::mock('behaviorChange');
        Phake::whenStatic($mock)->tryB->thenCallParent();
        Phake::whenStatic($mock)->b->thenReturn('there');
       print_r($mock::tryB());
    }
} 

in 2.1.1 the created Phake class called directly into the parent, which kept the late static binding associated to the mock, resulting in the static::b call correctly being mocked as well (returning 'there')

however in 3.1.6 the Phake class calles ParentDelegateCallback->__invoke()
which calls invokeArgs, which calls reflectionMethod with a null context. This means the late static binding will no longer be the mock, but instead be the SUT. static::b then does not hit the mock (returning 'here')

I suspect passing the mock as the context would fix it rather than null, but without looking it doesn't seem like an easy thing to do

@webkod3r
Copy link

I'm experiencing the same issue. I have a class mocking a static method, but when called internally it doesn't call the mocked one but instead it makes the actual call. Any idea or workaround?

@theilig
Copy link
Author

theilig commented Apr 17, 2023

here's a workaround patch, it's not ideal because it removes anything you might want from the Phake stubber callback, but it's what we've been doing for the last 3 years with success

+++ b/src/Phake/Stubber/Answers/ParentDelegate.php
@@ -81,14 +81,7 @@ class Phake_Stubber_Answers_ParentDelegate implements Phake_Stubber_IAnswer
 
             if (!$reflMethod->isAbstract())
             {
-                if (defined('HHVM_VERSION'))
-                {
-                    return array('parent', $method);
-                }
-                else
-                {
-                    return new Phake_Stubber_Answers_ParentDelegateCallback($context, $reflMethod);
-                }
+                return array('parent', $method);
             }
         }
         catch (ReflectionException $e)

@webkod3r
Copy link

Thanks @theilig, this behavior was changed in v2.2.1 and instantly broke the test my team had in their repo. Changing this will imply making changes to the vendor dependencies which I"m not a big fan of. But I guess we don't have other options. Thanks.

@theilig
Copy link
Author

theilig commented Apr 17, 2023 via email

@adoy
Copy link
Member

adoy commented Apr 17, 2023

I'll have a look at if/how we can configure that

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

3 participants