Skip to content

dimabdc/idea-php-phpunit-plugin

 
 

Repository files navigation

IntelliJ IDEA / PhpStorm PHPUnit Helper

Build Status IntelliJ Plugin Compatibility

PhpStorm plugin to provide smart autocomplete, code navigation and refactoring features for mocked class methods. Supported all versions of PhpStorm since 2020.1

Key Value
Plugin Url https://plugins.jetbrains.com/plugin/14672-phpunit-helper
ID com.dimabdc.idea.php.phpunit
Changelog CHANGELOG
Build and Deployment MAINTENANCE
Origin Fork Haehnchen/idea-php-phpunit-plugin

Installation

Stable version, JetBrains repository:

  • Go to PhpStorm -> Preferences... -> Plugins -> Install Plugin from Disk ... and select PHPUnit Helper plugin
  • Restart PhpStorm

Feature list

  • method autocomplete for class, abstract class and trait mock objects;
    • type providers: getMock, getMockForAbstractClass, etc. will return mock object with methods of mocking class and PHPUnit\Framework\MockObject\MockObject;
    • supported PHPUnit methods:
      • PHPUnit\Framework\MockObject\MockBuilder::setMethods
      • PHPUnit\Framework\MockObject\MockBuilder::onlyMethods
      • PHPUnit\Framework\MockObject\MockBuilder::addMethods
      • PHPUnit\Framework\MockObject\TestCase::getMock
      • PHPUnit\Framework\MockObject\TestCase::getMockClass
      • PHPUnit\Framework\MockObject\TestCase::getMockForAbstractClass
      • PHPUnit\Framework\MockObject\TestCase::getMockForTrait
      • PHPUnit\Framework\MockObject\Builder\InvocationMocker::method
      • PHPUnit\Framework\MockObject\MockObject::method
  • code navigation (go to declaration, find usages, etc.) and refactoring (rename methods);
  • highlighting of incorrect method usages;
  • Prophecy support.

Mocks

/** @var $x \PHPUnit\Framework\TestCase */
$x->createMock(Foo::class)->bar();
/** @var $x \PHPUnit\Framework\TestCase */
$x->prophesize(Foo::class)->bar();
class Foo extends \PHPUnit\Framework\TestCase
{
   public function foobar()
   {
       $foo = $this->createMock(Foo::class);
       $foo->method('<caret>')
   }
}
class Foo extends \PHPUnit\Framework\TestCase
{
   public function setUp()
   {
       $this->foo = $this->createMock('Foo\Bar');
   }
   public function foobar()
   {
       $this->foo->method('<caret>');
   }
}
class Foo extends \PHPUnit\Framework\TestCase
{
   public function setUp()
   {
       $this->foo = $this->createMock('Foo\Bar');
   }
   public function foobar()
   {
       $this->foo->bar();
   }
}
class FooTest extends \PHPUnit\Framework\TestCase
{
    public function setUp()
    {
        $this->foo = $this->prophesize(Foo::class);
    }
    public function testFoobar()
    {
        $this->foo->getBar()->willReturn();
    }
}
class FooTest extends \PHPUnit\Framework\TestCase
{
    public function testFoobar()
    {
        $foo = $this->getMockBuilder(\Foo::class)
            ->onlyMethods([
                'getFoobar',
            ])
            ->addMethods([
                'getFoobaz',
            ])
            ->getMock();
        $foo->expects($this->once())
           ->method('<caret>');
    }
}

Prophecy

class FooTest extends \PHPUnit\Framework\TestCase
{
    public function testFoobar()
    {
        $foo = $this->prophesize(Foo::class);
        $foo->getBar()->willReturn();
    }
}
class FooTest extends \PHPUnit\Framework\TestCase
{
    public function setUp()
    {
        $this->foo = $this->prophesize(Foo::class);
    }
    
    public function testFoobar()
    {
        $this->foo->getBar()->willReturn();
    }
}
class FooTest extends \PHPUnit\Framework\TestCase
{
    public function testFoobar()
    {
        $foo = $this->prophesize(Foo::class);
        $foo->reveal()->getBar();
    }
}

Intention / Generator

Use intention / generator to add new method mocks. Every caret position inside a mock object is detected

$foo = $this->getMockBuilder(Foobar::class)->getMock();
$foo->method('getFoobar')->willReturn();

$foo = $this->createMock(Foobar::class);
$foo->method('getFoobar')->willReturn();
$this->foobar = $this->getMockBuilder(Foobar::class)->getMock();
// ...
$this->foobar->method('getFoobar')->willReturn();

$this->foobar = $this->createMock(Foobar::class);
// ...
$this->foobar->method('getFoobar')->willReturn();
new Foobar();
// ...
new Foobar(
    $this->createMock(Foo::class),
    $this->createMock(FooBar::class)
);
/**
 * @expectedException \Foo\FooException
 */
public function testExpectedException()
{
    $foo = new FooBar();
    $foo->throwFooException();
}

Examples

\PHPUnit\Framework\MockObject\MockBuilder::onlyMethods

\PHPUnit\Framework\MockObject\MockBuilder::addMethods

\PHPUnit\Framework\MockObject\Builder\InvocationMocker::method

\PHPUnit\Framework\MockObject\Builder\InvocationMocker::method

PHPUnit Runner LineMarker

PHPUnit Prophecy

Navigation

Navigation