Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get code auto-completion with phpspec

I've just started learning phpspec with a view to replacing PHPUnit. Unfortunately, I've got rather hooked on using the PHPStorm editor's code completion feature, which makes even PHPUnit's verbose mock interface very quick to type.

No such luck with phpspec. Given a class like this:

<?php

namespace spec\MyVendor\MyClass;

use PhpSpec\ObjectBehavior;

class MyClassSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType('MyVendor\MyClass');
    }

    function it_should_do_something()
    {
        $this->???
    }
}

Firstly, shouldHaveType is showing the 'method not found' highlight, and secondly, if I try to autocomplete at the ??? point, my options are limited to the few methods in ObjectBehaviour. I would want to see things like shouldHaveType, shouldImplement, and many more.

I've found this phpspec-stubs repository on Github, but it seems to only have one method defined, and requires extending a wrapper class.

There's also a PHPStorm plugin but it's not clear to me if this should provide auto-complete, and the current version gives me a NullPointerException in PHPStorm immediately on entering any PHP file.

So, are all you phpspec users typing a lot, or is there another solution?

like image 646
fazy Avatar asked Nov 17 '13 10:11

fazy


1 Answers

Update: PhpStorm has a built in support for PhpSpec since 2016.3. We can only expect it will be improved with each new release.

The PhpStorm plugin for PhpSpec from the question is not really maintained, but you could try another solution. Since PhpStorm 7 it is possible to use the @mixin annotation:

<?php

namespace spec\MyVendor;

use MyVendor\MyClass;
use PhpSpec\ObjectBehavior;

/**
 * @mixin MyClass
 */
class MyClassSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType('MyVendor\MyClass');
    }

    function it_should_do_something()
    {
        $this->doSomething(':D');
    }
}

Sources:

  • http://blog.jetbrains.com/phpstorm/2013/11/phpstorm-7-1-eap-133-51/
  • http://youtrack.jetbrains.com/issue/WI-1730#

For proper PhpSpec support in PHPStorm give your vote here : https://youtrack.jetbrains.com/issue/WI-22670

like image 119
Jakub Zalas Avatar answered Nov 12 '22 09:11

Jakub Zalas