PHPUnit codecoverage of my Symfony bundle does not work - symfony

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.

Related

Why phpunit is green when there are tons of exceptions?

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>

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.

PHPUnit throws error on mountain lion

I've followed the instructions exactly described as here
http://phpunit.de/getting-started.html
Everything went fine but when i execute the following command
phpunit --bootstrap src/autoload.php tests/MoneyTest
It throws following error
PHPUnit 4.0.4 by Sebastian Bergmann.
PHP Fatal error: Class 'SebastianBergmann\Money\Money' not found in /Users/myname/Sites/lab/unittesting/tests/MoneyTest.php on line 54
PHP Stack trace:
PHP 1. {main}() /usr/local/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:581
PHP 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:132
PHP 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:179
PHP 5. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:419
PHP 6. PHPUnit_Framework_TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
PHP 7. PHPUnit_Framework_TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:764
PHP 8. PHPUnit_Framework_TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:692
PHP 9. PHPUnit_Framework_TestCase->runTest() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:824
PHP 10. ReflectionMethod->invokeArgs() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:956
PHP 11. SebastianBergmann\Money\MoneyTest->testExceptionIsRaisedForInvalidConstructorArguments() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:956
Any idea what might be the cause?
Thanks
The following instructions worked for me:
$ git clone https://github.com/sebastianbergmann/money
$ cd money
$ phpunit --bootstrap src/autoload.php tests/MoneyTest
PHPUnit 3.7.32 by Sebastian Bergmann.
......................
Time: 882 ms, Memory: 3.25Mb
OK (22 tests, 41 assertions)
Be aware that I using 3.7.32 and you are using 4.0.4. If its still not working then it could be a bug.

PhpUnit 3.6.10 + NetBeans 7.1: Good test throws exception

In cmdline everything is OK:
d:\xampp\htdocs\PhpProject1\Tests>phpunit TestStub.php
PHPUnit 3.6.10 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 2.75Mb
OK (1 test, 1 assertion)
Called in NetBeans7.1:
Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Could not find class "" in "D:\Program Files (x86)\NetBeans 7.1\php\phpunit\NetBeansSuite.php".' in D:\xampp\php\PEAR\PHPUnit\Util\Skeleton\Test.php:123
Stack trace:
0 D:\xampp\php\PEAR\PHPUnit\TextUI\Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('', 'D:\Program File...')
1 D:\xampp\php\PEAR\PHPUnit\TextUI\Command.php(130): PHPUnit_TextUI_Command->run(Array, true)
2 D:\xampp\php\phpunit(46): PHPUnit_TextUI_Command::main()
3 {main}
thrown in D:\xampp\php\PEAR\PHPUnit\Util\Skeleton\Test.php on line 123
I think this is a NetBeans issue.
If I run:
d:\Program Files (x86)\NetBeans 7.1\php\phpunit>phpunit NetBeansSuite.php run=d:\xampp\htdocs\PhpProject1\Tests\TestStub.php
PHPUnit 3.6.10 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 2.75Mb
OK (1 test, 1 assertion)
It's OK. But if I give the test directory, it fails.
d:\Program Files (x86)\NetBeans 7.1\php\phpunit>phpunit NetBeansSuite.php run=d:\xampp\htdocs\PhpProject1\Tests\
Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Could not find class "" in "D:\Program Files (x86)\NetBeans 7.1\php\phpunit\NetBea
nsSuite.php".' in D:\xampp\php\PEAR\PHPUnit\Util\Skeleton\Test.php:123
Stack trace:
0 D:\xampp\php\PEAR\PHPUnit\TextUI\Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('', 'D:\Program File...')
1 D:\xampp\php\PEAR\PHPUnit\TextUI\Command.php(130): PHPUnit_TextUI_Command->run(Array, true)
2 D:\xampp\php\phpunit(46): PHPUnit_TextUI_Command::main()
3 {main}
thrown in D:\xampp\php\PEAR\PHPUnit\Util\Skeleton\Test.php on line 123
Any temporary solution?
When running PHPUnit against a single file it will always open the file and look for a test case inside of it.
When running it against a directory (what netbeans does) it will only look into files that end in *Test.php (unless something else is specified in the phpunit.xml).
If it can't find any Tests it will try to look into the $TestSuiteName folder and {$TestSuiteName}.php files hence the error message:
Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Could not find class ""
as it is looking for a "not named" test suite in the netbeans phpunit harness. The error message is really not helping you out there :)
The general suggestion would be to create a phpunit.xml and point Netbeans to it.
On a more personal note: From the phpunit support in the major IDEs Netbeans does quite a lot of things right and works well for a lot of people so don't let this bump discourage you from using it from the IDE if thats what you want.

Resources