Should how I write PHP and PHPUnit command? - php

As my phpunit file test/a.php needs to use the PHP extension A.so, I write the following command:
php -d extension=A.so -d max_execution_time=100 /usr/local/bin/phpunit -v --debug test/
When I run the command and it will output the following info:
/usr/bin/env php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/phpunit/5.4.6/libexec/phpunit-5.4.6.phar "$#"
Why does it not run the phpunit code?
If I add extension=A.so to php.ini file and run phpunit file, it will be OK.
So, I want to combine the php command and the phpunit command.

Related

GitLab CI failing after call php in a docker composer exec command

My CI script is failing without any error message, do you guys have any idea why?
docker compose exec -T my-app-name bash -c 'php -d xdebug.mode=off ./vendor/maglnet/composer-require-checker/bin/composer-require-checker check --output=json > composer-require-check-report.json'
# I also tried variations, always with the same result.
docker compose exec -T my-app-name php -d xdebug.mode=off ./vendor/maglnet/composer-require-checker/bin/composer-require-checker check --output=json > composer-require-check-report.json
docker compose exec -T my-app-name XDEBUG_MODE=off php ./vendor/maglnet/composer-require-checker/bin/composer-require-checker check --output=json > composer-require-check-report.json
docker compose exec -T my-app-name bash -c 'php -d xdebug.mode=off ./vendor/maglnet/composer-require-checker/bin/composer-require-checker check --output=json' > composer-require-check-report.json
docker compose exec -T my-app-name bash -c 'XDEBUG_MODE=off php ./vendor/maglnet/composer-require-checker/bin/composer-require-checker check --output=json > composer-require-check-report.json'
As mentioned, the command is executing correctly and the file is generated at the end.
The command works well locally (and apparently works well in the runner too), I just don't know why the job is failing.

Why can't I run the composer script from PHP using exec, shell_exec, system?

I can run about any binary but not that composer.phar using PHP's exec or shell_exec.
In the same folder I have composer and another php executable with same permissions:
ls -lh
total 1,8M
-rwxr-xr-x 1 me me 1,8M août 22 20:48 composer.phar
-rwxr-xr-x 1 me me 39 août 22 21:05 test.php
Test.php contains:
#!/usr/bin/env php
<?php
print 'hello';
I then have this script:
<?php
print $cmd = "composer.phar --version 2>&1" ;
print "<br>";
$return = exec( $cmd );
var_dump($return);
print "<br><br>";
print $cmd = "test.php 2>&1";
print "<br>";
$return = shell_exec( $cmd );
var_dump($return);
Here is what I get:
composer.phar --version 2>&1
[...]Process.php:81:string 'sh: 1: : Permission denied' (length=26)
test.php 2>&1
[...]Process.php:88:string 'hello' (length=5)
Why do I get that string 'sh: 1: : Permission denied' error? I tried executing in PHP with /usr/bin/env php composer.php /usr/bin/php composer.php, I get the same error.
I solved it by disabling the xdebug extension.
From the doc:
To improve performance when the xdebug extension is enabled, Composer
automatically restarts PHP without it.
So I guess that this "PHP restart" is an issue when calling the binary/phar from PHP.
It is be possible to use the environment variable COMPOSER_ALLOW_XDEBUG to get it working with xdebug, also disbaling a few xdebug options that could alter performances:
<?php
$result = shell_exec('COMPOSER_ALLOW_XDEBUG=1 /usr/bin/env php -d xdebug.remote_enable=0 -d xdebug.profiler_enable=0 -d xdebug.default_enable=0 composer.phar --version 2>&1');
I think it will work with system('php /usr/local/bin/composer install -d /...'), because if you just run composer directly in a shell it will work by reading the shebang at the beginning of the file that says it should be executed by php, but using system() you don't have a shell so you need to specify this yourself.

How to run Docker from php?

I am a beginner in using Docker, and I am trying to run docker from php, specifically I need to run openface code. I used the command lines provided here https://cmusatyalab.github.io/openface/setup/ to make sure that docker is running on my pc correclty and it works. but now I need to call it from PHP, I wrote the same commands in batch file as follows
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash
cd /root/openface
./demos/compare.py images/examples/{lennon*,clapton*}
pause
and tried to execute it in php by calling
echo shell_exec ("test.bat");
but when I run the batch file directly, only the first line is executed. as it seems the next command is not being executed inside the docker container.
how can I make all commands execute?
any help will be very much appreciated,
thank you
The problem is the first bash won't exit before you exit it and the rest of the commands are interpreted by host bash.
What you need is that your work is done and then you get inside a bash (inside the container)
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash -c "cd /root/openface && ./demos/compare.py images/examples/{lennon*,clapton*} && exec bash"
First "bash -c" is to execute the commands and last command exec bash override the main bash and gives you a shell

Pre commit hook to check PHP code coverage and code sniffer for Symfony

How can I do a pre-commit hook for PHP Symfony code to analyze code coverage and code sniffer report? What are commands and tools I should use?
As of now, I can generate a code coverage report using PHPUnit in clover format:
#!/bin/bash
echo "##################################################################################################"
echo "Starting PHPUnit tests : "`date "+%y-%m-%d %H-%M-%S"`
echo "##################################################################################################"
php app/console -e=dev doctrine:database:drop --force
php app/console -e=dev doctrine:database:create
php app/console -e=dev doctrine:schema:create
php app/console -e=dev -n doctrine:fixtures:load
#phpunit -c app --coverage-html build/html
phpunit -c app --log-junit build/unit.xml
'[' -f build/coverage.xml ']'
phpunit -c app --coverage-clover build/coverage.xml
php app/console -e=dev doctrine:schema:drop --force
php app/console -e=dev doctrine:database:drop --force
echo "Finishing Cron at "`date "+%y-%m-%d %H-%M-%S"`
echo "Cron Task Complete"
echo "##################################################################################################"
If your bash script works as expected, you just need to name it pre-commit and put it in your git hooks: /path/to/repo/.git/hooks. You'll find some sample in this directory.
For more information about git hooks: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
Then, for you code sniffer, I recommend https://github.com/squizlabs/PHP_CodeSniffer.
There is also https://github.com/phpro/grumphp that will do everything for you.
No need to use grumphp when you can easily accomplish it with client side git hooks. Example below runs php-cs-fixer and prevents pushing broken code to github repository when you run git push origin ......
your_project_folder/.git/hooks/pre-push
#!/bin/sh
if [ -f ./bin/php-cs-fixer ]
then
./bin/php-cs-fixer fix --dry-run --verbose --diff src
if [ $? -ne 0 ]
then
printf "\n\t\033[1;31m[PHP-CS-Fixer] Push Aborted\n\n\033[0m"
return 1
fi
fi
Grant permissions
chmod +x .git/hooks/pre-push
Test
git push origin .....
See example here.

How to run PHP CLI command form wget?

I want to run a php cli command from wget but it doesn't work:
run.sh file:
wget -t 1 --timeout=40 -e "php file.php"
error:
wget: Invalid command --execute "php file.php"
The files "file.php" and "run.sh" are in the same directory.

Categories