phpunit path in MAMP - php

I'm having a hell of a time with correctly installing phpunit with the zend framework in MAMP.
I created the zf alias and every time i create a new project i get "Testing Note: PHPUnit was not found in your include_path, therefore no testing actions will be created." in the terminal.
I know i have phpunit installed.Just typing in 'phpunit' in the terminal gives me the manual. I have it installed in "/Applications/MAMP/bin/php/php5.3.6/lib/php/PHPUnit".
my include_path in my php.ini file is
include_path = ".:/Applications/MAMP/bin/php/php5.3.6/lib/php:/Applications/MAMP/svn/zendframework/trunk/library"
if i type in 'which phpunit' into the terminal i get '/usr/local/bin/phpunit' which i think may be the problem.
Any ideas would be MUCH appreciated!

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.

vendor/bin/phpunit not working for Lumen

I'm new to Lumen (and Laravel). I've created a project with Composer and now I'm trying to get PHPUnit to work.
I'm following a book, where it should be possible to run a default passing test by typing vendor/bin/phpunit in the terminal, but it gives the error:
'vendor' is not recognized as an internal or external command,
operable program or batch file
I've checked that the phpunit file is actually there and that phpunit is added as dependency in my composer.json file. I've also tried ./vendor/bin/phpunit and vendor/bin/phpunit/phpunit, but with the same result.
I've searched Google to find a solution, but everyone else seem to have issues when running phpunit (wihout vendor/bin) and the solution is to use the full path vendor/bin/phpunit, but since I'm already doing that, it does not fix my problem.
I'm using PHPStorm on a Windows machine and running the PHP server via PHPStorm. I've not modified the default Lumen project.
Any help is greatly appreciated!
UPDATE:
Trying php vendor/bin/phpunit gives the following error:
You need to set up the project dependencies using the following
commands:
wget http://getcomposer.org/composer.phar
php composer.phar install
I'm not sure what that means, since I've already installed Composer. I used Composer to create the project and I haven't changed the dependencies from the default.
I had the same problem, for Windows it's vendor\bin\phpunit ;)
It turned out that some symlinks and permissions were not installed properly in the default project. I tried deleting the entire vendor/ directory and run composer install.
Now I can run phpunit with the command vendor\bin\phpunit (because I'm running on Windows - thanks Nizarii)
try this:
php vendor/bin/phpunit
Try putting php in front of the phpunit path like so:
php vendor/bin/phpunit

phpunit run problems in phpstorm, Cannot create phar

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

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

Executing path for PHPUnit

I have installed PHPunit using composer. The application has unit test cases. from my project folder when i do a "phpunit" it gives me an error saying phpunit not found..but if i do a "./vendor/bin/phpunit" it will work. Any idea how to fix this ?
You need to add PHPUnit to your path. Include the ./vendor/bin/phpunit into your PATH environment (to be searched when looking for programs) in your OS.
I had the same problem and got tired of fiddling with PATHs on all my machines, so I wrote a small tool that makes composer behave a bit more like PEAR did:
https://github.com/flack/poser
You can use it to install Composer packages globally by running:
poser require phpunit/phpunit
Afterwards, calling phpunit on the shell will work as expected.

Categories