PHPUnit autocompletion on Windows - php

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.

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.

Composer - Is it possible to install packages in "require-dev" globally with one command?

quick question, and please let me know if anything about my idea is silly in general / if there is a better approach to this:
For the purpose of creating a Continuous Delivery pipeline, I would like to declare some packages needed for various types of tests in Composer with "require-dev". However, I am wondering if it was possible to run the install command in a way that it would install all packages listed under "require-dev" globally with one command?
It would be nice, since it would allow me to keep the test environments up-to-date with ease and allow for global access of all PHP Testing solutions I need. I know it is not much work and I would already be done with it if I just went through all of them manually, but I was curious to know if there was a nice way of doing this, since I think downloading and everything by hand, giving it execution rights and then moving it to the bin/ directory for global access is kind of a tedious solution.
According to the composer's help using the global keyword with the composer allow running commands in the global composer dir ($COMPOSER_HOME).
This means when you run composer global require phpunit/phpunit, composer will update its global directory instead of the directory you are in at that moment.
so in my case, $COMPOSER_HOME is in my home directory /Home/.composer, running the above command will;
update the /Home/.composer/composer.json file.
Download the latest phpunit package to the /Home/.composer/vender
Add a symlink to the phpunit executable file into the /Home/.composer/vendor/bin directory
so at this point if i have the bin folder included in my paths i would be able to run phpunit within my system regardless of which directory i'm running the command in.
Now if you prefer the dependency to be installed as part of the development requirement all you need to do is to add --dev parameter to the install (or update) command as well e.g. composer global require phpunit/phpunit --dev
What is that global install supposed to do?
The usual thing to do would be to install Phpunit. Now it wouldn't help you if you have two projects, one using the old version 3.7, and the newer using 4.6, to install any version globally.
Don't install dependencies globally that are used by a certain package explicitly.
However, tools that are not required, or are not stated to be required in a specific version, like PHP Codesniffer, can easily be installed centrally with
composer global require squizlabs/php_codesniffer
Then put the resulting path of ~/.composer/vendor/bin into the PATH environment variable. Note that ~ does not resolve there, you have to do that yourself.

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).

Using Composer PHP on Shared Space

I'm using Bluehost and do have access to SSH, thankfully. I've also set my PHP to 5.4 in the Control Panel settings. Now, here's the two big questions that I can't seem to grasp: how do I install Composer and furthermore, after installing Composer, how do I get the dependencies included?
In this case, I am attempting to use the official Tumblr PHP Library, which has dependencies -- all of which can be found on Packagist.
I've referred to this question in which the OP is using HostGator. I have attempted to install Composer in a similar fashion and have done so with seemingly successful results. The issue, however, is that I don't know where to install it on Bluehost so I now have Composer installed in several random places on the server simply because I don't know how to navigate to find where to put it in this shared space.
I know this is the issue (i.e. it's installed in the wrong place) because when I use the composer phar files and syntax to install the packages, I get errors saying the commands don't exist.
I really hate asking for hand-holding assistance, but if someone could walk me through the proper installation of Composer on a shared space and the proper installation of a Composer package from Packagist on the same shared space, it would be much appreciated. I've dug through the Composer documentation and can't seem to find a proper guide -- if one can even exist -- for this case. At this point, I'm wondering if it's going to be different for every web host.
Once you install composer as mentioned by KLVTZ. In the bluehost environment, use the php-cli instead of php.
php-cli composer.phar install
When installing composer, the biggest problem I ran into was that Bluehost's command line php was 5.2 and composer needs 5.3+.
Fortunately, Bluehost does give you access to 5.4. You can find it at
/ramdisk/php/54/bin/php54-cli
I ended up setting up an alias to call composer.phar using the above php command and it's worked great. You can see more details on how to install composer on bluehost.
You should probably not use Composer on the remote host, but instead install it on the local machine that is used to upload your project to Bluehost. There you can manage all the stuff that is needed to fetch the dependencies, which might include having the ZIP extension enabled or having a ZIP program available, having GIT or Mercurial installed if you have to grab a version from a branch, and so on.
All these dependencies of using Composer might not be available on that remote host, and frankly: You really do not want to install these development tools on a production machine anyway, I think. Any software that potentially helps an attacker shouldn't be present if it can be avoided.
The issue, however, is that I don't know where to install it on Bluehost...
In order to fix this problem, you need to figure out where exactly your project folder is. Composer needs to be installed in that folder. If you have access to a UNIX shell, I recommend that you change your directory to that project folder
cd $_SERVER['DOCUMENT_ROOT']/project_folder/
Generally, your remote server will follow the same file directory tree as that on your local machine. However, if you are unsure as to how your file structure may be organized, you can always print working directory of your local machine:
pwd
After you have successfully located the correct file path on your shared space, simply install composer:
curl -sS https://getcomposer.org/installer | php
This will report back something like:
All settings correct for using Composer
Downloading...
Composer successfully installed to: /var/www/my_project/composer.phar
Use it: php composer.phar
You now have access to your composer.phar in your project. Let me know if you need any further help.
One problem on Bluehost is that the php command on Bluehost does not execute php-cli. So I "bypass" that by having my own php script earlier in the PATH and then forwarding the arguments to php-cli. In the same process I made script named composer which calls php-cli composer.phar directly.
So, this setup has worked for me on Bluehost:
In ~/bin I have the composer.phar and two bash scripts called php and composer. In ~/.bash_profile I have prepended ~/bin to my PATH.
~/bin/php looks as follows:
#!/bin/bash
php-cli "$#"
~/bin/composerlooks as follows:
#!/bin/bash
php-cli ~/bin/composer.phar "$#"
And the additions to ~/.bash_profile are these:
PATH=$HOME/bin:$PATH
PATH=$PATH:$HOME/.composer/vendor/bin
export PATH
(I have also added ~/.composer/vendor/bin as can be seen, this is handy for global access for some composed applications (for me that is laravel)).
Remember to source ~/.bash_profile. (Or reconnect)
I think you should do something like this.
In console
echo $PATH
Use one of these pathes
mv composer.phar {selected path}/composer
chmod +x {selected path}/composer
Now you can simply call composer everywhere
If you have composer installed in your root directory (under public_html), might be something related to PHP version. As Bluehost when chosing PHP version "Be aware that this only modifies your ~/public_html/php.ini file. If you choose to use PHP 5.4, you'll want to make sure your crons use "/usr/php/54/usr/bin/php" instead, as otherwise it will use PHP 5.2."
So you can try with the command
/usr/php/54/usr/bin/php-cli composer.phar install

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