Running PHPUnit in Laravel's Homestead - php

I'm using Homestead to serve my Laravel application. I'm trying to run PHPUnit. According to the docs:
An example test file is provided in the app/tests directory. After
installing a new Laravel application, simply run phpunit on the
command line to run your tests.
Well, when I'm "simply running" phpunit in my project root (inside the Homestead environment) I get this:
The program 'phpunit' is currently not installed.
Do I need to install PHPUnit separately then? The documentation does not mention it. What am I doing wrong?

You can install it globally on the system using.
composer global require phpunit/phpunit
However, if you need different versions for different projects this can cause issues.
The alternative option is to use the version installed as part of your dependencies by referencing the path to your vendor directory.
./vendor/bin/phpunit
You could even add an alias to your aliases file in your ~/Homestead directory. That way you're always using the phpunit version that is installed with your project dependencies.
alias phpunit=./vendor/bin/phpunit
You'll need to restart the homestead box to make use of the alias.

You can install it globally with:
$ composer global require "phpunit/phpunit=4.4.*"
# then use
$ phpunit
or you can use it with your local composer:
$ composer require "phpunit/phpunit=4.4.*"
# then
$ vendor/bin/phpunit

Since it's a package required for development, Laravel provide PHPunit(require-dev section in composer), you should find it in vendor's folder :
$ your_app/vendor/bin/
You can run the command from the root of your app folder by typing :
$ vendor/bin/phpunit
I hope it will help !

Related

symfony bridge isntalling phpunit to bin directory

I have a fresh symfony project and I need to install phpunit, so I run composer require --dev symfony/phpunit-bridge to install it. It creating symlink to phpunit executable in bin/ folder. But when I'm running tests using bin/phpuinit tests/ command I'm getting message "No composer.json found in the current directory, showing available packages from packagist.org
" and it starting phpunit installation into bin directory and at the end I have bin/.phpunit folder and all the phpunit related files there. Why it installing php unit there and not into vendor, why it's not see composer??? what I'm doing wrong ? Thanks in advance!
What you are using is the symfony/phpunit-bridge
It basically is way more flexible than just phpunit in the vendor folder, allowing to adapt to multiple versions of PHPUnit based on your environment.
Please read the documentation linked above for more details. You're not doing anything wrong!
Another way would be a plain composer require phpunit/phpunit, which would work the "basic"/"standard" way.

Laravel installation withouth vagrant

Is it possible to install Laravel Framework withouth installing vagrant first?
I just read about vagrant and seems just great, however I think I will skip it for now.
I "installed" Laravel through composer, but when I tried to use the Laravel command:
new <app-name>
The console outputs:
laravel command not found.
I already set the ~/.composer/vendor/bin route to the $PATH
In your ~/.profile file you should have something like this if you are using a mac
export PATH="~/.composer/vendor/bin:${PATH}"
If you are using Ubuntu you should have something like this in your ~/.profile file
PATH="~/.composer/vendor/bin:$PATH"
Finally, if you haven't installed the CLI tool for the Laravel Installer just run this code in the terminal
composer global require "laravel/installer=~1.1"
You need to install laravel in order to use it.
Use composer global require "laravel/installer=~1.1" to install laravel

Can't run phpunit tests from command line

I am trying to run unit tests in a new laravel 5 application, using the phpunit framework. In the root path of my laravel application I ru the following command:
./vendor/bin/phpunit /tests/ExampleTest.php
And then I get the following message:
You need to set up the project dependencies using the following commands:
wget http://getcomposer.org/composer,phar
php composer.phar install
I already have composer installed in may system and I install Laravel 5 using composer. Isn't phpunit installed when I install a new laravel 5 application? If not, how can I install it in a existent laravel 5 application?
I known that I can also install phpunit globaly and resolve the problem. But maybe it will be a duplication since I have all the phpunit code already in may laravel application.
I had the same problem and it had a specific solution that may apply to other people. I store my files in Dropbox, and the vendor/bin/phpunit file should be an symlink like this
$ ls -lo vendor/bin/phpunit
lrwxr-xr-x vendor/bin/phpunit -> ../phpunit/phpunit/phpunit
However, Dropbox will occasionally replace symlinks with the original file, and this will cause the error above. The behaviour is inconsistent, and with relative symlinks seems to break when 2 machines are accessing Dropbox at the same time. Fixing the symlink worked for me or you could make a new symlink directly to the vendor/phpunit/phpunit/phpunit outside of Dropbox and run that.
Edit: Nowadays I exclude Vendor and node_modules from Dropbox - and simply run composer install when necessary. This works really well, and also deals with the hassle of syncing so many files on Dropbox. What you can do is go into the folder and delete all the files. Wait for Dropbox to sync. Then mark the folder as excluded. Finally, run composer install and you get the contents as you need. (Delete + composer install often solves other issues too).
Running composer install did nothing in my case. However, removing vendor folder and then calling composer install fixed it.
You need to have Composer installed and run composer install or composer update in your application to install the different packages listed in your composer.json.
When you install your Laravel application it doesn't install the packages right away.
You can verify the packages are installed by looking in the vendor directory of your application and check that phpunit is in there.
did you install phpunit globally? I recommend you do it.
just type in your laravel's root directory (e.g. /var/www)
cd /var/www
phpunit
if you what to test just one file, you can do something like this:
phpunit tests/ExampleTest.php
Unit Test:
D:\xampp\htdocs\Samplemed\vendor\bin>
phpunit ../../tests/Unit/Company/CompanyUnitTest
Feature Test:
D:\xampp\htdocs\Samplemed\vendor\bin>phpunit
../../tests/Feature/Company/CompanyFeatureTest
Please try this. its working fine.

PHPUnit not working with Laravel 5

I just installed a fresh Laravel 5 project, my first one on this version. PHPUnit is supposed to be out of the box with the framework and every tutorials I saw just say to type phpunit within the project folder to launch the Unit Tests.
I checked and PHPUnit is in the composer.json, I also did a composer install and composer update just in case it wouldn't be here
website(master)$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Removing phpunit/phpunit (4.6.1)
- Installing phpunit/phpunit (4.6.2)
Downloading: 100%
But it just doesn't work phpunit isn't recognized at all
website(master)$ phpunit
-bash: phpunit: command not found
Seems like nobody got this problem before as I Googled it. I hope I'm not doing any stupid mistake. Any idea or suggestion ? Thanks guys ;)
I didn't install PHPUnit globally and didn't define the path. So for anyone who would have same problem :
composer global require phpunit/phpunit
composer global require phpunit/dbunit
Then you add this to you ~/.bash_profile or ~/.profile
export PATH=~/.composer/vendor/bin:$PATH
This occurs when you don't have phpunit installed globally.
Run this command to use the local version (installed with composer):
vendor/bin/phpunit
in windows machine the command is different please use this command
php vendor/phpunit/phpunit/phpunit
orignal source
You can run this command in cmd before running phpunit command:
doskey phpunit="vendor/bin/phpunit"
And if you are lazy as I am, you can run this one:
doskey pu="vendor/bin/phpunit"
for people who have WINDOWS 7, use the .\vendor\bin\phpunit command instead of ./vendor/bin/phpunit
Run the command
composer config --list --global | grep -w home
You can find the find the [home] with composer path, similar to this one.
[home] /home/example_username/.config/composer
The path ~/.config/composer is where composer global packages are installed. Next run the command...
export PATH=~/.config/composer/vendor/bin:$PATH
I made a permanent link to my phpunit like this
echo 'alias phpunit=vendor/bin/phpunit' >> ~/.bash_aliases
now phpunit is working by itself and stays even after I restart the terminal
Include this line on your composer.json
"phpunit/phpunit": "4.0.*",
Run composer update.
You should be able to run the following command on your Laravel directory.
vendor/bin/phpunit

How to install PHPunit in a Laravel project

I'm trying to set up PHPunit in a Laravel project (which is being running in a Linux VM using Vagrant and VirtualBox). I've added PHPunit to composer.json and run composer install and composer update, and yet nothing is working. Running phpunit from the command line does nothing. (Nor does php phpunit or php phpunit.phar.)
How do I get PHPunit to run my tests?
You must now run it using:
vendor/bin/phpunit
Install phpunit globally and then can you can use it with just phpunit command in your local project instead of vendor/bin/phpunit
(your composer/vendor/bin has to be in system path)
composer global require phpunit/phpunit
or you can add following alias in your VM and then you can run it within your project directory with just phpunit command
alias phpunit='vendor/bin/phpunit'

Categories