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
Related
this is my current status:
I have a running symfony environment, based on a docker image. Everything works fine and from PHPStorm i can execute phpunit tests. But i want to execute them manually from console.
After i started all services by using docker-compose up --build, i login into the phpfpm service by: docker-compose exec -it phpfpm bash
then i move into my symfony project folder that contain all folders and files like "app/, bin/, vendor/, ... composer.json... etc"
I could go by calling vendor/phpunit/phpunit/phpunit but i want to get a shorter way. Is there any chance, maybe calling bin/phpunit or something like this?
Thanks in advance,
Max
PHPUnit has that script in the own composer.json, so bin (or vendor/bin) directory should contains a relevant symlink after running composer install. Also check your composer.json for bin-dir settings.
At least you always can create a symlink:
$ ln -s vendor/phpunit/phpunit/phpunit phpunit
According to the symfony docs the preferable way of using phpunit is by .phar file. In this way you can download phpunit as a .phar file and it will works both outside and inside docker container.
wget https://phar.phpunit.de/phpunit-6.0.phar
php phpunit-6.0.phar --version
Type now php phpunit-6.0.phar to run tests.
So simply put. I did an in-place update from 10.10.5 to 10.11.1 recently and just discovered that my composer package manager is no longer working.
I've tried a number of solutions and have it installed into the correct directory, but my alias 'composer' which was set globally is now not able to find the composer file.
Following the steps as always
First, Download
curl -sS https://getcomposer.org/installer | php
Then move
sudo mv composer.phar /usr/local/bin/
Then update bash_profile
vim ~/.bash_profile
Finally add alias
alias composer="php /usr/local/bin/composer.phar"
And my end result, when calling composer is this
Could not open input file: /usr/local/bin/composer/
Any ideas why this might happen?
Found the answer by talking to someone on reddit.
///
First off, try going to the composer file in /usr/local/bin and double-click it. It should open up in a new Terminal window. This should at least confirm that the file can be executed.
Second, you shouldn't need to have an alias to use composer. You merely point your path to it like this:
export PATH="~/.composer/vendor/bin:$PATH"
If you have any other path variables you'll just attach a colon to the end of it like so:
export PATH="/usr/local/php5/bin:~/.composer/vendor/bin:$PATH"
Hope this helps.
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
I have snow leopard which apparently has php with pear pre-installed. I enabled php but could not find any signs of PEAR. So I have installed it and now phpinfo() shows its installation
include_path .:/usr/lib/php/share/pear
Still when I type in any pear command
$ sudo pear
I get an error: sudo: pear: command not found
What am I missing?
Many ways to skin this cat, but I would type this if you have locate installed (which you probably do):
$ locate bin/pear
That should list one or more things, one of which will look like the path to pear. Let's say it says something like /usr/local/bin/pear. Then your next command is:
$ sudo /usr/local/bin/pear
Two caveats come to mind:
It's possible that locate will list multiple executable pear files. If that's the case, it may be important to pick the right one based on which PHP you're using.
You may want to add the directory where pear is located to your PATH environment variable.
You need to update your system $PATH variable in order for the pear command to work. Edit the bash profile file using the following(if you have textmate):
mate ~/.bash_profile
and add in this line:
export PATH=/usr/local/pear/bin:$PATH
reload your terminal after that and it should work now
Edited:
Thanks for highlighting my mistake trott. I have changed the path to locate where the bin should roughly be(depending on where one chooses to install it)
If you have installed pear directly on PHP (MAMP, for example) you should copy pear to /usr/local/bin:
cp /php5.3.2/pear /usr/local/bin/pear
then export var PATH, and test with "pear" in the shell.
I had a similar issue and required updating secure_path in sudoers as it overrides user's $PATH.
Check for secure_path on sudo
[root#host ~]# sudo -V | grep 'Value to override'
Value to override user's $PATH with: /sbin:/bin:/usr/sbin:/usr/bin
If $PATH is being overriden us visudo and edit /etc/sudoers
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
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.