catching warnings, notices and deprecations with phpunit 9.4 on php 8.1.0 - phpunit

Quoting https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices , "By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception". Bearing that in mind, here's my unit test:
<?php
class DemoTest extends PHPUnit\Framework\TestCase
{
public function testDemo()
{
try {
trigger_error('zzz', E_USER_DEPRECATED);
} catch (\Throwable $e) {}
}
}
When I run vendor/bin/phpunit on that with PHPUnit 9.4.0 on PHP 8.0.9 I get the following (irrelevant parts of the output have been removed):
PHPUnit 9.4.0 by Sebastian Bergmann and contributors.
R 1 / 1 (100%)
When I run vendor/bin/phpunit on that on PHPUnit 9.4.0 on PHP 8.1.0 I get the following:
Deprecated: PHPUnit\Runner\DefaultTestResultCache implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in C:\path\to\code\vendor\phpunit\phpunit\src\Runner\DefaultTestResultCache.php on line 34
PHPUnit 9.4.0 by Sebastian Bergmann and contributors.
R 1 / 1 (100%)
ie. in PHP 8.1 it's like I'm getting a full dump of the Throwable, stack trace included, whereas I'm not getting that in PHP 8.0.
Any ideas? I don't want to see the error in the output and, if an exception isn't being thrown, then $this->expectException('PHPUnit\\Framework\\Error\\Deprecated') isn't gonna work for me either

This is an incompatibility of PHPUnit with PHP 8.1 and has already been fixed a while back in 9.5.5, so you need to update.
9.4 is not a supported version anymore.

Related

Phpunit error "This build of PHPDBG does not support code coverage"

I'm getting error "This build of PHPDBG does not support code coverage" when try to run phpunit with coverage with phpdbg. I found in PHAR line which generate this error:
if (!\function_exists('PHPUnit\\phpdbg_start_oplog')) {
throw new \PHPUnit\SebastianBergmann\CodeCoverage\RuntimeException('This build of PHPDBG does not support code coverage');
}
Then i run and get this result:
prompt> ev function_exists('phpdbg_start_oplog');
1
As i undestand, problem is in namespace before phpdbg_start_oplog. So, why phpdbg_start_oplog is prefixed with PHPUnit namespace in PHAR?
PHPUnit 9.1.4 by Sebastian Bergmann and contributors.
PHP 7.4.1 (cli) (built: Dec 28 2019 11:16:02) ( NTS )
You found a bug in PHPUnit (or PHP-Scoper, one of the tools involved in building the PHAR distribution of PHPUnit). I opened a ticket for this issue on GitHub.

Mac OS Mojave phpunit 8.0.1 symfony

After the installation of PHPunit 8.0.1 I got this error:
Fatal error: Declaration of Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::tearDown() must be compatible with PHPUnit\Framework\TestCase::tearDown()
I am using PHP 7.2
Your tearDown function is not compatible with the function you extend.
You have to add the return type in order to implement the same declaration.
protected function tearDown(): void
See also documentation https://phpunit.de/announcements/phpunit-8.html section "Return Type of Template Methods"

Issue in #Twig/Exception/exception_full.html.twig after updating symfony3 with Composer

I did the following :
1) I installed a fresh Symfony 3.2.2 version with this command line : php symfony.phar new Symfony3.2.2
2) I tested the version screen display with this url : http://localhost/Symfony3.2.2/web/app_dev.php/ and it well displays "Welcome to
Symfony 3.2.2 .... "
3) Then I did an update of symfony and all depencies with this command : php ../composer.phar update symfony/symfony --with-dependencies
4) I did not expected it updates anything (since I have installed latest version) but it seems it does, like "Updating twig/twig (v1.31.0 => v2.1.0)" and some few others libs (doctrine collections/annotations/common)
5) Then I retest http://localhost/Symfony3.2.2/web/app_dev.php/ but this times I got this error :
Twig_Error_Syntax in classes.php line 5280:
An exception has been thrown during the compilation of a template ("Catchable Fatal Error: Argument 1 passed to Twig_Filter::__construct() must be an instance of string, string given, called in C:\wamp\www\Symfony3.2.2\var\cache\dev\classes.php on line 5573 and defined") in "#Twig/Exception/exception_full.html.twig".
6) I have tried to clear cache with command : php bin/console cache:clear and then retest step 5) but I got same error
So all suggestions are welcome... thanks in advance for help
I have fixed by myself... it was a PHP issue... after the update, PHP 7 must be used instead PHP 5 (for some libs as Twig I presume like mentioned here )

Unable to run phpunit since upgrade to Symfony 2.8/3.0

Since upgrading to Symfony 2.8, I get the following error when I run phpunit in the Symfony root:
PHP Fatal error: Class 'PHPUnit_Util_ErrorHandler' not found in /var/www/foo/symfony/vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php on line 40
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_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:138
PHP 5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /usr/share/php/PHPUnit/TextUI/Command.php:657
PHP 6. PHPUnit_Util_Configuration->getTestSuite() /usr/share/php/PHPUnit/Util/Configuration.php:789
PHP 7. PHPUnit_Framework_TestSuite->addTestFiles() /usr/share/php/PHPUnit/Util/Configuration.php:873
PHP 8. PHPUnit_Framework_TestSuite->addTestFile() /usr/share/php/PHPUnit/Framework/TestSuite.php:416
PHP 9. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/Framework/TestSuite.php:355
PHP 10. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:76
PHP 11. Symfony\Bridge\PhpUnit\DeprecationErrorHandler::Symfony\Bridge\PhpUnit\{closure}() /usr/share/php/PHPUnit/Util/Fileloader.php:92
THE ERROR HANDLER HAS CHANGED!
This also occurs under Symfony 3.0. Should I now be running phpunit in a different manner?
I just ran into this problem using Symfony 2.8.
Just update your local install of phpunit.
Run this.
phpunit --self-update
That should do it.
I was running phpunit version 4.7.*, but Symfony wanted to see phpunit 5.1.3.
You should clear the app/cache/test directory.
You could also make your global phpunit symlink to the one provided by Symfony located in vendor/phpunit/phpunit/phpunit
Update: I'm not sure what this error is referring to, I still get it, but seems to occur when I have a problem with a test. At the least, it's a confusing message.

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

Resources