phpunit run problems in phpstorm, Cannot create phar - php

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

Related

How to configure PhpStorm to use symfony/phpunit-bridge

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.

PHP Composer does not clone symfony/assetic-bundle properly

I am trying to install Symfony on CentOS Linux wih PHP 5.6 and cPanel installed.
When I run composer require symfony/assetic-bundle , once adding bundle to the AppKernelphp, symfony (app/console too) stops working and keeps logging this error:
[10-Jun-2016 22:00:57 UTC] PHP Fatal error: Class 'Symfony\Bundle\AsseticBundle\AsseticBundle' not found in /home/avid24/public_html/app/AppKernel.php on line 19
After checking the vendor directory, turns out that composer has downloaded a single compressed file with a random name, I could extract it with unzip. but problem still exists!
This environment works properly on windows and I could easily update symfony and its components using composer.
Any help? Anyone with the same experience ?
Well, I eventually figured out the problem!
I enabled suhosin extension which prevents the composer and other similar command prompt php scripts from a proper execution.
As a temporary solution I copied php.ini and disabled unnecessary extensions and put it next to my project and call composer this way
$php -c ../composer-php.ini ~/composer ......
I'll create a script to make the life easier
I didn't find any resource which describes coexistence of composer and suhosin extension, so I'm not sure if this is the ultimate solution but the root cause is identified.
If anybody makes workaround this subject I'd be more than happy to know about it.

Trying to open a PHP application locally

I'm trying to open a CakePHP application locally that had a composer.phar file but did not have composer.json file.
I created a default composer.json file, went into the app & console folders and tried to use bin/cake server to open the application but it shows bash: bin/cake: no such file or directory.
I am pretty sure there is one file that I need to open that will open the whole program, is there a certain file I should be looking for? Am I missing a step?
Without a composer.json file you cannot run composer.phar install to install all the dependencies.
So you have to proceed with a "try and fail" approach.
First of all, create the default composer.json file required to run a standard installation of CakePHP and install with composer.phar install.
Then check the App class and look for third party packages: add them to the composer.json file and update.
Then start using the application and read the exceptions: there you will find info about other required dependencies. Add them to the composer.json file and then update.
This may take some time to be done but in the end you will have again a working application.
Except if private packages are required: in this case you will have hard times.
Hope this can help you! In bocca al lupo!

How can I get IDE autocomplete for PHPUnit if I installed from source?

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 phpstorm

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

Categories