Skip to content

Commit

Permalink
Register deprecation triggers in process isolation
Browse files Browse the repository at this point in the history
When process isolation is used, the deprecation triggers are not registered on the ErrorHandler.
  • Loading branch information
ruudk authored and sebastianbergmann committed Oct 28, 2024
1 parent bc822c6 commit 563604b
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/Framework/TestRunner/templates/class.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types=1);
use PHPUnit\Event\Facade;
use PHPUnit\Runner\CodeCoverage;
use PHPUnit\Runner\ErrorHandler;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
use PHPUnit\TextUI\Configuration\CodeCoverageFilterRegistry;
use PHPUnit\TextUI\Configuration\PhpHandler;
Expand Down Expand Up @@ -41,11 +42,33 @@ function __phpunit_run_isolated_test()

require_once '{filename}';

$configuration = ConfigurationRegistry::get();

if ({collectCodeCoverageInformation}) {
CodeCoverage::instance()->init(ConfigurationRegistry::get(), CodeCoverageFilterRegistry::instance(), true);
CodeCoverage::instance()->init($configuration, CodeCoverageFilterRegistry::instance(), true);
CodeCoverage::instance()->ignoreLines({linesToBeIgnored});
}

$deprecationTriggers = [
'functions' => [],
'methods' => [],
];

foreach ($configuration->source()->deprecationTriggers()['functions'] as $function) {
$deprecationTriggers['functions'][] = $function;
}

foreach ($configuration->source()->deprecationTriggers()['methods'] as $method) {
[$className, $methodName] = explode('::', $method);
$deprecationTriggers['methods'][] = [
'className' => $className,
'methodName' => $methodName,
];
}

ErrorHandler::instance()->useDeprecationTriggers($deprecationTriggers);

$test = new {className}('{name}');

$test->setData('{dataName}', unserialize('{data}'));
Expand Down
25 changes: 24 additions & 1 deletion src/Framework/TestRunner/templates/method.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types=1);
use PHPUnit\Event\Facade;
use PHPUnit\Runner\CodeCoverage;
use PHPUnit\Runner\ErrorHandler;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
use PHPUnit\TextUI\Configuration\CodeCoverageFilterRegistry;
use PHPUnit\TextUI\Configuration\PhpHandler;
Expand Down Expand Up @@ -41,11 +42,33 @@ function __phpunit_run_isolated_test()

require_once '{filename}';

$configuration = ConfigurationRegistry::get();

if ({collectCodeCoverageInformation}) {
CodeCoverage::instance()->init(ConfigurationRegistry::get(), CodeCoverageFilterRegistry::instance(), true);
CodeCoverage::instance()->init($configuration, CodeCoverageFilterRegistry::instance(), true);
CodeCoverage::instance()->ignoreLines({linesToBeIgnored});
}

$deprecationTriggers = [
'functions' => [],
'methods' => [],
];

foreach ($configuration->source()->deprecationTriggers()['functions'] as $function) {
$deprecationTriggers['functions'][] = $function;
}

foreach ($configuration->source()->deprecationTriggers()['methods'] as $method) {
[$className, $methodName] = explode('::', $method);
$deprecationTriggers['methods'][] = [
'className' => $className,
'methodName' => $methodName,
];
}

ErrorHandler::instance()->useDeprecationTriggers($deprecationTriggers);

$test = new {className}('{methodName}');

$test->setData('{dataName}', unserialize('{data}'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../phpunit.xsd"
cacheResult="false"
processIsolation="true"
>
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source>
<deprecationTrigger>
<method>PHPUnit\TestFixture\DeprecationTrigger\Test::triggerDeprecation</method>
<function>PHPUnit\TestFixture\DeprecationTrigger\triggerDeprecation</function>
</deprecationTrigger>
</source>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\DeprecationTrigger;

use const E_USER_DEPRECATED;
use function trigger_error;
use PHPUnit\Framework\TestCase;

final class Test extends TestCase
{
public static function triggerDeprecation(): void
{
trigger_error('deprecation triggered by method', E_USER_DEPRECATED);
}

public function testDeprecation(): void
{
self::triggerDeprecation();
triggerDeprecation();

$this->assertTrue(true);
}
}

function triggerDeprecation(): void
{
trigger_error('deprecation triggered by function', E_USER_DEPRECATED);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Configured deprecation triggers are filtered when displaying deprecation details in process isolation
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__ . '/_files/details-process-isolation/phpunit.xml';
$_SERVER['argv'][] = '--display-deprecations';

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s
Configuration: %s

D 1 / 1 (100%)

Time: %s, Memory: %s

1 test triggered 2 deprecations:

1) %sTest.php:25
deprecation triggered by method

Triggered by:

* PHPUnit\TestFixture\DeprecationTrigger\Test::testDeprecation
%sTest.php:23

2) %sTest.php:26
deprecation triggered by function

Triggered by:

* PHPUnit\TestFixture\DeprecationTrigger\Test::testDeprecation
%sTest.php:23

OK, but there were issues!
Tests: 1, Assertions: 1, Deprecations: 2.

0 comments on commit 563604b

Please sign in to comment.