Showing docker container output within an IDE - php

I am using docker container on Linux Mint host machine to run PHPUnit tests, like this:
docker run -t -i --volume=$PWD:/var/www username/phpunit:v1 --stderr tests/
PHPUnit 4.8.3 by Sebastian Bergmann and contributors.
..........
Time: 1.18 seconds, Memory: 11.25Mb
So, this works fine. My next step was to create File Watcher inside IDE(PHPStorm), so that this docker command is run every time some of the test files change. However, when IDE runs the command, instead of getting the output like above, what I get is this error mesage:
cannot enable tty mode on non tty input
Looks like this is happening a lot on Windows machines, but I couldn't find what causes this issue on Linux. Is there a way to fix this ?

You are running the command from an IDE, which most probably isn't a terminal. So you might want to remove the -t parameter from the command:
docker run -i --volume=$PWD:/var/www username/phpunit:v1 --stderr tests/
Find a more detailed answer here.

Related

PHPUnit Stalling in CI

We're having an issue with PHPUnit sometimes failing to correctly run our test suite in CI. The behaviour manifests as roughly 1/3 tests failing to start and the CI step timing out. Interestingly enough, we don't see this behaviour:
Locally on our machines
Every time we run the tests
Occur with the dusk tests
Our setup is to run the tests using docker compose. We specifically start the dependencies of the application container first and pause for 30 seconds to wait for them to come up (i.e. the database).
Some things I have tried to fix this that haven't made a difference:
Running the tests with process isolation on.
Splitting the test suite into two to run less tests (both splits have the same problem)
The tests are running in Github Actions, this makes around ~7 gigs of ram available to the machines, along with multiple cores.
What is also interesting is that the dusk based tests have never stalled like this, despite them using the same docker-compose file but with additional services (like a Selenium).
I'm not sure if this has a clear answer, but I'm at the point where I'm out of ideas of what to try next (short of hosting the GitHub action runners ourselves).
Output from a failed test looks like:
+ sleep 30
+ docker-compose -f docker-compose.ci.yml run --rm postgres pg_isready -h postgres -d testing -U testing -t 30
postgres:5432 - accepting connections
+ docker-compose -f docker-compose.ci.yml run --no-deps backend phpunit '--filter=/::test[J-Z|j-z]/' --testdox
Pulling backend (***.dkr.ecr.ap-southeast-2.amazonaws.com/builder:acb548c6125ad97e57e8070c414644c7e6f385f5)...
acb548c6125ad97e57e8070c414644c7e6f385f5: Pulling from builder
Digest: sha256:c84a53dbb201b809a2325a93c52d4a362eefa6de64ab98f759e43c5dd363aac2
Status: Downloaded newer image for ***.dkr.ecr.ap-southeast-2.amazonaws.com/builder:acb548c6125ad97e57e8070c414644c7e6f385f5
PHPUnit 9.1.5 by Sebastian Bergmann and contributors.
##[error]The operation was canceled.
A successul run:
+ sleep 30
+ docker-compose -f docker-compose.ci.yml run --rm postgres pg_isready -h postgres -d testing -U testing -t 30
postgres:5432 - accepting connections
+ docker-compose -f docker-compose.ci.yml run --no-deps backend phpunit '--filter=/::test[A-I|a-i]/' --testdox
Pulling backend (***.dkr.ecr.ap-southeast-2.amazonaws.com/builder:acb548c6125ad97e57e8070c414644c7e6f385f5)...
acb548c6125ad97e57e8070c414644c7e6f385f5: Pulling from builder
Digest: sha256:c84a53dbb201b809a2325a93c52d4a362eefa6de64ab98f759e43c5dd363aac2
Status: Downloaded newer image for ***.dkr.ecr.ap-southeast-2.amazonaws.com/builder:acb548c6125ad97e57e8070c414644c7e6f385f5
PHPUnit 9.1.5 by Sebastian Bergmann and contributors.
Audit Controller (Tests\Feature\Admin\AuditController)
✔ Audits index
(I've truncated the full test output)
Sometimes I will see it reach the first test before stalling.
I had similar (but not the same) problem on Codeception framework for tests but I was not using Github Actions and docker.
The issue was in the way how database was deployed to the server during tests. Because of that database was deployed in long long time but after that, tests starts. We didn't have limit on server so there was no time out.
On Local machine it was working fine.
As I see you are running functional/acceptance tests with database, so I propose to check module for database.
Enable verbose with -v --debug option for phpunit first. You will see then when it hangs and you should be able to debug then easily.

How to run PHPUnit with different Linux user

My unit tests are located inside a Vagrant machine
I'm using Xdebug with Apache on Ubuntu 18,
I use PhpStorm to run PHPUnit tests.
The issue is the unit tests are restricted to be run by "apache" user and the way I run them from the command line is:
sudo -u apache phpunit tests/EmailFunctionsLargeTest.php --stop-on-failure
and it works, but how do I simulate the sudo -u apache phpunit when using PhpStorm?
P.S.
PhpStorm runs the tests as vagrant user...
Not possible yet. Feel free to add your scenario as a comment to https://youtrack.jetbrains.com/issue/WI-38656
Create a file php_wrapper.sh at /home/vagrant/php_wrapper.sh with below contents:
#!/bin/bash
sudo /usr/bin/php $#
Replace the command with sudo -u apache $# if you want.
In your PhpStorm setup the remote interpreter as shown in the below screenshot. When you run phpunit, it uses the php_wrapper.sh as a PHP executable and runs seamlessly.

Debug Symfony CLI application with PhpStorm inside Docker container

My current setup involve PhpStorm IDE in which I have imported Symfony 3 projects which is basically CLI tool. On the host machine I don't have PHP installed so I'm running the application from Docker container which has PHP and Xdebug installed.
I don't have issues to debug web applications from Docker containers but with Symfony and this CLI tool it seems a little bit more tricky.
My question is how to properly set this up and debug it from PhpStorm? I tried to create a new debug configuration (PHP Remote Debug) but breakpoints are not trigged.
Suppossing you have followed into the instructions mentioned into the following links:
Can't connect PhpStorm with xdebug with Docker
How to setup Docker + PhpStorm + xdebug on Ubuntu 16.04
Or similar questions
Then you need to follow theese steps:
Step1:
Get shell access to your container via running:
docker exec -ti ^container_id^ /bin/sh
Or if running a debian/ubuntu based one (or you installed manually bash):
docker exec -ti ^container_id^ /bin/bash
The ^container_id^ can be found via docker ps command it is the first column of the table. If running on a small window just pipe int into less -S resulting the command:
docker ps | less -S
Then export the following enviromental variables:
export PHP_IDE_CONFIG="serverName=0.0.0.0:5092"
export XDEBUG_CONFIG="idekey=PHPSTORM"
Please keep in mind to setup the correct value specified into Servers section as you see in the image:
It is important in order not to run into the problem specified in this question.
Then just enable debugger listentin into the phpstorm and spawn the cli as you do when you run a symfony application.

PhpStorm doesn't read PHP configuration from Docker container

What I want
Run PHPUnit by terminal and configuration in PhpStorm
What I Have
Previous problem
Resolved in HERE - about not finding files
File that running
#!/usr/bin/env bash
# echo "Current working directory: '"$(pwd)"'"
cd $(pwd) && docker run --rm -t -v $(pwd):/var/www -e SYMFONY_ENV=dev ezsystems/php:7.1-v1-dev php $#
Summary
When I run this by terminal:
docker-phpez vendor/bin/phpunit --coverage-text
Everything working correctly.
When I try to run this with configuration of PhpStorm, I get warning that: PHP is not installed.
But running this as remote PHP interpreter, gives me:
docker://ezsystems/php:7.1-v1/php /opt/.phpstorm_helpers/phpunit.php --configuration /var/www/phpunit.xml.dist
Testing started at 17:06 ...
The value $_SERVER['IDE_PHPUNIT_PHPUNIT_PHAR'] is specified, but file doesn't exist '/var/www/vendor/bin/phpunit'
Process finished with exit code 1
Looking like mounting doesn't work.
Question:
do you know why? How to fix that?
In previous version of PhpStorm I didn't had any problem with that.
Right now, I have 2016.3.2.
Because after update you have to update tags for phpstorm_helpers docker image. There i put more how it should be fixed: https://youtrack.jetbrains.com/issue/IDEA-189164
After updating PHPStorm to 2017.1.4, what I did:
switched to docker configuration
phpunit loaded from composer's autoload.php
Started to working. Still don't know why docker-phpez not working by PHPStorm, but I will stop for now.

Call phpunit from console in my symfony project

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.

Categories