phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

PHP Warnings throw exception despite phpunit.xml settings and \PHPUnit\Framework\Error\Warning::$enabled = false;

Open vedadkajtaz opened this issue 9 years ago • 7 comments

Q A
PHPUnit version 6.1.3 & 6.0
PHP version 7.1.3
Installation Method PHAR

Hello,

I have a test case where a legitimate PHP warning raises an exception (and thus fails the test) despite setting convertWarningsToExceptions="false" in phpunit.xml and despite the \PHPUnit\Framework\Error\Warning::$enabled = false; instruction in test' ::setUp() method.

Here's the phpunit.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
	colors="true"
	processIsolation="true"
	convertErrorsToExceptions="true"
	convertNoticesToExceptions="false"
	convertWarningsToExceptions="false"
>
	<testsuites>
		<testsuite name="Bugreport test">
			<directory suffix=".php">/XXX/bug_report/tests</directory>
		</testsuite>
	</testsuites>
</phpunit>

The /XXX/bug_report/tests folder holds the single test.php file:

<?php /* test.php */ /* UTF8-Côôkie */
declare(strict_types=1);


class	testClass extends \PHPUnit\Framework\TestCase
{
	protected function	setUp() : void
	{
		// Neither this nor setting 'convertWarningsToExceptions="false"' seem to fix the problem...
		\PHPUnit\Framework\Error\Warning::$enabled = false;
	}

	function	test_php_warning()
	{
		if(($socket = socket_create(AF_UNIX, SOCK_STREAM, 0)) === false)
			throw new Exception('Failed creating socket');

		if(!socket_connect($socket, '/tmp/nosuch'))
		{
			// Some fallback mechanism...
		}
	}
}

The test fails with the following output:

$ ../bin/phpunit-6.1.3.phar --configuration ./phpunit.xml PHPUnit 6.1.3 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)

Time: 559 ms, Memory: 6.00MB

There was 1 error:

  1. testClass::test_php_warning PHPUnit\Framework\Exception: Warning: socket_connect(): unable to connect [2]: No such file or directory in /XXX/bug_report/tests/test.php on line 18

Call Stack: 0.0012 503920 1. {main}() -:0 0.2260 3758656 2. __phpunit_run_isolated_test() -:399 0.2261 3743712 3. PHPUnit\Framework\TestCase->run() -:307 0.2264 3744200 4. PHPUnit\Framework\TestResult->run() phar:///XXX/bin/phpunit-6.1.3.phar/phpunit/Framework/TestCase.php:855 0.2265 3744976 5. PHPUnit\Framework\TestCase->runBare() phar:///XXX/bin/phpunit-6.1.3.phar/phpunit/Framework/TestResult.php:694 0.2299 3761896 6. PHPUnit\Framework\TestCase->runTest() phar:///XXX/bin/phpunit-6.1.3.phar/phpunit/Framework/TestCase.php:900 0.2299 3762192 7. ReflectionMethod->invokeArgs() phar:///XXX/bin/phpunit-6.1.3.phar/phpunit/Framework/TestCase.php:1041 0.2299 3762200 8. testClass->test_php_warning() phar:///XXX/bin/phpunit-6.1.3.phar/phpunit/Framework/TestCase.php:1041 0.2300 3762256 9. socket_connect() /XXX/bug_report/tests/test.php:18

ERRORS! Tests: 1, Assertions: 0, Errors: 1.

Thanks! Kind regards,

vedadkajtaz avatar May 09 '17 14:05 vedadkajtaz

I create pull-request #2806 with small fix. Now $enabled-Variables from class: PHPUnit\Framework\Error\Deprecated, PHPUnit\Framework\Error\Notice and PHPUnit\Framework\Error\Warning are set in src/Util/PHP/Template/TestCaseMethod.tpl.dist.

I do believe there is another bug, the error is still there.

wesolowski avatar Oct 14 '17 11:10 wesolowski

<?php
class Test extends PHPUnit\Framework\TestCase
{
    public function testOne()
    {
        $a = $b;
        
        $this->assertTrue(true);
    }
}

With the current state of #2806, I get the following:

convertNoticesToExceptions="false" processIsolation="false"

PHPUnit 6.4.1-24-ga3324d323 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.1.10 with Xdebug 2.5.5
Configuration: /home/sb/phpunit.xml

PHP Notice:  Undefined variable: b in /home/sb/Test.php on line 6
PHP Stack trace:
PHP   1. {main}() /usr/local/src/phpunit/phpunit:0
PHP   2. PHPUnit\TextUI\Command::main() /usr/local/src/phpunit/phpunit:53
PHP   3. PHPUnit\TextUI\Command->run() /usr/local/src/phpunit/src/TextUI/Command.php:148
PHP   4. PHPUnit\TextUI\TestRunner->doRun() /usr/local/src/phpunit/src/TextUI/Command.php:195
PHP   5. PHPUnit\Framework\TestSuite->run() /usr/local/src/phpunit/src/TextUI/TestRunner.php:537
PHP   6. Test->run() /usr/local/src/phpunit/src/Framework/TestSuite.php:743
PHP   7. PHPUnit\Framework\TestResult->run() /usr/local/src/phpunit/src/Framework/TestCase.php:894
PHP   8. Test->runBare() /usr/local/src/phpunit/src/Framework/TestResult.php:695
PHP   9. Test->runTest() /usr/local/src/phpunit/src/Framework/TestCase.php:939
PHP  10. ReflectionMethod->invokeArgs() /usr/local/src/phpunit/src/Framework/TestCase.php:1071
PHP  11. Test->testOne() /usr/local/src/phpunit/src/Framework/TestCase.php:1071
.                                                                   1 / 1 (100%)

Time: 34 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

convertNoticesToExceptions="false" processIsolation="true"

PHPUnit 6.4.1-24-ga3324d323 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.1.10 with Xdebug 2.5.5
Configuration: /home/sb/phpunit.xml

E                                                                   1 / 1 (100%)

Time: 87 ms, Memory: 4.00MB

There was 1 error:

1) Test::testOne
Undefined variable: b

/home/sb/Test.php:6

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

So, yes, it's not fixed yet. Need to investigate further.

sebastianbergmann avatar Oct 14 '17 13:10 sebastianbergmann

I can still reproduce it

ghost avatar Apr 06 '18 15:04 ghost

This issue has been automatically marked as stale because it has not had activity within the last 60 days. It will be closed after 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 05 '18 16:06 stale[bot]

This issue has been automatically closed because it has not had activity since it was marked as stale. Thank you for your contributions.

stale[bot] avatar Jun 12 '18 16:06 stale[bot]

Is there a solution to this issue. I having the same thing. Process isolation throws all warnings as errors even if convertWarningstoExceptions is false

Silent0bserv3r avatar Nov 23 '23 11:11 Silent0bserv3r

PHPUnit 6 is no longer supported. If you still experience this issue with PHPUnit 9 or PHPUnit then please open a new ticket.

sebastianbergmann avatar Nov 23 '23 11:11 sebastianbergmann