How do I tell Phing where PHPUnit is installed? - php

Both Phing and PHPUnit were installed from pear on ubuntu 12.04.
PHPUnit is located at /usr/bin/phpunit and /usr/bin is on the unix path and also on the include_path in PHP.INI
How do I tell Phing where PHPUnit is installed?
webroot#gm3:~/med1/pub$ phing utest
Buildfile: /home/webroot/med1/pub/build.xml
myproject > utest:
[echo] Unittests PHPUnit...
Execution of target "utest" failed for the following reason: /home/webroot/med1/pub/build.xml:8:40: /home/webroot/med1/pub/build.xml:8:40: PHPUnitTask requires PHPUnit to be installed
BUILD FAILED
/home/webroot/med1/pub/build.xml:8:40: /home/webroot/med1/pub/build.xml:8:40: PHPUnitTask requires PHPUnit to be installed
Total time: 0.1148 seconds

Answering my own question, I think the PHP version on this machine could be too old and not be able to handle PHAR archives. Or maybe the PHAR configuration is broken. PHPUnit is installed as PHAR on this machine via PEAR.
A workaround for me was to not use <phpunit> but <exec> in the target, which worked:
<target name="test">
<exec command="phpunit --bootstrap tests/bootstrap.php --configuration tests/phpunit.xml">
...
I am not fixing this for now and I am expecting on newer machines it will work out of the box.

Related

Unable to globally install older version of phpunit phar

As given in https://phpunit.de/manual/current/en/installation.html#installation.phar.verification, the steps to globally install the PHAR are:
$ 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.
I followed the above, but with the URL for the older version, i.e. https://phar.phpunit.de/phpunit-old.phar (because our PHP version is older). I ran the following commands then -
$ chmod +x phpunit-old.phar
$ sudo mv phpunit-old.phar /usr/local/bin/phpunit
Note - My PHP version is 5.3.29. and the Old Stable Release section in phpunit.de says PHPUnit 4.8 is supported on PHP 5.3, PHP 5.4, PHP 5.5, and PHP 5.6.
Looks fine till here. But, running phpunit --version gives -
PHP Fatal error: require(): Cannot redeclare class phpunit_extensions_database_constraint_tableisequal in /usr/local/bin/phpunit on line 109
zend_mm_heap corrupted
So, instead of moving the .phar to /usr/local/bin/ (in the step 3), I was managing so far by running this -
$ php phpunit-old.phar –-version
I was also able to run my unit test cases in this way -
php /home/sandeepan/phpunit-old.phar /var/cake_1.2.0.6311-beta/app/webroot/openx/lib/ad_agencies/unittests/Admarvel_generic_network_test.php
But, now I need to integrate phpunit with phing. I would like to use the basic utilities provided by PHPUnitTask of phing. So, I guess it needs phpunit phar to be globally installed.
I tried my luck by writing the following -
<phpunit haltonfailure="true" haltonerror="true"
pharlocation="/home/sandeepan/phpunit-old">
<formatter type="plain" usefile="false" />
<batchtest>
<fileset dir="${dir.scratchpad}/${dir.subdir}/unittests">
<include name="**/*_test.php"/>
</fileset>
</batchtest>
</phpunit>
But I get this error -
BUILD FAILED
...
: PHPUnitTask requires PHPUnit to be installed
Update
With reference to stackoverflow.com/a/23410676/351903, I tried with this older version of Phpunit, i.e. PHPUnit-3.7.35. Now, phpunit --version command works. But I still have no success using the PHPUnitTask of Phing. Still getting PHPUnitTask requires PHPUnit to be installed error.
Update 2
The solution which worked for me was using PHPUnit 3.7.35. It seems there is some compatibility issue of phing with PHPUnit 4.8.
I have just downloaded the old PHPUnit file you provided from the phpunit.de website, and have applied succesfully the commands you wrote. Running phpunit worked like a charm in my case.
Perhaps there is an incompatibility between your PHP version and the PHPUnit version you downloaded?
I cannot reproduce the issue you describe:
$ wget https://phar.phpunit.de/phpunit-old.phar
--2016-04-07 09:47:35-- https://phar.phpunit.de/phpunit-old.phar
Resolving phar.phpunit.de (phar.phpunit.de)... 188.94.27.25
Connecting to phar.phpunit.de (phar.phpunit.de)|188.94.27.25|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://phar.phpunit.de/phpunit-4.8.24.phar [following]
--2016-04-07 09:47:35-- https://phar.phpunit.de/phpunit-4.8.24.phar
Reusing existing connection to phar.phpunit.de:443.
HTTP request sent, awaiting response... 200 OK
Length: 3086772 (2.9M) [application/octet-stream]
Saving to: ‘phpunit-old.phar’
phpunit-old.phar 100%[=====================================================================================================================================>] 2.94M 3.97MB/s in 0.7s
2016-04-07 09:47:36 (3.97 MB/s) - ‘phpunit-old.phar’ saved [3086772/3086772]
$ php phpunit-old.phar --version
PHPUnit 4.8.24 by Sebastian Bergmann and contributors.
The solution which worked for me was using PHPUnit 3.7.35. It seems there is some compatibility issue of phing with PHPUnit 4.8.
Source - Phing can't see PHPUnit

PHPUnit "Could not read phpunit.xml" on Travis CI

I am encountering a strange issue while trying to run PHP unit tests on Travis CI.
.travis.yml
sudo: false
language: php
php:
- 5.4
env:
- VUFIND_HOME=$PWD VUFIND_LOCAL_DIR=$PWD/local
before_script:
- pear install pear/PHP_CodeSniffer
- pear channel-discover pear.phing.info
- pear install phing/phing
- composer global require fabpot/php-cs-fixer
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- phpenv rehash
script:
- phpunit --stderr --configuration module/VuFind/tests/phpunit.xml
- phpunit --stderr --configuration module/Swissbib/tests/phpunit.xml
- phpcs --standard=PEAR --ignore=*/config/*,*/tests/* --extensions=php $PWD/module
- phing php-cs-fixer-dryrun
module/VuFind/tests/phpunit.xml is a third party framework
module/Swissbib/tests/phpunit.xml is our own code
module/Swissbib/tests/phpunit.xml
<phpunit bootstrap="Bootstrap.php">
<testsuites>
<testsuite name="sbvfrd">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
The tests from the third party framework run without errors. Our own tests do not work and we get the error message:
$ phpunit --stderr --configuration module/Swissbib/tests/phpunit.xml
Could not read "module/Swissbib/tests/phpunit.xml".
Locally (Mac OS X) all the tests run through. Strangely enough the Bootstrap.php defined in module/Swissbib/tests/phpunit.xml runs completely through on Travis CI, I verified this using echo statements. Nevertheless phpunit tells us that it could not read phpunit.xml.
Travis: https://travis-ci.org/swissbib/vufind
Repo: https://github.com/swissbib/vufind (development branch)
Any ideas what could be going wrong?
I found the solution by downloading the phpunit source and debugging with it.
We were changing the directory within the Bootstrap.php file to a different location then the phpunit command was run from. We run the phpunit command from our project root folder and then changed the working directory to the tests folder, because we were using relative paths. I changed everything to absolute paths (using __DIR__) so we do not have to change the working directory anymore.
Bottom line: Do not change the directory in the bootstrap file as it causes phpunit to fail with this error message: Could not read phpunit.xml.

Phing can't see PHPUnit

I'm trying to setup a new Continuous Integration server that utilizes Phing and PHPUnit for automatically running test cases.
I've installed Phing with Pear:
pear channel-discover pear.phing.info
pear install phing/phing
I've installed PHPUnit using the new PHAR method:
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
Then I go to the directory where my build.xml file is located, rung phing and it looks like Phing has no idea where PHPUnit is.
[jenkins#leroy workspace]$ phing
Buildfile: /home/jenkins/.jenkins/jobs/Framework/workspace/build.xml
Framework > test:
[echo] Running Tests
Execution of target "test" failed for the following reason: /home/jenkins/.jenkins/jobs/Framework/workspace/build.xml:9:37: /home/jenkins/.jenkins/jobs/Framework/workspace/build.xml:9:37: PHPUnitTask requires PHPUnit to be installed
BUILD FAILED
/home/jenkins/.jenkins/jobs/Framework/workspace/build.xml:9:37:
/home/jenkins/.jenkins/jobs/Framework/workspace/build.xml:9:37: PHPUnitTask requires PHPUnit to be installed
Total time: 0.0764 seconds
[jenkins#leroy workspace]$
Checked the location of Phing and PHPUnit:
[jenkins#leroy workspace]$ which phing
/usr/bin/phing
[jenkins#leroy workspace]$ which phpunit
/usr/local/bin/phpunit
And, making sure PHP is new enough:
[jenkins#leroy workspace]$ php -v
PHP 5.3.3 (cli) (built: Dec 11 2013 03:29:57)
I'm doing all of this because we're replacing an older Jenkins server with this new one. Trying to use the newest software, but I can't figure out how to tell Phing where to find PHPUnit.
Thanks in advance for any help!
David
Seems like adding the pharlocation attribute to the phpunit task element, pointing to your local phpunit executable, does the job:
<phpunit haltonfailure="true" haltonerror="true"
bootstrap="./fw_init.php"
pharlocation="/usr/local/bin/phpunit">
<formatter type="plain" usefile="false" />
...
</phpunit>
It looks like there are some actual issues with phing and the latest 4.x versions of PHPUnit: http://www.phing.info/trac/ticket/1091.
So to fix the issue, I removed PHPUnit 4 and specified an older version:
pear install phpunit/PHPUnit-3.7.35
Phing and PHPUnit worked immediately at this point.
Here is a guy with exactly the same problem: How do I tell Phing where PHPUnit is installed?
His theory is that:
PHP version on this machine could be too old and not be able to handle PHAR archives
I can't comment if he is right or not, but that question received no replies from anyone other than the author
Maybe a silly remark, but do you have /usr/local/bin/phpunit in your PHP include path?
I get the same error when I remove the location of phpunit.phar from my include path.

How to implement PHPUnit

I am trying to work with Visual PHPUnit.
When I am using phpunit command its running nicely. No error occurs and my tests running successfully. Output of phpunit --version is as follows:
PHPUnit 4.0.14 by Sebastian Bergmann.
I installed phpunit using PEAR. Output of which pear is as follows:
/usr/bin/pear
On Line #9 of app/config/bootstrap.php of Visual PHPUnit, I set /usr/bin/pear as pear_path.
It shows the following errors when I try to run VPU from browser...
Warning: require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory in /path_to_vpu/app/config/bootstrap.php on line 83
Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.:/usr/share/php:/usr/bin/pear:/path_to_vpu:/usr/share/php') in /path_to_vpu/app/config/bootstrap.php on line 83
Output of which php is as follows:
/usr/bin/php
My Server is Ubuntu 13.10 Saucy. Output of php -v is as follows:
PHP 5.5.3-1ubuntu2.2 (cli) ....
Output of pear list -c phpunit is as follows:
INSTALLED PACKAGES, CHANNEL PEAR.PHPUNIT.DE:
============================================
PACKAGE VERSION STATE
DbUnit 1.3.1 stable
File_Iterator 1.3.4 stable
PHPUnit 4.0.14 stable
PHPUnit_Selenium 1.3.3 stable
PHPUnit_Story 1.0.2 stable
PHPUnit_TicketListener_GitHub 1.0.0 stable
PHP_CodeCoverage 1.2.17 stable
PHP_Invoker 1.1.3 stable
PHP_Timer 1.0.5 stable
PHP_TokenStream 1.2.2 stable
Text_Template 1.2.0 stable
My question is, why I can't include PHPUnit/Autoload.php?
Edit
My php.ini files (both cli and web) has the following:
include_path = ".:/usr/share/php:/usr/share/pear"
Your include path is probably not setup correctly.
Follow the instructions in the PEAR manual, section "Verifying the include path".
This is what happened to me after PHPUnit upgrade with PEAR. Before upgrade, VPU was working. Also I could not downgrade it via PEAR. After some tweaking, it worked again.
I installed PHPUnit under VPU web directory using composer. Place this composer.json file in VPU web directory.
composer.json
{
"require-dev": {
"phpunit/phpunit": "4.1.*"
}
}
and run composer install. After this, a small change in app/config/bootstrap.php file is needed;
//require_once 'PHPUnit/Autoload.php';
//require_once 'PHPUnit/Util/Log/JSON.php';
// this should be absolute path
require_once '/var/www/.... vpu web path ..../vendor/autoload.php';
and it will work.

PHPUnit and Ubuntu - Issues with include path after Installation with PEAR

I need to install PHPUnit for a web dev project on my Ubuntu box, but am having serious issues. Using suggestions from various sources, I've installed via pear and tried the following:
Currently, phpunit --version returns
PHPUnit 3.7.20 by Sebastian Bergman
when in /usr/share but throws errors everywhere else.
My include path is
include_path = ".:/usr/share/php:/usr/share/php/PEAR:/usr/share/php/File/Iterator"
I've tried different variations ranging from simple /usr/share/php to the full string above. PEAR is in all caps since that is the folder it is in.
Executing with a test case results in
PHP Warning: require_once(File/Iterator/Autoload.php): failed to open stream:
No such file or directory in /usr/share/php/PHPUnit/Autoload.php on line 64
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. require() /usr/bin/phpunit:43
PHP Fatal error: require_once(): Failed opening required
'File/Iterator/Autoload.php'(include_path='/usr/share/php/libzend-framework-php')
in /usr/share/php/PHPUnit/Autoload.php on line 64
pear list -c phpunit returns
Installed packages, channel pear.phpunit.de:
============================================
Package Version State
File_Iterator 1.3.3 stable
PHPUnit 3.7.20 stable
PHPUnit_MockObject 1.2.3 stable
PHP_CodeCoverage 1.2.10 stable
PHP_Invoker 1.1.2 stable
PHP_Timer 1.0.3 stable
PHP_TokenStream 1.1.5 stable
Text_Template 1.1.2 stable
I've confirmed that Autoload.php exist at both php/PHPUnit/ and php/File/Iterator/
My loaded php.ini file is at `/etc/php5/cli.php.ini1 as verified by
php --info | grep "onfiguration"
Any other suggestions would be appreciated.
Search for a file on your computer that is called Autoload.php and is in a file structure like this:
../File/Iterator/Autoload.php
Now open the file that throws the error (/usr/share/php/PHPUnit/Autoload.php in your case) and include a set_include_path statement that points to the absolute path of that file.
You seem to have a very similar problem to what I experienced on Mac OSX. For more information look at this very similar question. If all of this doesn't help, try to install with Composer, which I have heard doesn't have this issue.
For recent projects, I'm using Composer to install PHPUnit and the other related tools. It's quite easy:
## ... rest of the composer.json file
# "php -f composer.phar update --dev" to install
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
I also then have a shell script to actually run the PHPUnit command-line tool from the ./vendor/bin/ directory where a link to the locally installed script is installed.
Like Alister's answer, relying on Composer,
but I prefer to deploy the updated PHPUnit globally across my projects with:
composer.phar global require "phpunit/phpunit=dev-master"
That way, when I am at the root dir of my Symfony project, I can just run
~/.composer/vendor/bin/phpunit -c app/ -v --filter testFireNotifications |tee phpunit.log
That last bit with --filter was to run something selectively (actually, more for fast web automation via crontab, than testing...)
Even on a current Ubuntu 14.04.2 server, the repo is stuck with an outdated PHPUnit 3.7.28 "stable", so I sidestep the OS-provided one with this custom install that won't conflict (I haven't put ~/.composer/vendor/bin in my PATH).

Categories