Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why PhpStorm doesn't know PHPUnit_Framework_TestCase

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.

like image 717
BeetleJuice Avatar asked Jul 28 '17 02:07

BeetleJuice


People also ask

How do I run a test case in Phpstorm?

Create a test configuration Open the Run/Debug Configuration dialog by doing one of the following: From the list on the main toolbar, select Run | Edit Configurations. From the main menu, select Run | Edit Configurations. Press Alt+Shift+F10 and select Edit Configuration from the context menu.

Can not find PHPUnit library to install use PHP composer Phar install?

First go to Settings -> Languages and Frameworks -> PHP and Add a remote interpreter, then go to Settings -> Languages and Frameworks -> PHP -> PHPUnit click the + on top and click by Remote Interpreter . If you're using Composer autoloader, then enter your full Vagrant path to your autoloader file.

How do I debug PHPUnit tests in PhpStorm?

Running and debugging PHPUnit tests. You can run and debug single tests as well as tests from entire files and folders. PhpStorm creates a run/debug configuration with the default settings and a launches the tests. You can later save this configuration for further re-use.

How do I integrate PHP-specific testing frameworks with PhpStorm?

Use this page to integrate PHP-specific testing frameworks with PhpStorm in the current project. With PhpStorm, you can run and debug PHPUnit, Behat, Codeception, and PHPSpec tests. The page consists of two panes: The central pane shows existing configurations of test frameworks for different interpreters.

Can I run PHPUnit tests outside of the project?

If you need full coding assistance in addition to the ability of running PHPUnit tests, store phpunit.phar under the root of the project where PHPUnit will be later used. If you only need to run PHPUnit tests and you do not need any coding assistance, you can save phpunit.phar outside the project.

Why is PHPUnit_framework_testcase no longer working?

This no longer works because latests PHPUnit no longer supports old class names such as PHPUnit_Framework_TestCase. You really have to use ... extends PHPUnit\Framework\TestCase Just saying that I had to first mv phpunit /usr/local/bin/phpunit and then sudo chmod +x /usr/local/bin/phpunit.


1 Answers

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

like image 198
Dmitrii Avatar answered Oct 04 '22 21:10

Dmitrii