PhpStorm: Class 'PhpUnit\Framework\TestCase' not found (composer / autoload) - php

I've got a problem with PhpStorm, composer and PHPUnit.
Windows 8.1 Pro (64 bit)
PhpStorm is up to date: 2018.2.2
Tried different PHP interpreters like XAMPP and a clean PHP for win
PHPUnit is required by composer with: "phpunit/phpunit": "^7.3.3".
PHPUnit is successfully installed via composer to the vendor directory as well.
PHPUnit is recognized from PhpStorm:
My test class extends the PhpUnit\Framework\TestCase class and when you run the test, the following happens:
First it seems like PhpStorm loads the old PHPUnit (3.7.21) from XAMPP's PHP and not the recognized PHPUnit (7.3.3) as setup in the PhpStorm settings / installed to the vendor folder.
But I don't think so.
I think PhpStorm tries to load the PhpUnit\Framework\TestCase class by the autoloader, but I don't know why it doesn't find the PHPUnit...
Thanks in advance!
The whole project could be minimized to this simple test class:
The composer.json looks like the following:
"autoload": {
"psr-4": {
"Flo\\Newsletter\\": "src/"
}
},
"require": {
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^7.3.3"
}

Namespaces are case-sensitive. Please change PhpUnit to PHPUnit in the import.
Although PhpStorm could really detect that. Here's a feature request for that: https://youtrack.jetbrains.com/issue/WI-38140

Related

Composer not working after specifying platform php version

I am working on a project for a WebApp and I would like to specify types for my class properties. I didn't realise this is a PHP version 7.4.* feature, so I read this and updated my composer.json to include the relevant material:
{
"name": "srmes/shopping-app-test",
"description": "an assignment from `scandiweb.com`. A simple php-based web application to display and inventory a range of products",
"require-dev": {
"phpunit/phpunit":"~9.0",
"squizlabs/php_codesniffer": "~3.0"
},
"require": {
"doctrine/orm": "~2.7",
"php": "7.4.4"
},
"config": {
"platform": {
"php": "7.4.4"
}
},
"autoload" : {
"psr-4": {
"WebApp\\": "src/"
}
},
"autoload-dev" : {
"psr-4": {
"WebApp\\Tests\\" : "tests/"
}
}
}
I then ran composer install and composer update.
No problems seemed to occur with the install, except that now my phpunit tests don't run:
PHPUnit 9.1.1 by Sebastian Bergmann and contributors.
Time: 58 ms, Memory: 4.00 MB
No tests executed!
And all composer commands give the following error:
Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /Users/ScottAnderson/Documents/Tech/commissions/shopping_app_test/vendor/ocramius/package-versions/src/PackageVersions/Installer.php on line 33
Ironically this is syntax exception about the php feature I was trying to utilise! I can't even run composer -vvv to debug which php executable is being used by composer.
In order to resolve this should I use a package like phpbrew to make directory environments of php?
My assumption was that after requiring php 7.4.4 in composer.json that the correct php executable would be installed and used by composer and phpunit
Looks like you are not running php 7.4. and the dependencies installed (here phpunit and PackageVersion) need it.
Sometimes composer runs commands hooked on events and if the command fails everything else can fall.
Delete the vendor folder.
Delete composer.lock
Remove the constraint on php 7.4 version in your composer.json
Run composer update
You should be alright 🤗
Btw composer manages project dependencies, not php versions installed.
If you're starting out in PHP I'd recommend to stick with 7.3 which is widely available. Honestly you don't need the latest syntax additions to the language.

PhpStorm, Yii2 Basic and Codeception

I have configuration:
Open Server 5.2.8 (WAMP)
PhpStorm 2017.3.4
Yii 2.0.14 basic
I added to the %PATH% path to PHP and to the Codeception folder:
In the PhpStorm terminal I launched the codecept run command and everything looks good. But the Windows terminal does not maintain colors therefore I wanted to launch tests through PhpStorm.
I made such settings for PHPUnit and Codeception (from Yii2\vendor):
But for some reason I receive such error:
You are missing the dependency for phpunit, add the following to your composer.json
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
and run
composer update
Answers here and here. Thanks to #panosru.
Just download new version PHPStorm 2017.3.6.

Class 'PHPUnit_Extensions_SeleniumTestCase' not found

I installed phpunit-selenium using composer according to here; https://phpunit.de/manual/3.7/en/selenium.html
The problem is that I only have selenium 2 for phpunit. PHPUnit_Extensions_Selenium2TestCase.php is there. But the file PHPUnit_Extensions_SeleniumTestCase.php does not exist in the extension map where it supposed to be. So selenium 1 function can not be used.
How can I get selenium 1 for php unit using composer (on a windows machine)?
I know there is an answer involving pear; How do I solve this error: "Class PHPUnit_Extensions_SeleniumTestCase could not be found"
But pear is not preferred because composer is shared with the team. It would save installing it.
Solved it. I needed to move back a version in phpunit-selenium:
"require-dev": {
"phpunit/phpunit-selenium": "1.4.*"
}
instead of using the latest version which probably only works with selenium 2.
"require-dev": {
"phpunit/phpunit-selenium": ">=1.2"
}

Laravel phpspec cannot be autoloaded

I am trying to setup phpspec in a Laravel project.
I have installed phpspec properly and am running phpspec inside my project without any trouble.
My composer.json file has the following relevant lines:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Acme\\": "app/Acme"
}
},
My phpspec.yml:
suites:
app_suite:
namespace: Acme
src_path: app
spec_path: app
spec_prefix: spec
Executing phpspec describe Acme/Foo created app\spec\Acme\FooSpec.php file.
Executing phpspec run created app\Acme\Foo.php
But I am getting the following error when I run "phpspec run" -
The type Acme\Foo was generated but could not be loaded. Do you need
to configure an autoloader?
Everything is fine, except for the auto-loading error. How can I solve the issue?
try
composer dump-autoload
It just regenerates the list of all classes that need to be included, when you have a new class inside your project

Make CakePHP use local PHPUnit & PHPUnit Selenium installed via Composer

On my computer I have a CakePHP Project. And inside the cakephp project I have a composer.json file like below:
{
"name": "a/b",
"description": "c",
"require-dev": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit-selenium": ">=1.2"
},
"license": "Proprietary",
"authors": [
{
"name": "d",
"email": "e"
}
],
"minimum-stability": "f"
}
After running composer install I have phpunit and the selenium package installed and working here
/cakephpproject/vendor/bin/phpunit
/cakephpproject/vendor/phpunit/phpunit-selenium
Now from the /cakephpproject/app/ I tried to run the following command
../vendor/bin/phpunit Test/Case/Controller/MyControllerTest
But I am receiving the following error message
Fatal error: Class 'App' not found...
If I use ./Console/cake test app Controller/GranulesController the test does attempt to run but throws the PHPUnit_Extensions_Selenium2TestCase not found error since on the system PHPUnit Selenium is not installed.
How do I make cakephp use the phpunit & the phpunit-selenium I installed using composer and not use the phpunit installed globally?
The following command seems to be executing the PHPUnit & PHPUnit-Selenium2 I installed using Composer.
./Console/cake test app AllControllers --bootstrap ../vendor/autoload.php
I encountered a similar issue in CakePHP 2.x (it was fixed in the latest CakePHP 2.8.5).
CakePHP expects to find PHPUnit in one of the following places:
vendors/phpunit/phpunit
vendors/PHPUnit
vendors/phpunit.phar
app/Vendor/phpunit
app/Vendor/PHPUnit
app/Vendor/phpunit.phar
[Composer's global directory]/vendor/phpunit
[Composer's global directory]/vendor/PHPUnit
[Composer's global directory]/vendor/phpunit.phar
But Composer (by default) creates a directory called vendor for its files (note the lack of a plural).
This issue was recently fixed for CakePHP 2.x, but you can get the same behaviour by setting the COMPOSER_VENDOR_DIR environment variable to vendors.

Categories