Class 'PhpUnit\Framework\TestCase' not found
Hi there,
I'm trying to get PHPUnit running within my PhpStorm project.
PHPUnit is required in the composer.json:
"phpunit/phpunit": "^7.3.3"
PHPUnit is also successfully recognized in the PhpStorm settings:

But if I run the test, I get this:

And...

Which means PhpStorm does not always use the PHPUnit version 7.3.3 which is set up in the settings to the autoload.php.
What's wrong?
Thanks in advance!
Flo
Please sign in to leave a comment.
Did you provide the path to autoload script in the project's vendor folder?
Yup, have a look at the first screenshot.
#Edit#
It seems like PhpStorm does use the PHPUnit (3.7.21) from XAMPP, not the PHPUnit (7.3.3) via autoloader as defined in the PhpStorm settings...
#Edit2#
Nope, it doesn't: I've already checked this.
PhpStorm does not find the PhpUnit in the vendor directory, but I don't understand why.
What is the full command that IDE tries to execute? What are the full errors (interested in file paths)?
Have you specified that you are using "vendor/autoload.php" anywhere in PHPUnit files (e.g. phpunit.xml)? Based on errors and screenshots -- nope.
Please point to autoload.php in "Default bootstrap file" field.
So far, especially based on your 2nd screenshot, the autoload.php is not used anywhere so PHP itself is unable to locate the needed classes.
For reference purposes: same on StackOverflow -- https://stackoverflow.com/q/52160783/783119
Thanks for the link to stackoverflow, Andriy.
Yes, actually it seems like PHP does not know the autoload.php, but I don't know why.
Otherwise I had to require the autoload.php within the test classes, but that's not the composer module principle, is it?
PHP knows the autoloader once this composer module ist installed in another project...
#Edit#
Full paths are:
C:\xampp\php\php.exe C:/Users/flb/dev/composer/newsletter/vendor/phpunit/phpunit/phpunit --no-configuration Flo\Newsletter\FirstTest C:\Users\flb\dev\composer\newsletter\tests\FirstTest.php --teamcity
Fatal error: Class 'PhpUnit\Framework\TestCase' not found in C:\Users\flb\dev\composer\newsletter\tests\FirstTest.php on line 9
Do I have to specify the autoload.php within PHPUnit files (such as phpunit.xml)? I thought the autoloader / composer does this part?!
>Otherwise I had to require the autoload.php within the test classes, but that's not the composer module principle, is it?
Of course not.
>PHP knows the autoloader once this composer module ist installed in another project...
?? No idea what you mean here, sorry.
So far (right now) for me it looks like some basic misconfiguration from your end.
The "vendor/autoload.php" file still must be loaded by YOUR code in some way. It can be used as actual bootstrap file, or most often -- using own bootstrap file (to bootstrap your own code) and reference that file there at first lines. You can then also specify the bootstrap file in your phpunit.xml so PHPUnit will load it and PHP itself will be able to locate all other classes.
Remember one thing: you should be able to run your PHPUnit tests outside of IDE without relying on it (i.e. IDE is not present). If you can run your tests this way then it means that your tests/PHPUnit is configured properly and the IDE influence/possible misconfiguration will be a separate moment.
Got it! Just marked it as 'solved' on stackoverflow... It was the wrong namespace >_<
What do you mean with "It can be used as actual bootstrap file"?
Where do you use the autoloader.php?
Composer does it: If you install a new composer module, composer also rebuild the autoloader.php.
For example (the overall idea):
PROJECT_ROOT/phpunit.bootstrap.php
You can reference that phpunit.bootstrap.php in your phpunit.xml (https://phpunit.de/manual/6.5/en/appendixes.configuration.html -- look for "bootstrap" attribute) or in that filed in IDE settings (up to you, but for majority of projects specifying in phpunit.xml should be the ideal case)
Then tell IDE to use that phpunit.xml as your config file (there is a separate field on your first screenhot).
If you do not need to bootstrap your tests (tests are simple and you create everything you need in actual test files), then you can specify actual "vendor/autoload.php" as a bootstrap file directly.
>Got it! Just marked it as 'solved' on stackoverflow... It was the wrong namespace >_<
Good then. That's something that always slips my mind when looking at someone's code... Because you would expect that this part is written properly from the start...
Thanks for the bootstrap advice - I'll consider that.
And yup, I've expected that, too, but I don't know why the namespace has been wrong, just using the PhpStorm auto suggests...
Thanks anyway! :)