dimabdc / idea-php-phpunit-plugin

IntelliJ IDEA / PhpStorm PHPUnit Helper Plugin
https://plugins.jetbrains.com/plugin/14672-phpunit-helper
0 stars 0 forks source link
intellij intellij-plugin intellijidea intellijidea-plugin phpstorm phpstorm-plugin phpunit

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:

Feature list

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