39

I am running tests in PhpStorm and I get this error. Does anyone know what on earth could be causing it?

PHP Fatal error: Class 'PHPUnit_TextUI_ResultPrinter' not found in C:\Users\administrator1\AppData\Local\Temp\ide-phpunit.php on line 249

I run tests in other projects - that also utilise ide-phpunit.php and they run just fine.

Andrea
  • 11,801
  • 17
  • 65
  • 72
b85411
  • 9,420
  • 15
  • 65
  • 119
  • PHPUnit version? What kind of installation it is (PHAR/Composer)? PhpStorm version? – LazyOne Jul 22 '15 at 08:25
  • I figured out the issue (answer below). In the interests of helping Googlers that are troubleshooting this issue though it was a composer install, phpunit 3.7, PhpStorm 9 :) – b85411 Jul 22 '15 at 08:27

12 Answers12

29

I had the same issue with Ubuntu 16.10, phpStorm 2017.2 and Laravel 5.5

Fixed it by uninstalling phpunit from my Ubuntu-system with

sudo apt-get remove phpunit
sudo apt-get install --autoremove

or for mac:

brew uninstall phpunit
brew install phpunit

My PhpStorm configuration (File -> Settings -> Languages & Frameworks -> PHP -> Test Frameworks):

phpStorm-Configuration

Works great, now!

raveren
  • 17,799
  • 12
  • 70
  • 83
suud
  • 330
  • 1
  • 4
  • 8
  • 2
    I had this fix the same issue, with a similar setup. The only difference was Ubuntu 17.10. – polygone Nov 25 '17 at 15:01
  • 1
    @panepeter I only have one theory as to why. For me composer PHPUnit (PU) version was 6.5 and the system version was 5.1 I extended the namespaced version of the TestCase class so PU6 TestCase was autoloaded. somewhere however a PU6 class uses a class that exists in PU5, which was autoloaded from the system rather than via composer. I do not have an autoloader for the system version so I can only guess that installing phpunit modified something to include an autoloader for itself which is added before composers version. – tempcke Nov 29 '18 at 17:28
  • 1
    Fixed identical issue on Ubuntu 20.04 with the current PHPStorm 2020. I had been plagued by this for several months on all my TDD projects where I could not use PHPStorm's test runner. It didn't matter how I defined the reference to the PHPUnit executable I would get bizarre errors. Never thought about removing the distro's phpunit. – j.steelman Dec 07 '20 at 20:16
15

Silly mistake on my part... simply forgot to add phpunit as a dependency in the project. For anyone else that gets this error, to composer.json add:

"require-dev": {
    "phpunit/phpunit": "3.7.*"
},

And then run:

composer update

That solved the problem.

b85411
  • 9,420
  • 15
  • 65
  • 119
12

In my case the problem was caused by following set of reasons:

  1. I installed phpunit using composer with composer require phpunit/phpunit command. I did not pay attention that by default it used php7 and it installed phpunit6 which has class names with namespaces (PHPUnit\TextUI\ResultPrinter).
  2. IDE runs old version of phpunit which expects class names without namespaces (PHPUnit_TextUI_ResultPrinter)

I decided to reinstall phpunit running same composer command as above, but under php 5.6 (because it was important to be compatible with php5.6) and it installed phpunit 5.7 . But it is possible to go with newer version of phpunit and php: Settings > PHP > PHPUnit :: "Use composer autoloader" (set path to phpunit executable inside vendors (it was vendors/bin/phpunit in my case))

Murat Erkenov
  • 694
  • 1
  • 7
  • 15
  • 4
    I just did a 'refresh' in 'Test Framework Settings' and PhpStorm updated PhpUnit to the latest version - and that helped. – Ivan Yarych Sep 26 '18 at 07:41
  • Yes, this pointed me in the right direction, when updating phpunit in the composer.json, phpstorm config must be reloaded in order to detect the update. Thank you – funder7 Sep 18 '21 at 13:58
7

I got this error while using the various modifier-F10 keys in PHPStorm, because it didn't know where to find the correct PHPUnit.

In settings (ctrl-alt-s), search for PHPUnit. In my case I wanted to set it as follows:

  • "use composer autoloader"
  • Path to script: full-path-to-project/vendor/autoload.php (this field was empty for me, with an error showing in the dialog)

Otherwise it would try to use some cached version of the library (/tmp/ide-phpunit.php) instead of the composer version. If you prefer to use some globally installed binary, you could of course use the Path to phpunit.phar option.

okdewit
  • 2,406
  • 1
  • 27
  • 32
7

I had same problem with PHP7.1 and PHPUnit 6.3 via composer but I resolved via phpunit.phar. (in the options : select path to phpunit.phar, click on download and click on refresh)

enter image description here

Karima Rafes
  • 1,016
  • 10
  • 19
3

After updating to latest composer, composer run-script drupal-phpunit-upgrade, PHPStorm was still mapped to the old version even though I was loading through autoloader. I went to Languages & Frameworks > Php > Test Frameworks and hit the refresh button on the same line as "Path to Script".

Now my PHPUnit version reads 6.5.8.

powpow12
  • 587
  • 8
  • 13
1

In my case, in phpstorm preferences -> php -> CLI interpreter was set on local, when it needed to use remote php 7 (ubuntu).

Because I use vagrant box to have my environment configured on ubuntu.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
0

I recently came across similar error PHPUnit_TextUI_ResultPrinter not found in TeamCity.php on line 19

I am using PhpStorm 2018.3.3. Upon using terminal from PhpStorm $ phpunit PHPUnit 7.5.1 by Sebastian Bergmann and contributors.

from my Ubuntu system $ phpunit --version PHPUnit 5.1.3 by Sebastian Bergmann and contributors.

I had to make sure the my Ubuntu system PHPUnit is updated to 7.5.1. So here's what I did:

sudo wget https://phar.phpunit.de/phpunit-7.5.1.phar
sudo chmod +x phpunit-7.5.1.phar
sudo mv phpunit-7.5.1.phar /usr/local/bin/phpunit

Then I went to PhpStorm: File > Settings > Language & Frameworks > PHP > Test Frameworks and made sure that the PHPUnit version is also 7.5.1

And then from PhpStorm terminal: phpunit and the test cases were running perfectly fine.

Digvijay
  • 7,836
  • 3
  • 32
  • 53
0

I went to the settings and set my path to php as follows: (according to the brew)

/usr/local/opt/php/bin/php

Then phpunit works.

Ostap Brehin
  • 3,240
  • 3
  • 25
  • 28
0

I opened the php extension mbstring to solved this problem

Aroad
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 19 '23 at 20:27
0

If you use a remote CLI Interpreter (e.g. Linux through WSL) as your project default, with the default PHPUnit configuration set to Local, you may get that error.

Add a Remote Interpreter PHPUnit configuration instead.

Go to (PHPStorm settings) -> PHP -> Test Frameworks

PHPStorm Remote Interpreter config

Add new PHPStorm Remote Interpreter config

I got the error with PHP 8.2 + PHPUnit 10 + PHPStorm 2023.1.2

sudoqux
  • 2,358
  • 1
  • 14
  • 15
-4

I got the same error. Solved by using phpunit4.8 (older version)

  • because there are several better solutions above, downgrading phpunit should not be considered an acceptable solution. In my experience this issue happens when you have more than 1 phpunit version (one system, one composer vendor). downgrading one would only work if by chance it made the 2 versions compatible with each other. – tempcke Nov 29 '18 at 17:33