PHPUnit - Version 3.7.21 instead of Version 6 installed - php

I followed the instructions on the official PHPUnit page to install PHPUnit 6.
composer require --dev phpunit/phpunit ^6.0
However, If I go to the project folder and execute phpunit --version then I get PHPUnit 3.7.21 by Sebastian Bergmann..
Why is PHPUnit 3.7.21 installed instead of PHPUnit 6?

I suppose you have xampp installed? It comes with PHPUnit 3.x.x preinstalled with PEAR which strangely can not be uninstalled with pear uninstall. And since its config is in php root folder, that 3-ish version has priority when running phpunit command (even if you have phpunit installed globally) in CMD or PS. How to fix:
Go to xampp/php folder and delete two files phpunit (with no extension) and phpunit.bat. Usually it's enough to prevent older PHPUnit version from being run but let's be on the safer side:
Go to xampp/php/PEAR dir and delete two folders PHPUnit and PHPUnit2.
Go to Control Panel, then navigate System and Security->System and click "Advanced system settings" link on the left. This will open Advanced tab in System Properties window. Click "Environment Variables" button, then under System Variables select "Path" variable and click "Edit..." button. In the new window, click "New" button and type your path to vendor/bin folder (if you already installed PHPUnit for your project there will be phpunit.bat file). By default for xamp it looks like this: C:\xampp\htdocs\yourprojectname\vendor\bin.
Restart your Command Prompt (or PowerShell) window, then type phpunit --version to see the that it's PHPUnit 6.x.x now.
P.S. (If you want latest PHPUnit do not use ^6.0 in require string, since it will install v6.0.0, write it in composer.lock file that will never update PHPunit to the latest e.g. 6.2.1 version atm). Just use
composer require --dev phpunit/phpunit
to install the latest stable version for your project.

You run your global PHPUnit version which is installed in another folder. To get the installed version you have to go to the vendor/bin folder.
vendor/bin/phpunit --version
PHPUnit 6.0.8 by Sebastian Bergmann and contributors.
In newer versions you can run it with bin/phpunit there should be the executable. When you need another PHP-Version then define it before php74 bin/phpunit.

If you use XAMPP, why don't you update XAMPP version of global PHPUnit to desired.
1.
Read this post, which quickly explains what to do:
StackOverflow post
2.
You can get phar file for version you need here:
proper PHPUnit phar file
3.
Which version of PHPUnit you need depends on your PHP version and you can identify it here:
which phar file version you need
Just click on links: PHPUnit 9 - PHPUnit 8 - PHPUnit 7 - PHPUnit 6 - PHPUnit 5 - PHPUnit 4 and read description explaining which PHP version it works with.

Related

Change path of phpunit command

I installed phpunit via composer but if I typed phpunit into cmd It would use old phpunit that was installed with PEAR. So I deleted all phpunit files from PEAR folder and It is still trying to use old phpunit path. How do I change the variable path so It uses the new phpunit installed via composer ?
phpunit is not properly install, there might be a version conflict.
Use like this: In windows, if you using wamp and blog being my root folder
c:/wamp/www/blog/>"vendor/bin/phpunit"

Using Laravel 5.4 in PhpStorm

Am trying to configure my PhpStorm to use Laravel 5.4 but don't know how to go about it. I have composer installed globally. And this are the steps I have already taken.
1.Go to Settings -> Languages & Frameworks -> PHP -> Composer
Set the composer.phar path to C:\ProgramData\ComposerSetup\bin\composer.phar
but am unable to locate the PATH to COMPOSER.JSON.
Who can give me the steps to configuring Laravel in PhpStorm.
Am using XAMPP
Reinstall composer and check that the PHP path is correct, if you run composer in the terminal you should see this do something, if not it is not installed correctly, similarly if you run php -v this will show the PHP version.
In your Laravel directory there should be a composer.json file, if not run composer init to create one

Change PHP version used by Composer on Windows

I have already use WAMP 2.5 with PHP 5.5.12, and with Composer.
The php is on:
C:\wamp\bin\php\php5.5.12
For new project, I need to use nginx and installed PHP 7.
The php is on:
C:\nginx\php
Now, using GitBash MINGW32, I tried to install laravel 5.3 using Composer create-project but it said
[InvalidArgumentException]
Could not find package laravel/laravel with version 5.3 in a version
installable using your PHP version 5.5.12.
I already put both C:\wamp\bin\php\php5.5.12 and C:\nginx\php on Windows System PATH variable.
How do I change the PHP version used by Composer?
Three ways to do this, really.
Create an alias in .bashrc to always run composer with the corresponding version
Something like alias ncomposer=`/path/to/php /path/to/composer.phar `
Specify the path to PHP version inside composer.phar itself
This is specified at the start of the file: #!/path/to/php php. Then composer should run with composer.phar
NB! The line will disappear upon self-update, so it's not a reliable solution.
Move up the path with the newest PHP version
If you place C:\nginx\php first, it should be used by default when using composer.
Hope this helps!
Although this question was solved, the answer didn't help me. I will explain how I managed to make composer to work in a version of PHP different from the one which is installed by default on my OS (PHP 7.1.1) as well as in my environment variables (these will not be changed !). Note that I'm using Xampp, but the principle remains the same for Wamp.
Starting from this answer :
Start up Git Bash
Type cd ~/ to go to your home folder
Type touch .bash_profile to create your new file.
Edit .bash_profile with your favorite editor
In my case I have a folder named php733 inside xampp folder which corresponds to PHP 7.3.3. This is this other answer that helped me in creating the alias :
alias composer733='/c/[xampp folder]/php733/php.exe /c/ProgramData/ComposerSetup/bin/composer.phar '
Then, type . .bash_profile to reload .bash_profile and update any functions you add. Notice the space between the two dots !
Finally, type this command in Git Bash :
composer733 [whatever you wan]
Example : in the project that requires at least PHP 7.1.3
Using composer :
$ composer update
This package requires php ^7.1.3 but your HHVM version does not satisfy that requirement.
Using composer733 (the alias I created) :
$ composer733 update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 98 installs, 0 updates, 0 removals
- Installing [...] (v1.11.0): Loading from cache
It works, without having to change the environment variables

Update to a specific version of PHPUnit

Currently my system installed phpunit is on 4.3.1
Doing phpunit --self-update will update to the latest version of phpunit which is 5.3.* at the moment.
I like to update it to a specific version (4.8.24)
How am I suppose to do this?
Note: I know about the composer option, but I would like to do it on my system installed phpunit.
I ended up changing the phpunit.phar file on my /usr/local/share/phpunit/ folder
This dev environment is set up via this http://vccw.cc/ and it installs phpunit 4.3.1, I need 4.8.24 coz I'm doing wordpress plugin unit testing http://wp-cli.org/docs/plugin-unit-tests/ it says there 4.8.* is required.
So here is what I did.
Download latest 4.8.* version of phpunit here https://phpunit.de/
Rename the phar file to phpunit.phar ( I remove the version section )
Execute this chmod +x phpunit.phar to make the phar file executable
Copy the phpunit.phar to /usr/local/share/phpunit/ folder
Note: I remove the old phpunit.phar ( that is the 4.3.1 ) and replace with this new one
Also their is already a symbolic link on /usr/local/bin/phpunit for /usr/local/share/phpunit/phpunit.phar setup by vccw.
I think changing phpunit version can be done too via modifying the provisioning files provided by vccw but I'm not that in to provisioning scripts, only basic knowledge.
I hope this helps other devs, specially windows devs like me working on unix based dev env.
PS. This dev env is only used by me, it isn't version controlled so its ok for me if I make changes to the dev env directly ( not via provisioning scripts provided by vccw )

PHPUnit and Ubuntu - Issues with include path after Installation with PEAR

I need to install PHPUnit for a web dev project on my Ubuntu box, but am having serious issues. Using suggestions from various sources, I've installed via pear and tried the following:
Currently, phpunit --version returns
PHPUnit 3.7.20 by Sebastian Bergman
when in /usr/share but throws errors everywhere else.
My include path is
include_path = ".:/usr/share/php:/usr/share/php/PEAR:/usr/share/php/File/Iterator"
I've tried different variations ranging from simple /usr/share/php to the full string above. PEAR is in all caps since that is the folder it is in.
Executing with a test case results in
PHP Warning: require_once(File/Iterator/Autoload.php): failed to open stream:
No such file or directory in /usr/share/php/PHPUnit/Autoload.php on line 64
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. require() /usr/bin/phpunit:43
PHP Fatal error: require_once(): Failed opening required
'File/Iterator/Autoload.php'(include_path='/usr/share/php/libzend-framework-php')
in /usr/share/php/PHPUnit/Autoload.php on line 64
pear list -c phpunit returns
Installed packages, channel pear.phpunit.de:
============================================
Package Version State
File_Iterator 1.3.3 stable
PHPUnit 3.7.20 stable
PHPUnit_MockObject 1.2.3 stable
PHP_CodeCoverage 1.2.10 stable
PHP_Invoker 1.1.2 stable
PHP_Timer 1.0.3 stable
PHP_TokenStream 1.1.5 stable
Text_Template 1.1.2 stable
I've confirmed that Autoload.php exist at both php/PHPUnit/ and php/File/Iterator/
My loaded php.ini file is at `/etc/php5/cli.php.ini1 as verified by
php --info | grep "onfiguration"
Any other suggestions would be appreciated.
Search for a file on your computer that is called Autoload.php and is in a file structure like this:
../File/Iterator/Autoload.php
Now open the file that throws the error (/usr/share/php/PHPUnit/Autoload.php in your case) and include a set_include_path statement that points to the absolute path of that file.
You seem to have a very similar problem to what I experienced on Mac OSX. For more information look at this very similar question. If all of this doesn't help, try to install with Composer, which I have heard doesn't have this issue.
For recent projects, I'm using Composer to install PHPUnit and the other related tools. It's quite easy:
## ... rest of the composer.json file
# "php -f composer.phar update --dev" to install
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
I also then have a shell script to actually run the PHPUnit command-line tool from the ./vendor/bin/ directory where a link to the locally installed script is installed.
Like Alister's answer, relying on Composer,
but I prefer to deploy the updated PHPUnit globally across my projects with:
composer.phar global require "phpunit/phpunit=dev-master"
That way, when I am at the root dir of my Symfony project, I can just run
~/.composer/vendor/bin/phpunit -c app/ -v --filter testFireNotifications |tee phpunit.log
That last bit with --filter was to run something selectively (actually, more for fast web automation via crontab, than testing...)
Even on a current Ubuntu 14.04.2 server, the repo is stuck with an outdated PHPUnit 3.7.28 "stable", so I sidestep the OS-provided one with this custom install that won't conflict (I haven't put ~/.composer/vendor/bin in my PATH).

Categories