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

Cannot use "self" return type in mocked class #226

Closed
mariusz-kraj-c opened this issue Nov 20, 2016 · 2 comments
Closed

Cannot use "self" return type in mocked class #226

mariusz-kraj-c opened this issue Nov 20, 2016 · 2 comments

Comments

@mariusz-kraj-c
Copy link

I created entity class:

class User {
    public function setName(string $name) : self {
        $this->name = $name;
        return $this;
    }
}

Then in test tried to create mock by

$userMock = \Phake::mock(User::class);

but test execution just halt at that line without any error. After some investigation i found out that mock content created by phake contains something like that:

/**
 * @param string $name
 *
 * @return User
 */
public function setName(string $name) : self
{
    $__PHAKE_args = array();
    $funcGetArgs = func_get_args();
    $__PHAKE_numArgs = count($funcGetArgs);
    if (0 < $__PHAKE_numArgs) $__PHAKE_args[] =& $refund;
    for ($__PHAKE_i = 1; $__PHAKE_i < $__PHAKE_numArgs; $__PHAKE_i++) $__PHAKE_args[] = func_get_arg($__PHAKE_i);


    $__PHAKE_info = Phake::getInfo($this);
    if ($__PHAKE_info === null) {
        return null;
    }

    $__PHAKE_funcArgs = func_get_args();
    $__PHAKE_answer = $__PHAKE_info->getHandlerChain()->invoke($this, 'addRefund', $__PHAKE_funcArgs, $__PHAKE_args);

    $__PHAKE_callback = $__PHAKE_answer->getAnswerCallback($this, 'addRefund');

    if ($__PHAKE_callback instanceof Phake_Stubber_Answers_ParentDelegateCallback)
    {
        $__PHAKE_result = $__PHAKE_callback($__PHAKE_args);
    }
    else
    {
        $__PHAKE_result = call_user_func_array($__PHAKE_callback, $__PHAKE_args);
    }
    $__PHAKE_answer->processAnswer($__PHAKE_result);
    return $__PHAKE_result;
}

the issue is that "self" mean in my case "User_PHAKE58319e767bab31" so the declaration is incompatibile with User class.

The simples fix (that I uses) is to explicy use "User" as return value.

I don't know if this is real issue but I wanted to save someone time searching for fix :)

@mlively
Copy link
Collaborator

mlively commented Nov 20, 2016

Good find. This is definitely fixable.

On Sun, Nov 20, 2016, 5:12 AM mariusz-kraj-c notifications@github.com
wrote:

I created entity class:

class User {
public function setName(string $name) : self {
$this->name = $name;
return $this;
}
}

Then in test tried to create mock by

$userMock = \Phake::mock(User::class);

but test execution just halt at that line without any error. After some
investigation i found out that mock content created by phake contains
something like that:

/**

  • @param string $name
    *

  • @return User
    */
    public function setName(string $name) : self
    {
    $__PHAKE_args = array();
    $funcGetArgs = func_get_args();
    $__PHAKE_numArgs = count($funcGetArgs);
    if (0 < $__PHAKE_numArgs) $__PHAKE_args[] =& $refund;
    for ($__PHAKE_i = 1; $__PHAKE_i < $__PHAKE_numArgs; $__PHAKE_i++) $__PHAKE_args[] = func_get_arg($__PHAKE_i);

    $__PHAKE_info = Phake::getInfo($this);
    if ($__PHAKE_info === null) {
    return null;
    }

    $__PHAKE_funcArgs = func_get_args();
    $__PHAKE_answer = $__PHAKE_info->getHandlerChain()->invoke($this, 'addRefund', $__PHAKE_funcArgs, $__PHAKE_args);

    $__PHAKE_callback = $__PHAKE_answer->getAnswerCallback($this, 'addRefund');

    if ($__PHAKE_callback instanceof Phake_Stubber_Answers_ParentDelegateCallback)
    {
    $__PHAKE_result = $__PHAKE_callback($__PHAKE_args);
    }
    else
    {
    $__PHAKE_result = call_user_func_array($__PHAKE_callback, $__PHAKE_args);
    }
    $__PHAKE_answer->processAnswer($__PHAKE_result);
    return $__PHAKE_result;
    }

the issue is that "self" mean in my case "User_PHAKE58319e767bab31" so the
declaration is incompatibile with User class.

The simples fix (that I uses) is to explicy use "User" as return value.

I don't know if this is real issue but I wanted to save someone time
searching for fix :)


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#226, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAKwFjPCMKTHBCPsw-3dudc0JhvCxAXiks5rAEc_gaJpZM4K3iJt
.

@Daanvm
Copy link
Contributor

Daanvm commented Nov 29, 2017

I ran into this issue as well, I've created PR #254 to fix this.

bfeaver added a commit that referenced this issue Jan 18, 2018
Support self return type in mocked class, fixes #226
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