Call phpunit from console in my symfony project - php

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.

Related

How to setup and run laravel, from git?

Either I miss something, or the whole chain lacks something.
Here's my assumption:
The whole point of containerization in development, is to reduce the cost of environment setup, and create a prepared image with all the required pieces.
So, when I read that Laravel Sail is installing laravel via containerization, I get excited. Thus I install it via their instructions, and everything works.
Then the problem begins. Because:
After a successful installation, I create a git repo, with GitHub's default laravel .gitignore
Then I push the newly installed laravel app into my git repo.
Then I ask a developer to start developing it. Please note that:
He does not have PHP installed
He does not have Composer installed
He clonse the repo, and as per installation guide, runs ./vendor/bin/sail up
But ./vender folder is correctly excluded in .gitignore
Thus his command results in:
bash: ./vendor/bin/sail: No such file or directory
He Googles it of course, and finds out that people suggest to run composer update
He goes to install composer, then before that PHP, then all extensoins of PHP, then ...
Do I miss something here? The whole point of containerization was to not install the required environment locally.
What is the proper way of running a laravel app, that is not installed from https://laravel.build, but is cloned from a git repo, WITHOUT having PHP or Composer installed locally?
Update
I found Bitnami laravel docker and it's exactly what containers should be.
You are right and the other developer doesn't need to have php nor composer installed.
All he/she needs is Docker installed on the local machine.
If you scaffolded the project with what is mentioned in the official Laravel docs under the Getting started section, then you will have a docker-compose.yml file in your project root directory.
For Windows
For Linux
For Mac OS
All the developer has to do after git cloning the repository is to run
docker-compose up --build -d
That's it.
For those struggling with this issue... I've found a command that work perfectly fine.
First of all, you don't need to locally have any PHP or Composer installed, maybe there is a misunderstanding about it, all you need is Docker.
Docker will install everything you need in something I understand is like a sandbox, not locally, for each project.
And for those downloaded projects, from GIT as example, that does not have vendor folder, and obviously cannot execute sail up you can simple execute:
docker run --rm --interactive --tty -v $(pwd):/app composer install
That command will download a composer image for docker, if you do not have one yet. Then, will run a composer install and you are free to execute a ./vendor/bin/sail up if you hadn't configured an alias or just sail up if you already configure an alias.
That's all.
The official documentation lists the following command.
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs
If you were to clone a Laravel project and run this command in the project root, it would create a very small container with php and composer installed and run composer in the project root to install all php dependencies. In effect, this installs the Laravel core code into the cloned project. Once the project in set up this way, the user should create a local .env file to match their development evironment.
cp .env.example .env # creates a .env file to be populated for the local environment
With the envronment set up, they can now create the application containers in docker and run the application. Laravel provides the Sail helper for this.
./vendor/bin/sail up -d # runs the docker containers in detached mode
Now it's a matter of setting up the laravel app and running the Laravel app. (I'm assuming the app uses one of the Laravel start kits that rely on Node.js. If you are using a Blade only application, you can skip the "npm" commands.)
sail artisan key:generate # (Best Practice) Generate a new application key on each machine
sail artisan migrate # Scaffold the database structure
sail artisan db:seed # (Optional) Seed the database with data
sail npm install # (Optional) Install front-end dependencies (Inertia, Vue, React, others...)
sail npm run dev # (Optional) Run the front-end framework in development mode
With this, the new developer should be running an exact copy of both the project and the development environment as the original developer.
Your project README may include additional steps to set up some other dependencies, but this is the basic workflow for contributing to a Laravel project.
The only prerequisites for this workflow is to have Docker installed with an Internet connection. This is most easily accomplished on Windows, Mac, and Linux by installing Docker Desktop.
Alternate for Older Projects
If you are working on an older project that doesn't use Laravel Sail, but does have a docker-compose.yml file, you should be able to build and run the necessary containers with the following command.
docker-compose up --build -d
Once you have the containers running, you would need to install the project dependencies directly into the container.
docker ps # find the container ID of your project's container
docker exec -it CONTAINER_ID php artisan key:generate
docker exec -it CONTAINER_ID php artisan migrate
docker exec -it CONTAINER_ID php artisan db:seed
docker exec -it CONTAINER_ID npm install
docker exec -it CONTAINER_ID npm run dev
Of course, Docker Desktop simplifies this process. With a button click you can have a terminal shell open directly in your container eliminating the need for the docker exec command.

Why despite "drush" being installed via `composer global install` during image build, I cannot find the tool from within a running PHP script?

I am developing a PHP web application inside of a Docker container. Using volumes: inside of my docker-compose.yml file, I have specified a local directory so that any files generated are dumped and persist after the container is destroyed.
volumes:
- ./docroot:/var/www/html
Inside my Dockerfile, I RUN a command that installs a command line management tool:
RUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer && \
ln -s /root/.composer/vendor/bin/drush /usr/local/bin/drush
RUN composer global require drush/drush:8.3.3 && \
composer global update
When the container comes up, I can use docker-compose exec -it <container> bash to get inside the container, and everything works fine. drush is in my path, and I can use it globally throughout the container to manage the app.
Now here is the strange part. Part of my application is that I have to run that command from a PHP script inside the container to help automatically manage some of the build process.
Using php, I run exec('drush dbupdate', $output, $retval); $retval returns a exit status of 127, or command not found and $output is empty. If I switch up the exec to use the full path I get an exit status 126.
If I go back into the container, I can run that command just fine. Note all other cli commands work as expected with exec (ls, whoami, etc but which drush returns exist status 1)
What am I missing? Why can I use it with no problems manually, but PHP exec() can't find it? passthru(), shell_exec(), and others have the same behavior.
composer global install will not install the command "globally" for all users, but "globally" as in "for all projects".
Generally, these packages are installed in the home directory for the user executing the command (e.g. ~/.composer), and if they are available in your path is because ~/.composer/vendor/bin is added to the session path.
But when you run composer global require (while building the image) or when you "log in" to the running container (using exec [...] bash) the user involved is root. But when your PHP script runs, it's being executed by another user (presumably www-data). And for that user, ~/.composer does not contain anything.
Maybe do not install drush using composer, but rather download the PHAR file directly or something like that while you are building the image, and put it in /usr/local/bin.
If you are using Drupal >= 8, the recommended way of installing Drush is not as a "global" dependency, but as "project" dependency, so that the appropriate drush version is installed. This comes straight from the docs:
It is recommended that Drupal 8 sites be built using Composer, with Drush listed as a dependency. That project already includes Drush in its composer.json. If your Composer project doesn't yet depend on Drush, run composer require drush/drush to add it. After this step, you may call Drush via vendor/bin/drush

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

Laravel Homestead Installation: bash init.sh not working

I'm trying to install Laravel Homestead in order to be able to test my Laravel projects locally.
I'm following this tutorial: http://laravel.com/docs/4.2/homestead
In the "Installing Homestead" part I followed the second step "Manually Via Git (No Local PHP)",I don't have any PHP installed locally and can't use Composer. (Wasn't PHP supposed to be installed as part of Homestead?)
I got to this part:
Once you have installed the Homestead CLI tool, run the bash init.sh command to create the Homestead.yaml configuration file:
bash init.sh
In command prompt I tried to enter the following command:
bash init.sh
I got the following error:
'bash' is not recognized as an internal or external command, operable program or batch file.
Then I tried to enter the same command in Git Bash, and I got the following error:
bash: init.sh: No such file or directory
When I tried to enter in Git Bash simply
init.sh
I got the following error:
sh.exe": init.sh: command not found
I ran into this problem well. You need to hop into your terminal (I am on Mac.)
Type into your console: cd ~/Homestead
and to make sure you are in your newly generated Homestead folder (you can always click Finder > your home directory > Homestead to confirm it's where it should be),
then while in terminal simply type ls -- if it lists all of the files like Vagrantfile, composer.lock and bash init.sh, you should be in.
Once you know you are in the correct Homestead folder, it is now at that point in which you run bash init.sh
After doing this it now says Homestead initialized!
.sh is a unix/linux script executable, that isn't recognized on Windows. There's a init.bat included in the package, but it isn't referenced in the tutorial.
Just run init.bat from the command line. It will copy the files to:
%HOMEDRIVE%%HOMEPATH%\.homestead
You need install Git Bash, and then run commands into Git Bash.
Try init.bat that worked for me
If the init.bat refused to work
Go to the Homestead folder.
C:/Users/Computer Name/Homestead
You will see init.bat: double click to run it.
Problem solved.
I found the answer you may be looking for. I was having the same problems with this. Just make sure at the end of bash init.sh you put a ;
So in the git bash cmd you will have this line
$ bash init.sh;
Just do it using sudo.
$sudo bash init.sh

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

Categories