Why phpunit is green when there are tons of exceptions? - phpunit

Why phpunit is green when there are tons of exceptions?
Here's an example job in travis: https://travis-ci.org/ConnectCorp/nexmo-client/jobs/79275172
Strict standards: Declaration of Nexmo\Tests\Service\TestServiceTrait::exec() should be compatible with Nexmo\Service\Service::exec($params, $method = 'GET') in /home/travis/build/ConnectCorp/nexmo-client/tests/Service/VoiceTest.php on line 42
Call Stack:
0.0013 415584 1. {main}() /home/travis/.phpenv/versions/5.4.37/bin/phpunit:0
0.0209 1038200 2. PHPUnit_TextUI_Command::main() /home/travis/.phpenv/versions/5.4.37/bin/phpunit:722
0.0209 1044680 3. PHPUnit_TextUI_Command->run() phar:///home/travis/.phpenv/versions/5.4.37/bin/phpunit/phpunit/TextUI/Command.php:104
0.0209 1045112 4. PHPUnit_TextUI_Command->handleArguments() phar:///home/travis/.phpenv/versions/5.4.37/bin/phpunit/phpunit/TextUI/Command.php:114
0.0281 1812016 5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() phar:///home/travis/.phpenv/versions/5.4.37/bin/phpunit/phpunit/TextUI/Command.php:664
0.0281 1812928 6. PHPUnit_Util_Configuration->getTestSuite() phar:///home/travis/.phpenv/versions/5.4.37/bin/phpunit/phpunit/Util/Configuration.php:814
0.0318 2063368 7. PHPUnit_Framework_TestSuite->addTestFiles() phar:///home/travis/.phpenv/versions/5.4.37/bin/phpunit/phpunit/Util/Configuration.php:901
0.1145 6533504 8. PHPUnit_Framework_TestSuite->addTestFile() /home/travis/build/ConnectCorp/nexmo-client/vendor/phpunit/phpunit/src/Framework/TestSuite.php:403
0.1145 6571744 9. PHPUnit_Util_Fileloader::checkAndLoad() /home/travis/build/ConnectCorp/nexmo-client/vendor/phpunit/phpunit/src/Framework/TestSuite.php:331
0.1146 6571880 10. PHPUnit_Util_Fileloader::load() phar:///home/travis/.phpenv/versions/5.4.37/bin/phpunit/phpunit/Util/Fileloader.php:42
0.1148 6681536 11. include_once('/home/travis/build/ConnectCorp/nexmo-client/tests/Service/VoiceTest.php') phar:///home/travis/.phpenv/versions/5.4.37/bin/phpunit/phpunit/Util/Fileloader.php:58
PHPUnit 4.8.6 by Sebastian Bergmann and contributors.
............................................................... 63 / 117 ( 53%)
......................................................
Time: 511 ms, Memory: 13.00Mb
OK (117 tests, 210 assertions)
https://travis-ci.org/ConnectCorp/nexmo-client/jobs/79275172
I would expect it would fail and travis report that a build has failed, but it just silently passes. Is there any phpunit config option I am missing?
Current phpunit config is this:
<?xml version="1.0" encoding="utf-8"?>
<phpunit colors="true"
bootstrap="./vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Nexmo Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory suffix=".php">vendor/*</directory>
</blacklist>
</filter>
</phpunit>

Even though there is an error, no tests have actually failed, so phpunit will return a success code.
Before running the tests, phpunit first loads and parses *Test.php files and this is where your errors are coming from, rather than from a test method itself.
Changing the config option stopOnFailure="false" to stopOnFailure="true" or stopOnError="true" should make this task fail.
From the documentation:
--stop-on-error Stop execution upon first error.
--stop-on-failure Stop execution upon first error or failure.
Edit
Actually, just been playing with this and as it is a notice and before the tests actually start those flags won't have any effect.

You seem to discovered a bug in phpunit and I suggest you report it to their repository.
It only seems to be a problem if a notice is triggered from the test class. Your mock classes are defined in the same file as a test class. If you move them out to its own file tests will start failing as expected. Methods need to have the same signature as their parent.
Might be also worth to configure error reporting in your phpunit.xml to report all errors (although this alone will not solve your current issue, error reporting will be enable no matter how environment is configured):
<php>
<ini name="error_reporting" value="-1" />
</php>

Related

PHPUnit codecoverage of my Symfony bundle does not work

In those days made a tool in a selfmade framework, and setted up unit tests with PHPUnit for that (whitout Symfony). Then i could make tests with codecoverage whitout problems.
Now i migrated my tool as a Symfony bundle. I can test it now with no errors (whitout codecoverage)
I googled and didn't found anything about CodeCoverage.php undefined notice problems.
phpunit -c app src/myVendor/myBundle/
.............................................................. 62 / 139 ( 44%)
.............................................................. 124 / 139 ( 89%)
...............
Time: 14.5 seconds, Memory: 45.00Mb
OK (139 tests, 5733 assertions)
But when i add the logging configuration for codecoverage, the execution will crash. But it worked before i migrated my tool into a bundle.
<phpunit bootstrap="bootstrap.php.cache"
stopOnError="true"
stopOnFailure="true"
stopOnRisky="true"
stopOnIncomplete="true"
stopOnSkipped="true"
>
<logging>
<log type="coverage-html"
target="C:/Users/FPI/Desktop/PHPUnit_CodeCoverage/html"
lowUpperBound="35"
highLowerBound="70"/>
</logging>
</phpunit>
I recieve this error:
Fatal error: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Notice: Undefined offset: 4658' in phar://C:/bin/phpunit/php-code-coverage/CodeCoverage.php on line 694
Symfony\Component\Debug\Exception\ContextErrorException: Notice: Undefined offset: 4658 in phar://C:/bin/phpunit/php-code-coverage/CodeCoverage.php on line 694
Call Stack:
34.3818 45467848 1. Symfony\Component\Debug\ErrorHandler->handleException() D:\Projekte\myproject\vendor\symfony\symfony\src\Symfony\Component\Debug\ErrorHandler.php:0
Thanks in advice for help. PHPUnit v 4.6.9. PHP 5.5.26.
Thanks to #Jean, an PHPUnit update fixed this issue.

Error when running a PHPUnit test

I get an error when I try to run a test in PHPUnit. Even if I type phpunit by itself, I still get an error. It works fine when I call it outside the Bootstrap folder.
Configuration read from /home/milen/Documents/codific/icho/module/Application/tests/phpunit.xml
PHP Fatal error: Call to a member function getId() on a non-object in /home/milen/Documents/codific/icho/module/Application/tests/ApplicationTest/Controller/PoCourseTest.php on line 268
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /home/milen/Documents/codific/icho/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:426
PHP 6. PHPUnit_Extensions_SeleniumTestCase->run() /home/milen/Documents/codific/icho/vendor/phpunit/phpunit/src/Framework/TestSuite.php:699
PHP 7. PHPUnit_Framework_TestCase->run() /home/milen/Documents/codific/icho/vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumTestCase.php:648
PHP 8. PHPUnit_Framework_TestResult->run() /home/milen/Documents/codific/icho/vendor/phpunit/phpunit/src/Framework/TestCase.php:760
PHP 9. PHPUnit_Framework_TestCase->runBare() /home/milen/Documents/codific/icho/vendor/phpunit/phpunit/src/Framework/TestResult.php:686
PHP 10. PoCourseTest->tearDown() /home/milen/Documents/codific/icho/vendor/phpunit/phpunit/src/Framework/TestCase.php:852
I tried reinstalling it, no luck.
I usually see an error along these lines when I have defined a bootstap.php file to run for PHPUnit which gets specified in the phpunit.xml file. With this setup, you need to be careful from where you run your tests so that the bootstrap file is run properly. This isn't a problem of PHPUnit per se, but a side effect from the phpunit.xml file.
This would appear to be the case because you stated that when you are not in the folder called "Bootstrap" (what / where ever that is), PHPUnit works fine. But without more information aside from the error dump this would be difficult to ascertain.
I think this question could be fixed with this answer.
I don't think its a problem with configuration, but rather a code execution in the setUp and tearDown method.

PhpStorm 7.1.3, XDebug 2.2.3, Symfony 2.4 - WebTestCase not found

I'm getting the following error when attempting to run my own Symfony unit tests in PhpStorm:
/usr/bin/php /home/kevin/www/src/Blah/MyBundle/Tests/Controller/HomeControllerTest.php
PHP Fatal error: Class 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in /home/kevin/www/src/Blah/MyBundle/Tests/Controller/HomeControllerTest.php on line 8
PHP Stack trace:
PHP 1. {main}() /home/kevin/www/src/Blah/MyBundle/Tests/Controller/HomeControllerTest.php:0
Fatal error: Class 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in /home/kevin/www/src/Blah/MyBundle/Tests/Controller/HomeControllerTest.php on line 8
Call Stack:
0.0002 234112 1. {main}() /home/kevin/www/src/Blah/MyBundle/Tests/Controller/HomeControllerTest.php:0
Process finished with exit code 255
Some research has informed me that I may have to point PhpStorm/Xdebug to Symfony's bootstrap and/or test suite XML file, but I'm not sure how to do that in PhpStorm. Nothing is jumping out at me in the various config screens. Please help.
Although I'm using IntelliJ IDEA, the configuration is the same, so:
Settings -> PHP -> PHPUnit - there are inputs for bootstrap and config

PHPUnit reports no tests run, then runs tests

I have an interesting (but ultimately harmless) problem: PHPUnit will report that no tests were executed, and then proceed to read the config file and run the tests, giving an output as below.
It would appear that PHPUnit is exiting and then resuming for some reason. I've looked through my bootstrap process and log files I can't see anything which would cause PHPUnit to exit (an exception or exit statement or the like).
The tests are being run from the tests directory, using the PHPUnit phar: php ../phpunit.phar -c phpunit.xml
Has anybody seen this behaviour before? What was the problem? Ultimately, this isn't harming anything at all, I'm just curious as to what's going on.
PHPUnit 3.7.34 by Sebastian Bergmann.
Time: 2.18 seconds, Memory: 23.50Mb
No tests executed!
PHPUnit 3.7.34 by Sebastian Bergmann.
Configuration read from /path/to/phpunit.xml
............................................................. 61 / 2630 ( 2%)
............................................................. 122 / 2630 ( 4%)
.. and so on ..
The configuration files also appears to be OK, since the tests do run.
<phpunit
colors="true"
bootstrap="/path/to/application/bootstrap.php"
backupGlobals="false"
>
<testsuites>
<testsuite name="Unit Tests">
<directory>./</directory>
</testsuite>
</testsuites>
</phpunit>
Try to run PHPUnit with --debug to see whats going on.

phpUnderControl and PHPUnit always failing build with code 255

I have the following build.xml file setup in phpUnderControl.
<target name="phpunit">
<exec executable="phpunit" dir="${basedir}/httpdocs" failonerror="on">
<arg line="--log-junit ${basedir}/build/logs/phpunit.xml
--coverage-clover ${basedir}/build/logs/phpunit.coverage.xml
--coverage-html ${basedir}/build/coverage
--colors
${basedir}/httpdocs/qc/unit/CalculatorTest.php" />
</exec>
</target>
For some unkown reason the build always fails with the below message.
phpunit:
[exec] PHPUnit 3.4.15 by Sebastian Bergmann.
[exec]
BUILD FAILED
/opt/cruisecontrol-bin-2.8.3/projects/citest.local/build.xml:30: exec returned: 255
I have run the very simple unit test manually within the unit directory and PHPUnit returns.
PHPUnit 3.4.15 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 5.25Mb
OK (1 test, 1 assertion)
Does anyone know why it keeps failing the build, when all the tests are fine?
My build script does have a clean method which removes and log files, so its not that. I have also manually removed the log files, just in case it's that script. And changed the owner of the log directories so they are writable.
If it makes any difference the phpunit.xml is empty after PHPUnit is run.
Thanks.
UPDATE: Incidentally if I remove failonerror="on" it works, obviously, but PHPUnit still returns 255 and I do want it to fail on any errors, the issue is there isn't any errors yet it still fails!
255 is the error code that PHP throws on fatal and (I think) parse errors.
Could there be a fatal or parse error in the script execution, maybe after the tests have run?
Any PHP errors? What happens if you add error_reporting(E_ALL); ?
Probably the 'exec returned: 255' is caused by the memory consumption of the CodeCoverage report, which will be displayed after enabling error_reporting(E_ALL) like Pekka mentioned. Try init_set('display_errors','on'); also. Usualy you will face a 'memory size ...exhaused' fatal.
In my case, it turned out to be that I didn't have a webserver installed. Installing nginx and setting it up as a webserver (even if I'm not using it) fixed the problem.
it might be caused by some empty testsuite in phpunit.xml, that is, no assertXXX in test code
I got Selenium, PHPUnit, PHPUnderControl, and CruiseControl all working together (here http://www.siteconsortium.com/h/p1.php?id=php002)
Just a very basic example but I was able to get it to work like this. Start from something simple and copy the other stuff back in.
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="build" basedir=".">
<target name="build">
<exec executable="C:\\PHP\\phpunit.bat" dir="C:\\workspace\\proj\\phpunit" failonerror="on">
<arg line="--log-junit C:\\workspace\\proj\\build\\logs\\phpunit.xml
--configuration C:\\workspace\\proj\\phpunit.xml
--include-path C:\\trunk\\includes C:\\workspace\\proj\\includes" />
</exec>
</target>
</project>

Resources