Skip to content

Commit

Permalink
Closes #5157
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 3, 2023
1 parent 844d066 commit c1ad138
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 10 deletions.
7 changes: 7 additions & 0 deletions ChangeLog-10.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes of the PHPUnit 10.0 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

## [10.0.1] - 2023-MM-DD

### Fixed

* [#5157](https://github.com/sebastianbergmann/phpunit/issues/5157): Test Runner tries to run hook methods for `TestSuite` objects that do not collect `TestCase` objects

## [10.0.0] - 2023-02-03

### Added
Expand Down Expand Up @@ -123,4 +129,5 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi
* The CLI test runner can no longer be extended through inheritance, the `PHPUnit\TextUI\Command` class has been removed
* PHP 7.3, PHP 7.4, and PHP 8.0 are no longer supported

[10.0.1]: https://github.com/sebastianbergmann/phpunit/compare/10.0.0...10.0
[10.0.0]: https://github.com/sebastianbergmann/phpunit/compare/9.6...10.0.0
3 changes: 1 addition & 2 deletions src/Event/Value/TestSuite/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
namespace PHPUnit\Event\TestSuite;

use function class_exists;
use function explode;
use PHPUnit\Event\Code\TestCollection;
use PHPUnit\Event\RuntimeException;
Expand Down Expand Up @@ -83,7 +82,7 @@ public static function fromTestSuite(FrameworkTestSuite $testSuite): self
// @codeCoverageIgnoreEnd
}

if (class_exists($testSuite->getName())) {
if ($testSuite->isForTestClass()) {
try {
$reflector = new ReflectionClass($testSuite->getName());

Expand Down
25 changes: 17 additions & 8 deletions src/Framework/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function implode;
use function is_callable;
use function is_file;
use function is_subclass_of;
use function sprintf;
use function str_ends_with;
use function str_starts_with;
Expand Down Expand Up @@ -334,20 +335,18 @@ public function run(): void
return;
}

/** @psalm-var class-string $className */
$className = $this->name;
$hookMethods = (new HookMethods)->hookMethods($className);

$emitter = Event\Facade::emitter();
$testSuiteValueObjectForEvents = Event\TestSuite\TestSuite::fromTestSuite($this);

$emitter->testSuiteStarted($testSuiteValueObjectForEvents);

$methodsCalledBeforeFirstTest = [];

if (class_exists($this->name, false)) {
if ($this->isForTestClass()) {
$beforeClassMethods = (new HookMethods)->hookMethods($this->name)['beforeClass'];

try {
foreach ($hookMethods['beforeClass'] as $beforeClassMethod) {
foreach ($beforeClassMethods as $beforeClassMethod) {
if ($this->methodDoesNotExistOrIsDeclaredInTestCase($beforeClassMethod)) {
continue;
}
Expand Down Expand Up @@ -409,8 +408,10 @@ public function run(): void

$methodsCalledAfterLastTest = [];

if (class_exists($this->name, false)) {
foreach ($hookMethods['afterClass'] as $afterClassMethod) {
if ($this->isForTestClass()) {
$afterClassMethods = (new HookMethods)->hookMethods($this->name)['afterClass'];

foreach ($afterClassMethods as $afterClassMethod) {
if ($this->methodDoesNotExistOrIsDeclaredInTestCase($afterClassMethod)) {
continue;
}
Expand Down Expand Up @@ -556,6 +557,14 @@ public function sortId(): string
return $this->getName() . '::class';
}

/**
* @psalm-assert-if-true class-string $this->name
*/
public function isForTestClass(): bool
{
return class_exists($this->name, false) && is_subclass_of($this->name, TestCase::class);
}

/**
* @throws \PHPUnit\Event\TestData\MoreThanOneDataSetFromDataProviderException
* @throws Exception
Expand Down
21 changes: 21 additions & 0 deletions tests/end-to-end/regression/5157.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5157
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__ . '/5157/';

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

Runtime: %s
Configuration: %s

. 1 / 1 (100%)

Time: %s, Memory: %s

OK (1 test, 1 assertion)
10 changes: 10 additions & 0 deletions tests/end-to-end/regression/5157/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
>
<testsuites>
<testsuite name="stdclass">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
25 changes: 25 additions & 0 deletions tests/end-to-end/regression/5157/tests/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;

final class Test extends TestCase
{
private static bool $variable = false;

public static function setUpBeforeClass(): void
{
self::$variable = true;
}

public function testOne(): void
{
$this->assertTrue(self::$variable);
}
}

0 comments on commit c1ad138

Please sign in to comment.