shredding
shredding

Reputation: 5591

PHPUnit via Composer and PhpStorm

I'm now trying for hours to setup PhpStorm for unit testing. Whatever I do, I get this

Process finished with exit code 1
Cannot find PHPUnit in include path ...

PHPUnit is (via command line) accessible from anywhere; I've set the correct include path and have added PHPUnit to the external libraries. No chance.

Is it possible at all or does it only work with installations via PEAR? I've worked through all related questions here, am on Mac and PHPUnit is running smoothly via CLI.

Upvotes: 25

Views: 27470

Answers (7)

James
James

Reputation: 1712

Possibly helpful for others.. PHPStorm successfully found my local PHPUnit via Composer, but am prompted with "Cannot find autoloader script at 'vendor/autoload.php'" and am unable to run my tests.

Fixed this by using the absolute path, e.g. /Users/Me/my-project/vendor/autoload.php

can't find autoloader

Upvotes: 0

aderuwe
aderuwe

Reputation: 1013

Patrick Visma posted the solution on http://youtrack.jetbrains.com/issue/WI-13429:

Edit the configuration for your phpunit.xml in PHPStorm, and set the Interpreter options like this:

-d auto_prepend_file=/path/to/vendor/autoload.php

Upvotes: 24

Vasiliy P
Vasiliy P

Reputation: 21

I want to add that current version of PHPStorm (7.1) works well only with the previous version of PHPUnit (3.7). And it fails to work with 4.0 and above. If you have downloaded phpunit.phar from http://phpunit.de, you can point PHPStorm to it via Settings => PHP => PHPUnit => Path to phpunit.phar

Upvotes: 2

b01
b01

Reputation: 4384

I have found that if I have all of my in the "Test" directory, and a test suite defined in a configuration file "Test/phpunit.xml"; I can simply setup a "Run Configuration" like so:

  1. Open the Run/Debug Configuration dialog box by clicking Run | Edit Configurations.
  2. Click + on the toolbar and choose PHPUnit (for unit tests executed locally).
  3. Fill in the Name at the top with what ever you like.
  4. Set Test scope to the directory where your test are located.
  5. Click OK
  6. Then run the new configuration.

phpstorm-run-configuration-dialog

Phpstorm Run|Configuration dialog

Upvotes: 1

Adam Lynch
Adam Lynch

Reputation: 3369

@aderuwe's answer works but that config isn't scoped per project, it's for all projects. Since PHPStorm 6 (actually even EAP version PS-126.260) you can do the following:

  1. Go to File -> Settings -> PHP -> PHPUNIT.
  2. Select Use custom loader and then enter the autoload.php file from your projects vendor directory. This autoloader will autoload all the dependencies managed by Composer (including PHPUnit).
  3. Specify the default configuration file (this is usually phpunit.xml.dist from your project's app directory).

<code>PHP</code> -> <code>PHPUNIT</code>

That needs to be repeated per project, but the following needs to be done once.

You need to configure you defaults in PHPStorm correctly.

  1. Go to Run -> Edit Configurations... or just click the dropdown menu on the toolbar and select Edit Configurations....
  2. Go to Defaults -> PHPUnit
  3. Under Test Scope, select Defined in configuration file
  4. Make sure the Interpreter options textfield is empty
  5. Go to PHPUnit (above Defaults) and delete any entry under it (so that next time you a test on a particular file, etc. it'll set up a new one but using the new default).

<code>Edit Configurations...</code>

Upvotes: 62

edorian
edorian

Reputation: 38961

PHPStorm has a custom PHPUnit runner script ( ide-phpunit.php ) that it uses internally to deal with different versions of PHPUnit and to do the IDE integration and that is just relying on the include path and the PEAR folder layout.

To do that it executes:

public static function checkIncludePath()
{
    //check include path
    $PHPUnitParentDirectory = self::getPHPUnitParentDirectory();
    if (is_null($PHPUnitParentDirectory)) {
        echo "Cannot find PHPUnit in include path (" . ini_get('include_path') . ")";
        exit(IDE_PHPUnit_Loader::FAILURE_EXIT);
    }
}

/**
 * @return null | string
 */
private static function getPHPUnitParentDirectory()
{
    $pathArray = explode(PATH_SEPARATOR, ini_get('include_path'));
    foreach ($pathArray as $path)
    {
        if (file_exists($path . DIRECTORY_SEPARATOR . 'PHPUnit/')) {
            return $path;
        }
    }
    return null;
}

You could just install it via composer and add it as a normal executable (ant/phing/native task) but you will loose all the shiny features PHPStorm offers like the progress bar, jumping to a failing test and the code coverage support.

So from the code you can tell taht you need to add a folder called named PHPUnit to one of the places in your include path. Adding vendor might help there but keep in mind it needs to match case when doing so.

Until PHPStorm supports using PHPUnit from composer or a phar I don't think you will get better integration ( http://youtrack.jetbrains.com/issue/WI-13429 ) with using it from pear but hacking your include path might work out.

The PHPStorm guys did some updates on the runner for 3.7 and are usually quite responsive to changes in PHPUnit :)

Upvotes: 1

Seldaek
Seldaek

Reputation: 42036

I would guess PhpStorm relies on finding PHPUnit in the include_path of php.ini itself, not of your project.

You can try adding your local project's vendor/bin directory to php.ini's include_path, but ultimately this seems like PhpStorm might benefit from supporting composer installs and run the phpunit of the project itself if it's available. There actually is an issue opened on their tracker so I would recommend you login there and upvote it: http://youtrack.jetbrains.com/issue/WI-13429

Upvotes: 5

Related Questions