/idea-php-phpunit-plugin

IntelliJ IDEA / PhpStorm PHPUnit Enhancement Plugin

Primary LanguageJava

IntelliJ IDEA / PhpStorm PHPUnit Enhancement

Build Status Version Downloads Downloads last month Donate to this project using Paypal

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

Key Value
Plugin Url https://plugins.jetbrains.com/plugin/9674
ID de.espend.idea.php.phpunit
Changelog CHANGELOG
Origin Fork maxfilatov/phpuaca

Installation

Stable version, JetBrains repository:

  • Go to PhpStorm -> Preferences... -> Plugins -> Browse repositories ... and search for PHPUnit Enhancement 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_TestCase::getMock
      • PHPUnit_Framework_TestCase::getMockClass
      • PHPUnit_Framework_TestCase::getMockForAbstractClass
      • PHPUnit_Framework_TestCase::getMockForTrait
      • PHPUnit_Framework_MockObject_Builder_InvocationMocker::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 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 setUp()
        {
            $this->foo = $this->getMockBuilder(\Foo::class);
        }
        public function testFoobar()
        {
            $this->foo->getMock()->bar();
        }
    }

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::setMethods

PHPUnit_Framework_MockObject_Builder_InvocationMocker::method

PHPUnit_Framework_MockObject_Builder_InvocationMocker::method

PHPUnit Runner LineMarker

PHPUnit Prophecy

PHPUnit Expected exception