what's the meaning of -bash PHPUnit command not found - php

i have pear's phpunit installed (see image)
I have that path in the include path (see image below) in php.ini. Note, other pear installations have worked with this include path, so I know there's nothing wrong with that path per see.
Yet, when I'm trying to do a an example functional test with Yii , I move into the directory protected/tests/ and run this code (provided by a book I'm learning)
% cd protected/tests/
% phpunit functional/SiteTest.php
I get told
-bash: phpunit: command not found
any idea what I'm doing wrong?

It means that your shell cannot find the phpunit command. Specify the full path to the command.
/some/path/here/phpunit ....php

Well, the command phpunit couldn't be found. Are you trying to run a PHP file? In that case, write:
php phpfile.php
Otherwise, specify the full path to the command.

Related

How to configure PHPUnit in MAMP with PHAR

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

phpunit command doesn't work for laravel 4 on windows 7

I've recently installed laravel and have written some tests in /tests directory but when I use phpunit at cmd in the same folder that phpunit.xml exists, it says 'phpunit' is not recognized as an internal or external command,operable program or batch file.. I'm using windows 7. what should I do?
The solution for me:
php vendor/phpunit/phpunit/phpunit
This, of course, assumes you've set up a php environment variable in Windows
As Unnawut said, it doesn't work because vendor/phpunit/phpunit/phpunit is not a native Windows executable. You need a .bat or .cmd file that will basically call 'php phpunit'. There should be one in vendor/bin, but to make life easy, try this - create a file phpunit.bat (or .cmd) at the root of your site, containing this:
#ECHO OFF
SET BIN_TARGET=%~dp0/vendor/phpunit/phpunit/phpunit
php "%BIN_TARGET%" %*
Now you can call phpunit from the command line at the root of the site.
If you are a window user and you are having this issue, do this:
You need to tell Window where to find PHPUnit command, you can first of all verify that this file exists in your Laravel project under /vendor/bin
Finally you need to append the full path to /vendor/bin in your window PATH variable,
To do this:
1. Right-click on 'Computer' then click properties
On the second window click Advanced system settings
On the next window under Advanced click Environmental Variables
On the next window double-click PATH then set PATH variable by appending
the full path to your laravel-project/vendor/bin; Notice the ; at the end.
NB: Other variables might already exists in the PATH, so ensure you don't overwrite them by appending your own at the very end
Finally click Ok on all the dialog boxes
alias phpunit="vendor/bin/phpunit"
I added this command in command line instead of just "phpunit"
vendor\bin\phpunit
That worked for me.
Install phpunit globally:
composer global require phpunit/phpunit
Afterwards you will be able to run phpunit ( even on Windows ):
phpunit
The phpunit executable is not in your project root folder, that's why it can't find it.
Now I assume that you already have phpunit in your composer.json file, something like this:
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
When installed by composer, the package will be installed to vendor/vendor_name/package_name. So to run it at your project root, type this command:
vendor/phpunit/phpunit/phpunit
Borrowing from #Chris' excellent answer:
Even better, you can make vendor/phpunit/phpunit/phpunit an environment variable, say "phpunit" and whenever you want to run the test in any laravel project you just call php %phpunit%.
Demonstration
This working for me
In double quotes this command in console windows
"vendor/bin/phpunit"
If it says the following:
$ phpunit tests/Feature/ExampleTest.php
PHPUnit 3.7.21 by Sebastian Bergmann.
Class 'tests/Feature/ExampleTest' could not be found in 'C:\xampp\htdocs\blog1\tests\Feature\ExampleTest.php'.
Instead of typing tests/Feature/ExampleTest.php you say tests " \\Feature\\Example.test" because you're using windows, not mac. :) GL & HF
Using just \ or / will give errors :)
With Laravel phpunit is set up right out of the box. The easiest way to run it on Windows is to add an entry to scripts in your package.json file...
"scripts": {
...
"tests": "php vendor/phpunit/phpunit/phpunit"
},
Now you simply run unit tests with
npm run tests

Run PHP everywhere from command line on windows 7

I'm using Yii and a WampServer 2.4 64bit.
I installed PEAR and PHPUnit manually.
I can only run the commands PHP, PEAR or PHPUnit in the install folder:
E:\wamp\bin\php\php5.4.12
But I want to execute the commands PHP and PHPUnit from here:
E:\wamp\www\repo\project\protected\tests
So I added the path to my php.exe to my PATH environment variable like this:
;E:\wamp\www\bin\php\php5.4.12
But I can't call php -v on E:\wamp\www\repo\project\protected\tests without the error message, that the command is unknown.
I followed a lot of tutorials how to add php or PHPUnit to my environment variable PATH but nothing is working.
So I realy realy hope,someone can explain me where my mistake is.
You probably added a "www" that is not needed in your PATH variable
;E:\wamp\bin\php\php5.4.12
seems the correct version to me.

yiic - can't run - env: php\r: No such file or directory

On MAC OS X Lion - Running MAMP
I'm trying to execute the following yiic command (from Yii framework);
./yiic message ./app/messages/config.php
I'm getting this message:
env: php\r: No such file or directory
I've looked into yiic file and I see:
#!/usr/bin/env php
<?php
require_once(dirname(__FILE__).'/yiic.php');
I've looked into /usr/bin/env
and I see, nothing related to php (I believe):
Should I have there something related with php ?
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/qq/4k5m37mn16bgfpp6yt8ggljc0000gn/T/
Apple_PubSub_Socket_Render=/tmp/launch-L43tVY/Render
TERM_PROGRAM_VERSION=299
TERM_SESSION_ID=1999B4D4-939B-4065-B71C-D9B0563A9EC6
USER=mem
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/tmp/launch-FBmmga/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0:0
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin
PWD=/usr/bin
SHLVL=1
HOME=/Users/mem
LOGNAME=mem
LC_CTYPE=UTF-8
DISPLAY=/tmp/launch-oXxXFl/org.x:0
_=/usr/bin/env
OLDPWD=/usr
As anyone add this error before?
Is this something to do with the fact that php is not on that env think perhaps?
Please advice
It looks you're just experiencing an encoding issue with your yiic file. The shell is trying to use php\r interpreter to open yiic, instead of php.
Run the following command to replace all CARRIAGE_RETURN characters by LINE_FEED :
perl -pi -e 's/\r/\n/g' yiic.php
Worked pretty well in my case.
I just read this blog: could be the very same problem you are experiencing?
The next step is to tell the yiic application, found in the framework folder, to create a new site. The syntax is
yiic webapp path/to/directory
But before you even begin to use this command, let me explain it a bit, as it’s very important and can be complicated. The yiic file is an executable that runs using the computer’s command-line PHP and that really just invokes the yiic.php script. You may be able to call it using just yiic or using ./yiic (i.e., run the yiic command found in the current directory). Or you can more explicitly call either script using php yiic or php yiic.php. Or you may need to indicate the PHP executable to be used: C:\php\php.exe yiic. You should try the variations on this command, as applicable to your computer, just to make sure you can invoke yiic, prior to trying to create the Web application.
In my case that worked for using xampp:
Creating new project:
X:\xampp\php>php.exe X:/xampp/htdocs/yii/framework/yiic webapp X:/xampp/htdocs/YOUR_PROJECT
yiic shell:
X:\xampp\php>php.exe X:/xampp/htdocs/YOUR_PROJECT/protected/yiic shell X:/xampp/htdocs/YOUR_PROJECT/index.php
Just I changed php version of my system form 4 to 7.
I run this command to salve this issue-
source ~/.bash_profile
This solved my issue.

PHP: How can I tweak PHPUnit to use a different PHP interpreter?

My system has two PHP interpreters. One came bundled with the OS and the other I installed via the XAMPP package. All of my PHP extensions are applied to the XAMPP installation but PHPUnit seems to only run the version of PHP that came with my machine.
Does anybody know of a way I can configure or rebuild PHPUnit so that it uses my XAMPP PHP interpreter exclusively?
For Mac/Linux, the first line of the phpunit script with starts with
#!/usr/bin/php
change that to
#!/Applications/XAMPP/xamppfiles/bin/php
or whatever other php interpret you want to use.
Find the folder you installed PHPUnit in. There should be a file called phpunit.bat. It should have a line that reads something like
set PHPBIN="C:\php\php.exe"
%PHPBIN% "C:\php\phpunit" %*
Change it to read
set PHPBIN="C:\xampp\php\php.exe"
%PHPBIN% "C:\xampp\php\phpunit" %*
Or whatever the path to your PHP executable is
Since modifying phpunit file did not work for me because of phar signature errors, I was running phpunit with different php version by calling interpreter explicitly (on Linux):
php7.1 /usr/local/bin/phpunit
php5.6 /usr/local/bin/phpunit
Following the example with XAMPP, full path to php interpreter could be provided:
/Applications/XAMPP/xamppfiles/bin/php /usr/local/bin/phpunit
In agreement with Thomas' statement, additionally there's a line further below
if (strpos('/Applications/MAMP/bin/php5.3/bin/php', '#php_bin') === 0) {
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
}
That I've been told you're also supposed to change to reflect the PHP you're interested in using
(I've set mine to MAMP obviously)
I've switched back and forth from 5.2 and 5.3 a lot recently :)
This applies to phpunit installed using Homebrew on Mac OS 10.9. I’ve editing the file located at /usr/local/Cellar/phpunit/4.2.6/bin as seen below. CAVEAT: I don’t know how Homebrew will handle this on a PhpUnit update, but for now it’s working to be able to select the php version that PhpUnit is using for it's testing.
#!/usr/bin/env bash
php=/Applications/MAMP/bin/php/php5.3.14/bin/php
#php=/Applications/MAMP/bin/php/php5.4.4/bin/php
/usr/bin/env $php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/phpunit/4.2.6/libexec/phpunit-4.2.6.phar $*
On Windows, this may be achieved using a similar approach to the ones mentioned in other replies.
In your /path/to/composer/phpunit directory, open the phpunit file in an editor. The first line should look like this:
#!/usr/bin/env php
Simply download your desired version of PHP for Windows, place the contents of the ZIP file somewhere to your liking, and reference the fully quantified path to the php.exe file, instead of just php. Like so:
#!/usr/bin/env /c/misc/php-5.5.9-nts-Win32-VC11-x86/php.exe
In my case, I put it in /c/misc/php-5.5.9-nts-Win32-VC11-x86/, which corresponds to C:\misc\php-5.5.9-nts-Win32-VC11-x86\ using Windows path syntax.
Remember to verify that the correct php.ini file is being used (php --ini or in a script file php_ini_loaded_file()).

Categories