How to get code auto-completion with phpspec
Asked Answered
C

1

15

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?

Copywriter answered 17/11, 2013 at 10:59 Comment(0)
C
20

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:

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

Canzona answered 23/11, 2013 at 10:7 Comment(2)
This just solved so many problems of mine :) I have yet to see that @mixin annotation before. Thank you. Thank you.Kerwon
Great workaround. Is there also a workaround for prophecies? Like $this->doSomething(':D')->willReturn('something');?Heder

© 2022 - 2024 — McMap. All rights reserved.