9

I'm trying to configure PhpStorm 2017.2 to use PhpUnit 5 for my PHP 5.6 project.

  1. I've downloaded the phpunit-5.7.21.phar file from the official source and placed it in my PHP 5.6 installation dir.

  2. In PhpStorm Settings >> Languages & Frameworks >> PHP >> Test Frameworks, I've linked to the .phar executable and set the default config file to a phpunit.xml in the project root directory

enter image description here

  1. Here are the contents of phpunit.xml:

.

<?xml version="1.0" encoding="UTF-8"?>

<phpunit>
    <testsuites>
        <testsuite name="Test suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

I'm trying to structure tests in a tests/unit directory within which my source file project structure would be mirrored as described in the manual. For instance:

// project files:
ClassOne.php
vendor/
  ClassTwo.php
  Utility.php

// test files
tests/unit/
  ClassOneTest.php
  vendor/
    ClassTwoTest.php
    UtilityTest.php

I have two problems though:

First, I don't know how to configure PhpStorm to create tests within tests/unit/ mirroring the sructure with respect to the project root. When I create a test, by default the file is put in the same directory as the project file.

Secondly, I don't know how to get PhpStorm to index the PHPUnit source code. Even though I've linked to phpunit-5.7.21.phar file as shown above, the IDE complains when I create a test:

namespace vendor;
class UtilityTest extends \PHPUnit_Framework_TestCase{}

Undefined class PHPUnit_Framework_TestCase

Update 1

I solved the 2nd problem by adding the directory where I had saved the .phar to the PhpStorm include path, set in Settings >> Languages & Frameworks >> PHP >> Include Path tab. Alternatively, I could just put the .phar file within the project directory and it will be indexed.

I still need help with my first problem.

Update 2

Thanks to Ástþór's answer I figured out how to get PhpStorm to mirror the project structure within a dedicated tests directory. Go to PhpStorm Settings >> Directories and select the base testing directory. The click Test near the top to mark it as a Test Sources Root

enter image description here

The next time you create a test, it will automatically be placed in that directory.

1
  • 1
    If you downvote, please leave feedback as to why so I can learn what makes this a poor question in your opinion. Commented Jul 28, 2017 at 2:37

1 Answer 1

3

Mark a directory with your source classes as "Source root" and a directory with tests as "Test Sources Root". After that directories will be pre-filled on a test creation (e.g. via ctrl+shift+T on a source class).
Not sure though if that would work fine with your "mirroring" system: I guess you would still have to manually adjust directories for the part of your tests

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'm looking up how to declare a folder as a "Test Sources Root" in PhpStorm. When I right-click the tests/ directory the options I see under "Mark Directory As" are Resource Root, Excluded and Sources Root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.