0
class MainTest extends TestCase
{
    public function testMain()
    {
        $stub = $this->createMock(Project\NotImplementedClass::class);

        $stub->method('doSomething')
            ->will($this->returnCallback(function ($string) {
                return strtoupper($string);
            }));

        $this->assertEquals('ABC', $stub->doSomething('abc'));
    } 
}

PhpStorm tells that method doSomething doesn't exists. I searched any plugin which can autocomplete methods. Is any plugin for this?

1
  • Where are you expecting doSomething to be defined? If this is just a "hello world" unit test you're creating, why not define a method yourself and stub it out? Commented Jun 20, 2016 at 8:55

2 Answers 2

1

PHPStorm's autocomplete relies heavily on type hints. In your case - since $this->createMock() will return a PHPUnit_Framework_MockObject_MockObject which does not have the method it will complain.

What you can do is "overwrite" the type hint for the variable:

/** @var Project\NotImplementedClass|PHPUnit_Framework_MockObject_MockObject $stub */
$stub = $this->createMock(Project\NotImplementedClass::class);

or you could put the mock creation in a method with a similar @return docblock.

This will tell PHPStorm to look at both classes for autocomplete.

Sign up to request clarification or add additional context in comments.

Comments

0

We use the Dynamic Return Type-plugin to improve the type hinting of PHPUnit. It's not perfect, but is easy to set up and use. The plugin let you define return types for methods based on the value of a parameter.

Add the file dynamicReturnTypeMeta.json to the root of your project with the following contents:

{
    "methodCalls": [
        {
            "class": "\\PHPUnit_Framework_TestCase",
            "method": "createMock",
            "position": 0,
            "mask": "%s|PHPUnit_Framework_MockObject_MockObject"
        }
    ],
    "functionCalls": []
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.