Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce TestRunAwareTestListener #3381

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/Framework/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,33 @@ public function addFailure(Test $test, AssertionFailedError $e, float $time): vo
$this->time += $time;
}

public function startTestRun(TestSuite $suite): void
{
$this->topTestSuite = $suite;

foreach ($this->listeners as $listener) {
if ($listener instanceof TestRunAwareTestListener) {
$listener->startTestRun($this->topTestSuite);
}
}
}

public function endTestRun(): void
{
$this->flushListeners();

foreach ($this->listeners as $listener) {
if ($listener instanceof TestRunAwareTestListener) {
$listener->endTestRun($this->topTestSuite, $this);
}
}
}

/**
* Informs the result that a test suite will be started.
*/
public function startTestSuite(TestSuite $suite): void
{
if ($this->topTestSuite === null) {
$this->topTestSuite = $suite;
}

foreach ($this->listeners as $listener) {
$listener->startTestSuite($suite);
}
Expand Down
26 changes: 26 additions & 0 deletions src/Framework/TestRunAwareTestListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/*
* 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.
*/
namespace PHPUnit\Framework;

/**
* A Listener for test progress.
*/
interface TestRunAwareTestListener extends TestListener
{
/**
* A test run started.
*/
public function startTestRun(TestSuite $suite): void;

/**
* A test run ended.
*/
public function endTestRun(TestSuite $suite, TestResult $result): void;
}
29 changes: 27 additions & 2 deletions src/Runner/Hook/TestListenerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestRunAwareTestListener;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning;
use PHPUnit\Util\Test as TestUtil;

final class TestListenerAdapter implements TestListener
final class TestListenerAdapter implements TestRunAwareTestListener
{
/**
* @var TestHook[]
Expand Down Expand Up @@ -130,4 +131,28 @@ public function startTestSuite(TestSuite $suite): void
public function endTestSuite(TestSuite $suite): void
{
}

/**
* A test run started.
*/
public function startTestRun(TestSuite $suite): void
{
foreach ($this->hooks as $hook) {
if ($hook instanceof BeforeFirstTestHook) {
$hook->executeBeforeFirstTest();
}
}
}

/**
* A test run ended.
*/
public function endTestRun(TestSuite $suite, TestResult $result): void
{
foreach ($this->hooks as $hook) {
if ($hook instanceof AfterLastTestHook) {
$hook->executeAfterLastTest();
}
}
}
}
27 changes: 20 additions & 7 deletions src/TextUI/ResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestRunAwareTestListener;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning;
use PHPUnit\Runner\PhptTestCase;
Expand All @@ -27,7 +27,7 @@
/**
* Prints the result of a TextUI TestRunner run.
*/
class ResultPrinter extends Printer implements TestListener
class ResultPrinter extends Printer implements TestRunAwareTestListener
{
public const EVENT_TEST_START = 0;

Expand Down Expand Up @@ -248,16 +248,29 @@ public function addSkippedTest(Test $test, \Throwable $t, float $time): void
$this->lastTestFailed = true;
}

/**
* A test run started.
*/
public function startTestRun(TestSuite $suite): void
{
$this->numTests = \count($suite);
$this->numTestsWidth = \strlen((string) $this->numTests);
$this->maxColumn = $this->numberOfColumns - \strlen(' / (XXX%)') - (2 * $this->numTestsWidth);
}

/**
* A test run ended.
*/
public function endTestRun(TestSuite $suite, TestResult $result): void
{
$this->printResult($result);
}

/**
* A testsuite started.
*/
public function startTestSuite(TestSuite $suite): void
{
if ($this->numTests == -1) {
$this->numTests = \count($suite);
$this->numTestsWidth = \strlen((string) $this->numTests);
$this->maxColumn = $this->numberOfColumns - \strlen(' / (XXX%)') - (2 * $this->numTestsWidth);
}
}

/**
Expand Down
21 changes: 4 additions & 17 deletions src/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te
$listenerNeeded = false;

foreach ($this->extensions as $extension) {
if ($extension instanceof TestHook) {
if ($extension instanceof TestHook || $extension instanceof BeforeFirstTestHook || $extension instanceof AfterLastTestHook) {
$listener->add($extension);

$listenerNeeded = true;
Expand Down Expand Up @@ -602,26 +602,13 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te
if ($suite instanceof TestSuite) {
$this->processSuiteFilters($suite, $arguments);
$suite->setRunTestInSeparateProcess($arguments['processIsolation']);
}

foreach ($this->extensions as $extension) {
if ($extension instanceof BeforeFirstTestHook) {
$extension->executeBeforeFirstTest();
}
$result->startTestRun($suite);
}

$suite->run($result);

foreach ($this->extensions as $extension) {
if ($extension instanceof AfterLastTestHook) {
$extension->executeAfterLastTest();
}
}

$result->flushListeners();

if ($this->printer instanceof ResultPrinter) {
$this->printer->printResult($result);
if ($suite instanceof TestSuite) {
$result->endTestRun();
}

if (isset($codeCoverage)) {
Expand Down
31 changes: 17 additions & 14 deletions src/Util/Log/TeamCity.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
*/
class TeamCity extends ResultPrinter
{
/**
* @var bool
*/
private $isSummaryTestCountPrinted = false;

/**
* @var string
*/
Expand Down Expand Up @@ -200,6 +195,23 @@ public function printIgnoredTest($testName, \Throwable $t, float $time): void
);
}

/**
* A test run started.
*/
public function startTestRun(TestSuite $suite): void
{
if (\stripos(\ini_get('disable_functions'), 'getmypid') === false) {
$this->flowId = \getmypid();
} else {
$this->flowId = false;
}

$this->printEvent(
'testCount',
['count' => \count($suite)]
);
}

/**
* A testsuite started.
*
Expand All @@ -213,15 +225,6 @@ public function startTestSuite(TestSuite $suite): void
$this->flowId = false;
}

if (!$this->isSummaryTestCountPrinted) {
$this->isSummaryTestCountPrinted = true;

$this->printEvent(
'testCount',
['count' => \count($suite)]
);
}

$suiteName = $suite->getName();

if (empty($suiteName)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/end-to-end/log-teamcity.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ PHPUnit %s by Sebastian Bergmann and contributors.
##teamcity[testSuiteFinished name='BankAccountTest' flowId='%d']


Time: %s, Memory: %s

OK (3 tests, 3 assertions)


Time: %s, Memory: %s

OK (3 tests, 3 assertions)