Execute PHPUnit when is installed as part of a project - php

I have added composer to a standalone project as:
{
"require-dev": {
"phpunit/phpunit": "5.4.*"
}
}
After I run composer require "phpunit/phpunit=5.4.*" --dev the libraries gets installed under vendor/. I have write a small test case and I put it under tests/CollectionTest.php and I want to run it but ....
# phpunit
bash: phpunit: command not found
# phpunit --bootstrap vendor/autoload.php tests
bash: phpunit: command not found
I have also added this:
~/.composer/vendor/bin/
to ~/.bash_rc file. Did I miss something? How do I execute the test case?

PHPUnit is installed in vendor folder in your project directory rather than in global directory. Try to cd into your projects directory and run the PHPUnit by:
$ cd /{project-directory}
$ ./vendor/bin/phpunit
Detailed test setup can be configured with the phpunit.xml file.

You should find an alias in the bin directory to the ./vendor/phpunit/phpunit/phpunit path. So try:
>bin/phpunit
or
>vendor/phpunit/phpunit/phpunit
Hope this help

Related

Access phpunit inside my project folder. phpunit: command not found

I have installed phpunit by opening gitbash of my windows system and running the following command.
composer require --dev phpunit/phpunit ^9
It installed successfully and when I type following command
./vendor/bin/phpunit --version
it returns PHPUnit 9.5.21 . I think phpunit is installed in this location
C:\Users\ERIKSON\vendor\phpunit
Now I have a project in my xampp folder
C:\xampp\htdocs\erikson-project\wp-content\plugins\youtube-plugin
when I open GitBash in this project location and run the command PHPUnit it says
bash: phpunit: command not found
. How can I solve this ? Please help.
I have composer.json file inside youtube-plugin folder and the code is
C:\xampp\htdocs\erikson-project\wp-content\plugins\youtube-plugin\composer.json
{
"require-dev": {
"phpunit/phpunit": "9.5"
}
}
Do I need to add anything in my windows system environment variable?

Unable to run PHP unit

I followed the below tutorial step by step to run PHPUnit. But I am only getting options and configuration part.
https://www.youtube.com/watch?v=cRVkchE5kuE&t=393s
Followed the below steps to setup PHPUnit:-
Installed the composer.
Installed the PHPUnit via command line
PHP composer.phar require --dev phpunit/phpunit ^8
After these steps, I am able to get the version, options, configuration, and other information by using the command
PHPUnit (on path cd /vendor/bin/)
In the tutorial below steps used to run the PHPUnit:-
create phpunit.xml file on root and defined the below code.
<?xml version = "1.0" encoding = "UTF-8" ?>
<phpunit bootstrap = "vendor/autoload.php">
<testsuits>
<testsuit name = "unit">
<directory>tests</directory>
</testsuit>
</testsuits>
</phpunit>
Created the tests directory on the root and also create unit directory in the tests directory and again run the below command.
phpunit on path cd /vendor/bin/
But still, I am getting information part. but in the video, he is getting tested ok line after the command.
Please suggest me, what am I doing wrong?
This is the file structure
R:
vendor
bin
composer
doctrine
.
.
.
.
.
.
autoload.php
tests
unit
composer.json
composer.lock
composer.phar
phpunit.xml
Don't run phpunit from the /vendor/bin/ directory, run it from your project's root directory (the same place as the phpunit.xml file):
/project/dir # vendor/bin/phpunit

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'

Install PHPUNIT with Composer

I have project on Symfony 2 and i would like use PHPUNIT on Windows 7.
On githut phpunit is:
Composer
Simply add a dependency on phpunit/phpunit to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on PHPUnit 3.7:
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
For a system-wide installation via Composer, you can run:
composer global require 'phpunit/phpunit=3.7.*'
Make sure you have ~/.composer/vendor/bin/ in your path.
First i use system-wide installation but i dont know when this installed.
Next i add to my composer.json require-dev.
This installed phpunit in C:/wamp/www/myproject/vendor/symfony. Next i try commands:
composer install --dev
And i can't use phpunit. In cmd.exe i enter "phpunit" and i have error:
'phpunit' is not recognized as an internal or external command operable program or batch file
How can i use phpunit? I have Windows 7, Wamp server and php 5.4.12.
When you install PHP-Unit in windows via composer, the global installation will create files in
C:\Users\YOUR_USERNAME\AppData\Roaming\Composer
To execute phpunit easily via command line you need to add path of phpunit.bat file in windows Environment Variables. For this:
Right click My Computer
Go to Properties -> Advance system settings and
Click Environment variables from the Advance tab.
Now add C:\Users\YOUR_USERNAME\AppData\Roaming\Composer\vendor\bin to the windows PATH.
You can now run the phpunit from command. Note that you may need to restart your command prompt for the changes to take effect.
The bin file of packages are put in the configured bin directory. By default, this is vendor/bin and when you use the symfony standard edition, this is the bin folder.
To execute this bin file, run ./bin/phpunit (or ./vendor/bin/phpunit when not using the Symfony Standard Edition)
Windows users have to put this in double quotes: "bin/phpunit" (or "vendor/bin/phpunit")
composer require --dev phpunit/phpunit ^7
The above example assumes, composer is already on your $PATH variable.
You composer.json should look similar to;
{
"name": "vendor_name/package_name",
"description": "This project is for practicing writing php unit tests",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Umair Anwar",
"email": "umair.anwar#gmail.com"
}
],
"autoload": {
"classmap": [
"src/"
]
},
"require-dev": {
"phpunit/phpunit": "^7",
"phpunit/dbunit": "^4.0"
}
}
Easiest way to install phpunit via composer is to run from project root.
$ composer require phpunit/phpunit
What this would do is, it will create a phpunit folder inside vendor/bin
and you can run unit tests like this..
$ ./vendor/bin/phpunit
Too simple operation on Windows with composer and works for me following way:
Install composer
https://getcomposer.org/doc/00-intro.md#installation-windows Go to
your symphony folder e.g C:\wamp64\www\symfony\UserManagement where is
composer.json and run this command.
Should be register with global to not have issue $phpunit bash: phpunit: command not found
//old version is 5.7 new 6.4 or put newest version.
composer global require --dev phpunit/phpunit ^5.7
I remember futzing around with the composer dependency stuff for phpunit and never could get it to work.
Instead, from your git bash shell:
mkdir ~/bin
cd ~/bin
curl https://phar.phpunit.de/phpunit.phar > phpunit
chmod +x phpunit
exit out of bash and then start a new bash session.
And you should be good to go. You can echo $PATH to verify you have a path to ~/bin but one seems to added by default.
https://phar.phpunit.de/phpunit.phar
I also came across the same issue and find out the solution by following steps
To run PHPUnit in Windows 7 under WAMP installation
Composer Install
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
Simply Set Environment Variable
The php unit will be install in a vendor dir in vendor/bin
Path : C:\wamp\www\myproject\vendor\bin;
Open a new Command Prompt
C:\Users\guny >phpunit --version
PHPUnit 3.7.30 by Sebastain Bergmann

Categories