I created a phpunit test file , when I try to run it via phpstorm, I get the message:
Unable to attach test reporter to test framework or test framework quit unexpectedly
seems the following command is executed:
/usr/local/bin/php /private/var/folders/4b/qrnw7nbd6llgmhrss5rf1_880000gt/T/ide-phpunit.php --configuration /Users/Shared/sites/pac/app/app/phpunit.xml.dist BackendControllerTest /Users/Shared/sites/pac/app/modules/Pac/Backend/Tests/Controller/BackendControllerTest.php
Testing started at 23:22 ...
Process finished with exit code 0
when i execute this via command line , i get much more output
PHPUnit 3.6.11 by Sebastian Bergmann.
...etc...
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
seems phpunit isn't executed in phpstorm? shouldn't there be some sort of error message instead of finishing with exit code 0?
Paths to php & phpunit (same) in phpstorms configuration should be ok (both installed via homebrew in /usr/local/bin, path added to phpstorm)
osx 10.7.4
php 5.3.14
PHPUnit 3.6.11
Thanks for helping me!
Matthias
On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.
Note that this solution will not work on Mountain Lion (10.8).
Do you edit include_path in your php.ini?
I have the simillar situation to you.After I added pear's include path,it works.
Look at this.
On MacOSX, I was struggling with a simialair problem for quite a while.
The reason in my case turned out to be a PHP-upgrade script, that had messed with some permissions.
What I did:
Made sure I had the right to read and execute with my own account in pear bin_dir and php_dir
Make sure the php_dir is in the include_path
To find these directories you can issue
# pear config-show
(Look for the bin_dir and php_dir)
This meant I would do the commands:
# (sudo) chmod -R 755 <bin_dir>
# (sudo) chmod -R 755 <php_dir>
More coding - Less IDE-config!
Related
I'm in a multi developer program. The other developer installed PHPUnit via composer and successfully is running tests. I took the composer files, ran composer update, but when I try to run the tests, I am receiving this error:
C:\Repos\Project\TDD>php vendor/bin/phpunit
Output:
dir=$(cd "${0%[/\\]*}" > /dev/null; cd "../phpunit/phpunit" && pwd)
if [ -d /proc/cygdrive ]; then
case $(which php) in
$(readlink -n /proc/cygdrive)/*)
# We are in Cygwin using Windows php, so the path must be translated
dir=$(cygpath -m "$dir");
;;
esac
fi
"${dir}/phpunit" "$#"
This is the content of the phpunit shell script. The shebang first line #!/usr/bin/env sh is not being output, but this is the rest of the file. The problem appears to be that the PHP CLI is not executing the script, just reading it. I can't figure out how to fix that.
I am on a windows environment, and I am not running apache locally.
Things I have tried:
I have tried using full paths (eg, C:/php/php.exe) for both the php and the shell script, and that didn't matter.
I have tried reversing the slashes vendor\bin\phpunit
I can run php -v successfully, but php -f vendor/bin/phpunit still just outputs the file contents.
I have confirmed the PATH for php is accurate and restarted.
All expected vendor files do appear to be present.
Composer update is not throwing errors of any kind.
I've changed my terminal / CLI processor to use powershell instead of the default cmd.exe (same result as above)
I've confirmed encoding of the phpunit file is UTF-8 without BOM
I've tried making the php command execute the phpunit script in powershell and git bash directly with no luck --- it outputs the shell file. It seems to be that php is just not executing it, which makes me think there is a problem with my php.ini or something.
I CAN run the phpunit shell directly, such as phpunit --version, but I can't get it to run my tests.
I set the composer platform to match my cli php -v
"config": {
"platform": {
"php": "7.4.20"
}
}
Edit: Interestingly, I uploaded it all to my LAMP webserver and ran the same script there, and it is not executing there either. It's outputting the files...
There is a lot of stuff you're fiddling with while trouble-shooting your issue, so there can be only giving extended comments on it. Take it with a grain of salt and pick the pieces that make sense to you, it looks to me you're clever and should be close to the point where you connect the dots more soon than later (don't give up):
C:\Repos\Project\TDD>php vendor/bin/phpunit
This on your windows shell prompt command the php binary to execute the (php) script vendor/bin/phpunit.
And PHP does exactly this (everything else would be a surprise, right?), it executes that script.
The output you see confirms it. As php is the PHP CLI SAPI (PHP Command Line Binary), the first line starting with # is not output (you can find this behaviour described in the PHP documentation as well: https://www.php.net/manual/en/features.commandline.usage.php and on antoher place that I can't find right now that exactly describes this difference to the common mode https://www.php.net/manual/en/language.basic-syntax.phpmode.php).
You then correctly analyze:
This is the content of the phpunit shell script.
and make the note on the shebang line:
#!/usr/bin/env sh
So this looks to me that this shell script vendor/bin/phpunit is not to be executed with the given commandline:
php vendor/bin/phpunit
but instead just by:
C:\Repos\Project\TDD>vendor/bin/phpunit
Which won't (or might not) work on your windows system.
Some more background information (this applies to phpunit bin-stubs as well as other when installed as composer(1) dependencies):
composer manages these scripts (for you)
composer handles them for POSIX compatible systems as well as for others (compare the /usr/bin/env sh which is stupid, it's /bin/sh, always, may need a report on the composer project) incl. windows or windows with cygwin or WSL running on Windows, Vagrant box setups etc. pp. (yes they really care since years).
composer handles this at the time of install/update.
for windows (which I can't compare are not using it but from what I remember) composer is adding .bat or .cmd files next to the commands (as that is how Windows handles executables).
So mabye using:
C:\Repos\Project\TDD>composer exec phpunit
already solves your invocation problem.
Or
C:\Repos\Project\TDD>vendor\bin\phpunit.bat
Or
C:\Repos\Project\TDD>vendor\bin\phpunit.cmd
Or
C:\Repos\Project\TDD>vendor\bin\phpunit
does. Point in case is this: https://getcomposer.org/doc/articles/vendor-binaries.md
All in all this looks to me you got instructions from one developer working on a unixoide sytem (Mac, Linux) and you're running on Windows. This should not pose a problem on that level, however the onboarding in your team might be low or it's just the knowledge management (unfortunately after a decade or more of Stackoverflow this got worse) and you've been left alone with the trouble shooting.
It's not a programming problem, but merely getting the tooling to run.
Perhaps there is some regime about composer and running it which prevents you from looking there first. But this is hard to say on Stackoverflow.
I'd start fresh with the project, remove it from disk and install it anew. This must work, always. To not sink the current project you can do this a-new in a second directory (the project might support composer create-project to give it a quick start - that is cloning new and doing the composer install).
Similar it is that you could do a composer update in the existing project (which is intended to modify it and depending on how well the project you work with is integrated with composer and your development platform will enable or break things).
At the end of the day the common workflow is:
> rmdir vendor
(remove the vendor directory, that is the blank slate)
> composer install
(install the vendor directory, that is all PHP dependencies of the project)
> composer exec phpunit
(execute the phpunit test-runner)
Edit: Interestingly, I uploaded it all to my LAMP webserver and ran the same script there, and it is not executing there either. It's outputting the files...
Never install phpunit on a webserver system. Run it in development or CI, but not on the webserver. Period. (this has security implications, and it won't help you to execute it there, you need to run it where you develop when you're doing TDD - keep this focus for troubleshooting)
The true path to the PHP script that is represented by vendor/bin/phpunit is:
vendor/sebastianbergmann/phpunit/phpunit
(you can find it in the composer.json of phpunit https://github.com/sebastianbergmann/phpunit/blob/master/composer.json#L66)
Given your original command-line and from the various other information you give, this should work:
php vendor/sebastianbergmann/phpunit/phpunit
What I want
Run PHPUnit by terminal and configuration in PhpStorm
What I Have
Previous problem
Resolved in HERE - about not finding files
File that running
#!/usr/bin/env bash
# echo "Current working directory: '"$(pwd)"'"
cd $(pwd) && docker run --rm -t -v $(pwd):/var/www -e SYMFONY_ENV=dev ezsystems/php:7.1-v1-dev php $#
Summary
When I run this by terminal:
docker-phpez vendor/bin/phpunit --coverage-text
Everything working correctly.
When I try to run this with configuration of PhpStorm, I get warning that: PHP is not installed.
But running this as remote PHP interpreter, gives me:
docker://ezsystems/php:7.1-v1/php /opt/.phpstorm_helpers/phpunit.php --configuration /var/www/phpunit.xml.dist
Testing started at 17:06 ...
The value $_SERVER['IDE_PHPUNIT_PHPUNIT_PHAR'] is specified, but file doesn't exist '/var/www/vendor/bin/phpunit'
Process finished with exit code 1
Looking like mounting doesn't work.
Question:
do you know why? How to fix that?
In previous version of PhpStorm I didn't had any problem with that.
Right now, I have 2016.3.2.
Because after update you have to update tags for phpstorm_helpers docker image. There i put more how it should be fixed: https://youtrack.jetbrains.com/issue/IDEA-189164
After updating PHPStorm to 2017.1.4, what I did:
switched to docker configuration
phpunit loaded from composer's autoload.php
Started to working. Still don't know why docker-phpez not working by PHPStorm, but I will stop for now.
I wanna test my php codes and I have decided to user PHPUnit for my test.
I have followed steps with official documentation
$ wget https://phar.phpunit.de/phpunit.phar
$ chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.
But I am using MAMP with my MacOS X.
So I am not sure how to implement PHAR files in MAMP.
Normally, the documentation tells to use this comment in terminal:
sudo mv phpunit.phar /usr/local/bin/phpunit
And my PHP location is:
/Applications/MAMP/bin/php/php5.2.17/bin/
I've tried to run this comment:
sudo mv phpunit.phar /Applications/MAMP/bin/php/php5.2.17/bin/
I don't know what should I do at this step. Please take a look because it does not work.
Here are the steps I used to successfully get PHPUnit working in MAMP. These instructions
are pieced together from various places. I hope that having it all in one place helps
someone else. Happy testing!
Use MAMP's PHP in the Terminal
Adapted from How to override the path of PHP to use the MAMP path?
Edit or create ~/.bash_profile with the lines below
# Use MAMP's latest version of PHP
MAMP_LATEST_PHP=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${MAMP_LATEST_PHP}/bin:$PATH
Place these lines after any other lines exporting $PATH - this assures that your MAMP PHP is found first in the path. Note that these lines try to find the highest numbered version of PHP in your MAMP installation. Feel free to adjust this to a specific one that you have, if desired.
You can tell you did it right when you get a MAMP path from which php in your terminal. You should get something like this:
/Applications/MAMP/bin/php/php7.0.0/bin/php
Install PHPUnit
Mostly, this is downloading the PHP archive (PHAR) from the PHPUnit website. There are ways to do this from the command line that I couldn't get to work. So, I used a web browser.
Download the most recent PHPUnit PHAR from https://phar.phpunit.de
Move it to /usr/local/bin
cd /usr/local/bin
Make it executable with chmod +x phpunit-5.3.2.phar (adjust according to actual name)
Make a symbolic link with ln -s phpunit-5.3.2.phar ./phpunit (adjust according to actual name)
Check the version with phpunit -—version
You should get something like this:
PHPUnit 5.3.2 by Sebastian Bergmann and contributors.
Building a symbolic link in step 5 permits you to use phpunit instead of having to type
phpunit-5.3.2.phar instead. It also allows you to update PHPUnit without having to
change what you type, assuming of course that you create a new symbolic link when you
update.
Write a Test
This isn't an exhaustive section. There are far better tutorials on writing tests.
Instead, this is merely some notes from my experience on rules that tripped me up,
though I'm sure everyone else knows them:
Your test class name must end with Test:
class SomeTest extends PHPUnit_Framework_TestCase
Your test class file name must end with Test.php and match the contained class:
SomeTest.php
Method names in your test class that are to be run as tests must start with test:
public function testSomething()
Run a Test
By this time, it should be as easy as:
phpunit SomeTest
If everything goes well, PHPUnit will run your test and give you the results.
Add a Handy Alias
Assuming that it all works (Yay!) try this alias in your ~/.bash_profile
# Use colors when running phpunit
alias phpunit='phpunit --colors'
/usr/local/bin/ is just recommended as a convention, because it's always in the $PATH on Unix systems, you can use any other location that's in your path, or leave it anywhere else and specify the absolute path when using it.
With what you have, you should be able to run:
/Applications/MAMP/bin/php/php5.2.17/bin/phpunit.phar --version
Note the .phar suffix, because you did not rename the file while moving, as you'd have with sudo mv phpunit.phar /usr/local/bin/phpunit
I tried. You can use this command. It worked for me.
sudo mv phpunit.phar /Applications/MAMP/bin/php/php5.2.17/bin/phpunit
I am reading the book "Learn ZF2: Learning By Example" by Slavey Karadzhov. Now I am at pages 103-107. I am trying to run PHPUnit test as described, but nothing happens... Where is the problem and how to fix it?
To go into the same situation you would have to:
git clone https://github.com/slaff/learnzf2.git .
composer.phar self-update
composer.phar install
git stash
git checkout 'ch-unittest'
This should bring you the same files as I have (=as in the book).
Now, I try to run this command:
ZF2_PATH=`pwd`/vendor/zendframework/zendframework/library \php vendor/bin/phpunit -c module/User/tests/
And the command does not work as expected...
I would expect some test result output or so, but all I get is the output witch is similar to the content I have in vendor/bin/phpunit file. It is:
#!/usr/bin/env sh
SRC_DIR="`pwd`"
cd "`dirname "$0"`"
cd "../phpunit/phpunit/composer/bin"
BIN_TARGET="`pwd`/phpunit"
cd "$SRC_DIR"
"$BIN_TARGET" "$#"
To be more precise, the first line (#!/usr/bin/env sh) does not show up in the output. The rest do show up.
**
I am running the code on Windows 8.1 Pro OS, using Git Bash (git version 1.9.4.msysgit.1). php keyword seems to work well as writing php -v works as expected (outputs PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58) ...)
My own guess would be that Git bash cannot resolve the code it finds in vendor/bin/phpunit so it fails there... But this is the guess only...
EDIT #1:
Just tried on shared hosting with LAMP set. Everything went fine. It seems that Zend Framework 2 and Windows are not so friendly at some points (e.g. at the just mentioned unit testing case). Looks like the very good book (no sarcasm, it is really the good one) would be better if it would have one more chapter on something like VirtualBox + Vagrant + PuPHPet (or something close to that).
The line of composer.json file used in the book: phpunit/phpunit" : "3.7.*
I have changed phpunit/phpunit" : "3.7.* to phpunit/phpunit" : "4.5.* as 4.5 version of PHPUnit is the current stable.
Then, of course: php composer.phar update
Then this one will work nicely:
ZF2_PATH=`pwd`/vendor/zendframework/zendframework/library \php vendor/phpunit/phpunit/phpunit -c module/User/tests/
The command above is not the same as in the book.
Tested on Windows 8.1, Ubuntu Trusty 14.04 LTS x64, the isolated development environment provide here. Works well.
There is also a way to make PHPUnit 3.7.* work on all systems (same systems tested by me), but the command must be changed to:
ZF2_PATH=`pwd`/vendor/zendframework/zendframework/library \php vendor/phpunit/phpunit/composer/bin/phpunit -c module/User/tests/
The command above is not the same as in the book.
So... Here we have two ways mentioned to go with PHPUnit tests to make them work. It is up to us to decide witch of two is better. I would prefer the one with increasing PHPUnit version to 4.5 at the moment.
Many thanks go to Slavey Karadzhov - the author of the book who made isolated development environment for programming with Zend Framework 2(ZF2) available.
Also, thanks to #jamaldo, #steeler and #malte whose activity (post, comments) brought me on the right track. The post I am talking about is here: https://stackoverflow.com/questions/23881289/php-vendor-bin-phpunit-is-printing-in-console-a-text
I'm a newbie programmer and I have tried for an embarrassingly long time to get PHPUnit set up and working with WAMP. I have read the documentation and went through various sites to see what I'm doing wrong, but I give up! I need someone to explain this to me in simple terms.
I've probably seen all the guides on how to set it up, but feel free to link me to something you believe is foolproof!
Try this blog : http://nishutayaltech.blogspot.com/2011/04/installing-phpunit-on-windows.html
This setup is for Windows. Hope this will help you.
Also check this out, may be useful http://www.mark-leong.com/installing-php-and-phpunit-on-windows-7/
// adding required pear channels
pear channel-update pear.php.net
pear upgrade-all
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com
pear update-channels
// performing install
pear install --alldeps --force phpunit/PHPUnit
If everything ok, check whether phpunit have been installed by putting in command line
phpunit -v
If you encountered any errors or interrupted installing
pear clear-cache
may be usefull in that case.
1) download https://phar.phpunit.de/phpunit.phar
2) run it via php phpunit.phar
Btw if that file ever goes away you can look at the original directions here: https://github.com/sebastianbergmann/phpunit
I recommend using composer. It can be used per project, but also 'globally'.
Make a folder on your C drive called 'globalpackages', then cd into it from the command line.
run 'composer require phpunit/phpunit'
once that finishes run 'composer install'
once that finishes you can check the contents of C:\globalpackages\vendor\phpunit' and should see two phpunit files, one of them a bat.
Add C:\globalpackages\vendor\phpunit to your system path and then you will be able to run phpuni from anywhere on your system.
For me this is the fastests and easiest way.
Make sure you have wamp with WAMP with PHP 7 installed as phpunit requires PHP7 to work! or at least 6.2
Go to folder
wamp64\bin\php\php7.0.10
(wherever you installed your wamp - BEWARE last folder is name of PHP version you have so it might be different then above )
We will be working in this folder during entire installation. So if I say edit file etc it means in this folder.
Download newest version of phpunit from https://phpunit.de/
At the time of writing this I get version phpunit 6.2 and php7.0.10
4. Copy downloaded file to wamp64\bin\php\php7.0.10 folder
For me it's: phpunit-6.2.2.phar
for you it might be different version.
Now change name of the phpunit-6.2.2.phar to phpunit.phar
Now make sure you have added your php to PATH in Environment Variables
To do this on Windows 10 and Windows 8
6a. In Search, search for and then select: System (Control Panel)
6b. Click the Advanced system settings link.
6c. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
6d. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Paste location of your PHP. For me it would be: D:\AnyFolderYouInsalledWampTo\wamp64\bin\php\php7.0.10
(if in PATH there was other command or path just add ";" at the end. For exmaple:
%someOtherCommand%; D:\AnyFolderYouInsalledWampTo\wamp64\bin\php\php7.0.10)
6e. Click OK. Close all remaining windows by clicking OK.
In cmd (to run cmd go to search in Windows and type cmd.exe and click Enter)
go to folder wamp64\bin\php\php7.0.10
(to go to folder type for example cd D:\AnyFolderYouInsalledWampTo\wamp64\bin\php\php7.0.10)
now once you are in this folder run this command in cmd:
echo #php "%~dp0phpunit.phar" %* > phpunit.cmd
now run this command:
phpunit
you should get list of all commands available in phpunit
now run this:
phpunit --check-version
you will get info what phpunit version you have and if you are using the newest version.
if this doesn't work go to wamp64\bin\php\php7.0.10
Edit file phpunit.bat
Add this line:
""%PHPBIN%" "D:\AnyFolderYouInsalledWampTo\wamp64\bin\php\php7.0.10\phpunit.phar" %*
Repeat point 9.
if it still doesn't work let me know.
Very well done, Emil! You're a star! Perfect answer to install PHPUnit with WAMP on Windows. Look no further than Emil.
https://stackoverflow.com/users/5753967/emil