I'm trying to set up a pre-commit git hook that will run and validate our unit tests. We are using PHPUnit in the Symfony 2 platform.
For some reason it seems that when I run the unit tests via the git hook that it is using a different version of PHP.
When I check my php version I get:
php -v
PHP 5.4.14 (cli) (built: May 8 2013 10:23:18)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
Here is my git hook:
#!/usr/bin/php
<?php
// Hook configuration
$project = 'My Project';
// Tell the commiter what the hook is doing
echo PHP_EOL;
echo '>> Starting unit tests'.PHP_EOL;
// Execute project unit tests
exec('bin/phpunit -c app/', $output, $returnCode);
// if the build failed, output a summary and fail
if ($returnCode !== 0)
{
// find the line with the summary; this might not be the last
while (($minimalTestSummary = array_pop($output)) !== null)
{
if (strpos($minimalTestSummary, 'Tests:') !== false)
{
break;
}
}
// output the status and abort the commit
echo '>> Test suite for '.$project.' failed:'.PHP_EOL;
echo $minimalTestSummary;
echo chr(27).'[0m'.PHP_EOL; // disable colors and add a line break
echo PHP_EOL;
exit(1);
}
echo '>> All tests for '.$project.' passed.'.PHP_EOL;
echo PHP_EOL;
exit(0);
When I run the unit tests manually ("bin/phpunit -c app/" from my project directory) the tests execute with out error. When I run the tests via the git hook I get a PHP Parse Error. I've determined that the parse error stems from the use of array bracket notation (['key'=>'value']) that was added in PHP 5.4
When I echo php -v in the git hook I get the following output
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
Since the Zend Engine is different (2.4.0 when run manually and 2.3.0 when run via the git hook) I'm assuming that there is a PHP version mismatch happening.
Does anyone have any clues as to why this is happening?
Thanks!
the git/phpunit executable most likely sees a different PATH environment variable than your shell (bash/zsh/...) executable.
If a program looks for i.e. php - the first match from your PATH variable will be used.
Probably you're adding a folder containing a different PHP executable to the PATH variable in one of your shell startup files.
Possible files include:
/etc/profile
/etc/zshenv
/etc/bashrc
~/.profile
~/.bashrc
~/.zshrc
...
Related
I need to use a php script without "php" command.
For example:
$ ./test.php
Permissions are sets to 755.
This is the script
#!/usr/bin/php -q
<?php
echo "hello world";
?>
/usr/bin/php -v (so path exists)
returns
PHP 7.0.15-1+deb.sury.org~xenial+1 (cli) (built: Jan 20 2017 08:53:13) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.15-1+deb.sury.org~xenial+1, Copyright (c) 1999-2017, by Zend Technologies
This is the error I'll get everytime:
Exception: Zend Extension ./test.php does not exist
Also calling script with fullpath I'll get same error.
Calling this it works properly
$ php ./test.php
Any idea?
NOTE: The author found the solution and put it up in the comments but never posted an actual answer, so this answer is just clarifying what the author already said above so as to make the answer more obvious.
I was also getting the Exception: Zend Extension does not exist when I was trying to pipe an email via cpanel forwarder into a php script.
I opened the file in my editor (Komodo Edit on Windows) and went to EDIT > CURRENT FILE PREFERENCES and noticed that LINE ENCODINGS was set to DOS/Windows (\r\n)
I changed the LINE ENCODING to UNIX (\n) and saved it and re-uploded it and the error went away and all is good now.
Obviously the steps will vary depending on what editor you use, but the solution is to make sure your Line Encodings are UNIX and not DOS/Windows.
Just run dos2unix on the file
# ./database.php
Exception: Zend Extension ./database.php does not exist
# apt install dos2unix
# dos2unix database.php
dos2unix: converting file database.php to Unix format...
# ./database.php
Yeah!!! It work's!!!!
Currently running PHPUnit with Codeception on my Windows machine gives me an error:
[PHPUnit_Framework_Exception]
Use of undefined constant SIGTERM -
assumed 'SIGTERM'
As far as I know is that SIGTERM is a constant provided by PCNTL, which is not supported in Windows. In that way this CONSTANT shouldnt be used for a test running on Windows env. at all.
My PHP setup:
PHP 5.6.17 (cli) (built: Jan 6 2016 13:28:38)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
PHPUnit
PHPUnit 5.7.15
Codeception
Codeception PHP Testing Framework v2.2.9
phiremock-codeception-extension
phiremock-codeception-extension v1.2.2
The problem is depending on phiremock-codeception-extension which uses SIGTERM without checking the OS/PCNTL extension is available. So I created a Bug report at GitHub https://github.com/mcustiel/phiremock-codeception-extension/issues/4..
This are the infected code lines - https://github.com/mcustiel/phiremock-codeception-extension/blob/master/src/Extension/PhiremockProcess.php#L74
/**
* Stops the process.
*/
public function stop()
{
$this->process->signal(SIGTERM);
$this->process->stop(3, SIGKILL);
}
I will update this answer once the problem has been fixed by the developer.
Update
This issue has beend fixed in phiremock-codeception-extension v.1.2.3.
I have a wierd problem with executing gulp from php script. Tests:
browser: http://server.com/pull.php <- does not work
shell: php pull.php <- works
shell: gulp <- works
Here is my setup:
Ubuntu 12.04.5 LTS
Node v5.6.0
PHP 5.5.30-1+deb.sury.org~precise+1 (cli) (built: Oct 4 2015 16:14:34)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
Here are packages in package.json:
"gulp": "^3.9.1",
"gulp-concat": "^2.6.0",
"gulp-less": "^3.0.5",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^1.5.2",
"gulp-uglifycss": "^1.0.6",
"gulp-watch": "^4.3.5"
It works perfectly when i run this from shell but returns error when run in php as webserver:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Object #<Object> has no method 'existsSync'
at /usr/local/lib/node_modules/gulp/node_modules/liftoff/lib/find_config.js:21:10
at Liftoff.buildEnvironment (/usr/local/lib/node_modules/gulp/node_modules/liftoff/index.js:67:20)
at Liftoff.<anonymous> (/usr/local/lib/node_modules/gulp/node_modules/liftoff/index.js:192:32)
at /usr/local/lib/node_modules/gulp/node_modules/liftoff/node_modules/flagged-respawn/index.js:17:3
at Liftoff.<anonymous> (/usr/local/lib/node_modules/gulp/node_modules/liftoff/index.js:185:9)
at /usr/local/lib/node_modules/gulp/node_modules/liftoff/index.js:159:9
at /usr/local/lib/node_modules/gulp/node_modules/v8flags/index.js:99:14
at Array.0 (/usr/local/lib/node_modules/gulp/node_modules/v8flags/index.js:38:7)
at EventEmitter._tickCallback (node.js:192:41)
Command to run it is simple:passthru('gulp 2>&1');. I hoestly don't know what is wrong, owner of the files, web server (nginx runs as) user and git owner is the same user named git. Tried various different things as making bash script to run it from php but no luck. I am guessing i am missing some system variables?
Thanks in advance, let me know if you need more info.
Turns out node couldn't play nicely with gulp. My solution:
passthru('node ./node_modules/gulp/bin/gulp.js 2>&1');
I can't explain why, just tried few random ideas and that one worked. One thing bothers me though, why can't i get full info on git pull? I am only getting:
Updating dcd958f..db05960
I have been using the CLI interface to send out cron jobs from my codeigniter page. It worked fine until I updated Wordpress yesterday. I do not know how this effected Codeigniter but that is when the trouble started. I also installed cURL at about the same time. I am not sure if that could have made a difference.
SYMPTOMS:
None of my codeigniter CLI scripts work. I have two scripts that send out email reminders, and another that synchronizes my database and none function.
ERRORS:
I had some errors come up when I tried to run my scripts such as:
Use of undefined constant __DIR__ - assumed '__DIR__'
This was never a problem before. But for now I change that to
dirname(__FILE__)
and that seemed to help. At least that error stopped.
Next another error notice appeared regarding code in my scripts that I was not getting before: "Can't use method return value in write context in . . ."
This error was in reference to this line of code:
if (!empty($this->get_available_hours($date, $provider_id))) {
I modified this to
$availabehours=$this->get_available_hours($date, $provider_id);
if (!empty($availabehours)) {
And the error stopped. But the script usually sends out email regarding availability and no email is sent.
Now I have no errors. I run the scripts and I get no results. If I purposefully mess with the code and do things wrong, I get the appropriate error messages. So, at some level it is reading the file.
I tried just running a simple "hello world file" as discribed here
https://ellislab.com/codeigniter/user-guide/general/cli.html
And nothing was returned.
I tried a simple email script that would send out an email without accessing my database and it did not send anything to me.
It appeares to me like something has caused my code to be interpreted in an older version of php. So I looked at the version currently running:
When logged into the terminal in PuTTY I get:
PHP 5.2.17 (cli) (built: Feb 23 2012 10:42:34)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
Out of date ...
But when I look in the terminal within WinSCP I get:
PHP 5.5.28 (cli) (built: Sep 4 2015 12:07:49)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
This looks up to date.
Running this works: php -r 'echo "Hello World!\n";'
MY QUESTIONS:
1) What tests can I run to find out what is blocking things with my CLI?
Any tips would be appreciated.
2) Why am I seeing two different versions of PHP depending on the terminal I am running and could this be the cause of my problem?
I'm using Ubuntu Natty with PHP 5.3.5 and PECL Gearman 0.8.0. Here's the version info:
PHP 5.3.5-1ubuntu7.3 with Suhosin-Patch (cli) (built: Oct 13 2011 22:20:48)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with the ionCube PHP Loader v4.0.10, Copyright (c) 2002-2011, by ionCube Ltd., and
with Zend Guard Loader v3.3, Copyright (c) 1998-2010, by Zend Technologies
with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
I'm getting a segmentation fault when trying to run a Gearman Client via the command line (I already have my worker running).
Here's what I get on the command line:
root#Local:~/sandbox# php php_gearman_client.php
Sending job
Segmentation fault
Here is my worker code:
<?php
echo "Starting\n";
# Create our worker object.
$gmworker= new GearmanWorker();
# Add default server (localhost).
$gmworker->addServer();
# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");
print "Waiting for job...\n";
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function reverse_fn($job)
{
echo "Received job: " . $job->handle() . "\n";
$workload = $job->workload();
$workload_size = $job->workloadSize();
echo "Workload: $workload ($workload_size)\n";
# This status loop is not needed, just showing how it works
for ($x= 0; $x < $workload_size; $x++)
{
echo "Sending status: " . ($x + 1) . "/$workload_size complete\n";
$job->sendStatus($x, $workload_size);
sleep(1);
}
$result= strrev($workload);
echo "Result: $result\n";
# Return what we want to send back to the client.
return $result;
}
# A much simpler and less verbose version of the above function would be:
function reverse_fn_fast($job)
{
return strrev($job->workload());
}
?>
And here is my client code:
<?php
# Create our client object.
$gmclient= new GearmanClient();
# Add default server (localhost).
$gmclient->addServer();
echo "Sending job\n";
# Send reverse job
do
{
$result = $gmclient->do("reverse", "Hello!");
# Check for various return packets and errors.
switch($gmclient->returnCode())
{
case GEARMAN_WORK_DATA:
echo "Data: $result\n";
break;
case GEARMAN_WORK_STATUS:
list($numerator, $denominator)= $gmclient->doStatus();
echo "Status: $numerator/$denominator complete\n";
break;
case GEARMAN_WORK_FAIL:
echo "Failed\n";
exit;
case GEARMAN_SUCCESS:
break;
default:
echo "RET: " . $gmclient->returnCode() . "\n";
exit;
}
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);
?>
EDIT
It appears the segmentation fault was being caused by Imagick. So I did the following to deal with the issue.
Remove imagick dpkg --purge --force-all php5-imagick. I had installed this when I was setting up PHP
Restart PHP (This could vary depending on how you installed php)
Restart Gearman Job Server /etc/init.d/gearman-job-server stop && /etc/init.d/gearman-job-server
Everything seems to be working ok now.
since it is segmentation fault that means something is wrong with your installation. Run dmesg to see more details there may be problem with some php extension that could be disabled.
I just also had this problem.
This is the soution I took (to fully resolving it), and steps to observing the error.
Step 1.
Running the Gearman Worker resulted in a:
Segmentation fault
Step 2.
Run dmesg from shell, I observed that this was showing up.
[2423402.716386] php[21232]: segfault at 30 ip b66d0321 sp bfc704c0
error 6 in libuuid.so.1.3.0[b66cf000+3000]
Step 3.
Googled for libuuid.so and it became apparent that Imagick uses uuid_create() for Image UUIDs.
http://usrportage.de/archives/922-PHP-segfaulting-with-pecluuid-and-peclimagick.html
Step 4. (The solution)
I ensured that Imagick extension gets initialized last out of all of my PHP extensions.
So previously I had in the first line of my
/etc/php5/conf.d/extensions.ini file imagick.so
What I did instead was created a file called imagick.ini and the contents of that were
imagick.so
Because the extensions are loaded in alphabetical order (and if you are running your Worker through command line), loading imagick last ensures that all the dependant extensions are loaded first. In this case libuuid.