I had problems with configuring PhpStorm IDE to use http://symfony.com/doc/current/components/phpunit_bridge.html while working with Symfony 3.3.
I decided to just download phpunit.phar to bin and use it instead.
Symfony 3.4 (and Symfony 4), does not even have phpunit.xml.dist out of the box, so there is a problem with using phpunit.phar easily.
I've installed PHPUnit using flex:
composer req phpunit
That created phpunit.xml.dist and I was able to run tests from command line by:
php bin/phpunit
But again I could not make PhpStorm use it.
So I downloaded phpunit.phar and it can work together with provided phpunit.xml.dist.
Question 1: Is there any way for PhpStorm IDE to use phpunit-bridge?
Question 2: What is the best practice for Symfony 4 (phpunit-bridge or vanilla phpunit.phar)?
What I usually do is point my phpunit testing framework on PHPStorm to the secret .phpunit directory which was created by the bridge, like:
The location of the "phar" file is:
bin/.phpunit/phpunit-(major).(minor)/phpunit
or in some cases:
vendor/bin/.phpunit/phpunit-(major).(minor)/phpunit
After this, the specified phpunit executable will be called correctly when exeuting unit-tests, but with a --no-configuration option. This can cause autoloading problems (a lot of "class not found" errors), because the autoloader generated by Composer is not specified anywhere.
To fix this, you should have a phpunit.xml file in your project (this is common practice anyway), in which you specify Composer's autoloader, something like this:
<phpunit bootstrap="vendor/autoload.php">
This phpunit.xml should then be specified in the "Default configuration file" option and you should be good to go.
Regarding phpstorm using phpunit-bridge:
It's possible as a custom script, but you won't have the nice interface and the possibility to run (and debug) specific tests via PHPStorm interface.
I manage to run symfony/phpunit-bridge with success using this configuration:
PhpStorm 2018.2.5
Symfony 4.1.x
PHP 7.1 running on docker
"symfony/test-pack": "^1.0"
Steps:
after composer require --dev symfony/test-pack i have in dir /vendor/bin file simple-phpunit which should run symfony/phpunit-bridge just fine.
Then in PhpStorm in File | Settings | Languages & Frameworks | PHP | Test Frameworks set:
radio select to Path to phpunit.phar option
Path to phpunit.phar to absolute path of simple-phpunit file (e.g /application/vendor/bin/simple-phpunit)
check Default configuration file: and set input value to absolute localization of your phpunit.xml.dist (in my case /application/phpunit.xml.dist)
note: phpunit.xml.dist file should be configured to use symfony/phpunit-bridge - check https://symfony.com/doc/current/components/phpunit_bridge.html
Click Appply/Ok and now you can run tests from PhpStorm interface
Aside given answer, it's worth mentioning that the secret .phpunit directory won't appear out of thin air.
After composer req phpunit, one has to run the phpunit script first, eg.:
bin/phpunit
which will download a local copy of the PHPUnit and place it in the same folder, so the path to the phar executable will be:
// path may differ, at the time being is:
bin/.phpunit/phpunit-6.5-0/phpunit
The accepted answer didn't work for me using PHPStorm 2020.1 with Symfony 5.2
I found that it works if I apply a blank value for 'Path to phpunit.phar', despite the fact that the preferences dialog complains that '!Path to phpunit.phar is empty'.
So in Preferences > Languages & Frameworks > PHP > Test Frameworks:
It is very simple and doesn't matter version of Symfony, because of the concept of testing still stable. At first, you need to configure test framework on the PHPstorm preferences (screenshot), in your case, you can to use Vendored PHPUnit or downloaded manually, you can join in the PHPUnit library section. Then you need to add Run/Debug configuration.
"phpunit/phpunit" and "symfony/phpunit-bridge" as I see was installed, this is all.
Related
I came from frontend stack so I can misanderstand something when I'm learning symfony3.
I downloaded Symfony according to documentation with:
php -r "readfile('https://symfony.com/installer');" > symfony
To make php command available i added some values to the enviroment PATH variable (according to this tutorial: https://seiler.it/installing-symfony-framework-into-xampp-for-windows/ - step 2.3) like this:
c:\xampp\mysql\bin;c:\xampp\php;c:\xampp\php\PEAR;C:\xampp\php\symfony
As you see I'm using xampp for PHP development. I added that last, because i copied that downloaded symfony file to c:\xampp\php\symfony directory.
OK, now if i want to use symfony command, like
symfony new project
I need to go to directory where there is that symfony file and add 'php' before, like
php symfony new project
Because simple 'symfony new project' doesn't work (error msg tells about that command isn't known by Windows). How can i fix it?
In frontend there is something like npm (node package manager) with which you can install library/tool locally in directory or globally (to run it in terminal from any place on the computer). Is there is any equivalent in PHP development? I tried to install this by pear (from mentioned tutorial in step 7), but it installed me symfony 1.x version (and error of running that command was that there is no task like 'new') and i want to learn 3, not 1.
I tried to get composer, but i don't see where can i install there symfony globally.
So, how to run symfony globally? What am I missing?
From this doc:
In Windows, move the symfony file to a directory that's included in the PATH environment variable to create the global command or move it to any other directory convenient for you
idea
Install and use Git Bash. Gives you more futures then the windows CLI.
Performance
To run a ten times more fast environment for Symfony i prefer a Linux OS.
NetBeans does not seem to locate the PHPUnit sources, therefore I can not use autocompletion on for instance PHPUnit_Framework_TestCase.
I run PHPUnit from within NetBeans using a cmd, on Windows using EasyPHP, generated with:
echo #php "%~dp0phpunit.phar" %* > phpunit.cmd
Note that this is the recommended way, older variants are not longer supported as I noticed.
Where are the PHPUnit sources located and how can I tell NetBeans to account for support?
I came up with a workaround:
Install the sources of PHPUnit globally using Composer:
composer global require "phpunit/phpunit=4.7.7"
And finally add the following path to the include directory of the project:
%APPDATA%\Composer\vendor\phpunit\phpunit\src
This will do the trick. However make sure the installation of PHPUnit and the sources are of the same version to prevent complications.
Note that the sources can be installed using a lot of different ways, but keep in mind to use the right include directory.
I am running phpStorm 7.1.3 version on Windows 7 and I have done everything accordingly to install composer and then phpunit. Now I have it in vendors directory. But every time I run my program which is a simple Yii application I get the same error.
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with
message 'Cannot create phar
'C:/wamp/www/myproject/vendor/bin/phpunit', file extension (or
combination) not recognised or the directory does not exist' in
C:\Users\myUser\AppData\Local\Temp\ide-phpunit.php:177
Please help, I am stuck here already two days and cant get this working, almost changed and reinstalled everything but is not helping.
I didn't use pear, I downloaded everything through the phpStorm. I downloaded composer, and after that through phpStorm composer I downloaded phpUnit 3.7.37.
Here is the screenshot of my settings for composer
Here is the content of my composer.json
{
"require": {
"phpunit/phpunit": "3.7.37"
}
}
Here is the screenshot of my settings for PHPunit
On your 2nd screenshot (PHPUnit settings):
Why did you choose PHAR option if you are using COMPOSER?
Why did you point to .../bin/phpunit (even if you choose 3rd option .. you should point it to a PHAR file then)?
Therefore:
For composer installations you should use 2nd ("Use custom loader") option.
The edit box -- you should point it to your autoloader script (yes -- composer autoloader script).
Related (similar issue): https://stackoverflow.com/a/17316104/783119
If you are not using composer, but the 'Path to phpunit.phar' option and still receive this error, just make sure the path you specify ends with a .phar (usually ends with phpunit.phar) I was seeing this same error but because I had the path pointing to my command-line alias, not the actual .phar file, it gets hung up on the improper extension and bails.
I was trying to run phpUnit on zend framework 1.12.9 and had the same problem - solved it by changing path to phpunit.phar to use custom loader.
Then You have to point it to the generated autoload.php by composer.
In my case that was
C:\wamp\www\phpu3\vendor\autoload.php
Later, the important thing to do was to point the bootstrap file from tests.
When you download the phpunit.phar file using the wget command, following the instruction on the getting started manual -
https://phpunit.de/getting-started/phpunit-4.html
the "phar" extension is being removed, and PHPSTORM has issues to interrupt this file. I've changed the extension:
mv phpunit phpunit.phar
make sure you update the path to phpunit.phar file in the IDE setting accordingly
I know it seems like a duplicate, but my problem is different:
Since I installed PHP from source, some folders are not there. I installed PHPUnit the usual way using pear.
Normally I would add /usr/share/php to the include path of the IDE or project and be done. But that folder doesn't exist.
$ updatedb
$ locate phpunit
/usr/local/bin/phpunit
/usr/local/lib/php/.channels/pear.phpunit.de.reg
/usr/local/lib/php/.channels/.alias/phpunit.txt
/usr/local/lib/php/.registry/.channel.pear.phpunit.de
/usr/local/lib/php/.registry/.channel.pear.phpunit.de/phpunit.reg
$ locate PHPUnit
/usr/local/lib/php/doc/PHPUnit
/usr/local/lib/php/doc/PHPUnit/LICENSE
/usr/local/lib/php/doc/PHPUnit/README.md
I tried including /usr/local/lib/php, but I still get "undefined class PHPUnit_Framework_TestCase"
Note that php and phpunit work fine, I only need help with IDE autocompletion.
What can I do?
As of PHPUnit v4 PEAR distribution now contains PHAR version instead of lots of individual files. And there is one file (phpunit) instead of launcher + php files (on installation phpunit.phar gets renamed into phpunit).
Because it now has no .phar extension, IDE will not be able to recognize it accordingly.
The easiest option is to download actual PHAR version of PHPUnit and place in anywhere in your project (e.g. inside vendors folder).
Cannot find PHPUnit in include path is the error message I get when I try to run my code in phpstorm.
In the PHP Settings, my PHP level language is 5.4 (traits, short array syntax, etc.) and interpreter is Name(5.4.7) where Name is user defined.
The error appears when I try to run the code and the exact message is this:
C:\xampp2\php\php.exe C:\Users\DELL\AppData\Local\Temp\ide-phpunit.php --no-configuration
C:\xampp2\htdocs\ft-website
Testing started at 2:34 PM ...
Process finished with exit code 1
Cannot find PHPUnit in include path (.;C:\xampp2\php\PEAR)`
Just encountered this problem myself.
I'm unsure why it is unable to find PHPUnit in the include path (despite the fact that it is there, albeit in all lowercase).
I got around this by changing my IntelliJ Preferences for PHPUnit (under PHP --> PHPUnit). I changed the PHPUnit library settings to Use custom loader and then specified the path to the phpunit executable. On my mac, that was /usr/local/Cellar/php54/5.4.26/bin/phpunit.
UPDATE:
I just discovered that pear now installs PHPUnit as a phar named phpunit. It previously installed the PHPUnit source, which was really nice for reference and code completion in PHPStorm. I think that this is the reason things aren't working any longer with PHPStorm, because it's expecting a php executable and not a php archive.
So, I'm moving away from using pear to install PHPUnit, and I'm using composer instead. This bundles PHPUnit directly as a dependency of my project, which makes it more portable than a system dependency. You'll need to add vendor/phpunit/phpunit as a PHP include path in your PHPStorm preferences. This will serve two purposes:
PHPStorm can find the phpunit executable now
PHPStorm will index all the PHPUnit classes now, so you'll get auto-complete. Yay!
To do this, go to Languages and Frameworks in the PhpStorm settings.
If you click on PHP, on the right you have your include paths
I had this problem after adding PHPUnit via composer.
I fixed this by choosing use custom autoloader in Settings -> Languages and Frameworks -> PHP -> PHPUnit, then adding /vendor/autoload.php as the location of the custom autoloader.
In addition to Ben's answer, in phpStorm 7.1.3 it works by specifying the phpunit.phar location under the "use custom loader" option, for example:
"Path to script: /usr/share/php/phpunit.phar"
This is the way to do it without using composer, and using your global phpunit.
Phpunit now comes with a phar file. My path was /usr/local/Cellar/phpunit/5.0.0/libexec/phpunit-5.0.0.phar
PhpStorm Preferences -> Path to phpunit.phar -> select the phpunit phar
That's all, good stuff.
I had this same problem for PHPStorm 2017 using Vagrant. 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.
For future readers, to fix the same issue when using PHPStorm + Vagrant + PHPUnit by defining path to the global phpunit.phar (not composer)
While the tests runned successfully, I was missing the code completion. I had confirmed that phpunit was in the PATH on my local machine, and to make sure, I had added /usr/local/bin to the PHP Include paths in PHP Settings. Still no code completion.
The reason: PHPstorm was looking for phpunit.phar, not just phpunit. To fix, on my local machine I established a symlink phpunit.phar pointing to phpunit:
$ cd /usr/local/bin/
$ sudo ln -s phpunit phpunit.phar
Then I reindexed the project, and code completion started working.
For those that are looking at it in 2020, starting from PHPStorm 2019 this is under:
Languages and Frameworks->PHP->Test Frameworks
Choose to add a new library
Select "PHPUnit"
Select "Path to phpunit.phar" radio
You'll have an automatic option to download PHPUnit