Skip to content
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
"allure-framework/allure-php-commons": "^2.0",
"allure-framework/allure-php-commons": "^2.4",
"phpunit/phpunit": "^10.0.5 || ^11 || ^12.0.1 || ^13"
},
"require-dev": {
Expand Down
5 changes: 2 additions & 3 deletions src/Event/TestConsideredRiskySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public function __construct(
public function notify(ConsideredRisky $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->updateStatus($event->message(), Status::failed());
}
}
5 changes: 2 additions & 3 deletions src/Event/TestErroredSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public function __construct(
public function notify(Errored $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->updateDetectedStatus($event->throwable()->message(), Status::broken());
}
}
5 changes: 2 additions & 3 deletions src/Event/TestFailedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public function __construct(
public function notify(Failed $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->updateDetectedStatus($event->throwable()->message(), Status::failed(), Status::failed());
}
}
5 changes: 2 additions & 3 deletions src/Event/TestFinishedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public function __construct(
public function notify(Finished $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->stop()
->updateRunInfo()
->write();
Expand Down
5 changes: 2 additions & 3 deletions src/Event/TestMarkedIncompleteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public function __construct(
public function notify(MarkedIncomplete $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->updateStatus($event->throwable()->message(), Status::broken());
}
}
5 changes: 2 additions & 3 deletions src/Event/TestPassedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public function __construct(
public function notify(Passed $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->updateStatus(status: Status::passed());
}
}
5 changes: 2 additions & 3 deletions src/Event/TestPreparationStartedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public function __construct(
public function notify(PreparationStarted $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->reset()
->create();
}
Expand Down
5 changes: 2 additions & 3 deletions src/Event/TestPreparedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public function __construct(
public function notify(Prepared $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->updateInfo()
->start();
}
Expand Down
5 changes: 2 additions & 3 deletions src/Event/TestSkippedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public function __construct(
public function notify(Skipped $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test->nameWithClass() : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method)
->switchTo($test)
->updateStatus($event->message(), Status::skipped());
}
}
5 changes: 2 additions & 3 deletions src/Event/TestWarningTriggeredSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public function __construct(
public function notify(WarningTriggered $event): void
{
$test = $event->test();
$method = $test instanceof TestMethod ? $test : null;
if (!isset($method)) {
if (!$test instanceof TestMethod) {
return;
}

$this
->testLifecycle
->switchTo($method->nameWithClass())
->switchTo($test)
->updateStatus($event->message(), Status::broken());
}
}
14 changes: 14 additions & 0 deletions src/Internal/TestInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Qameta\Allure\PHPUnit\Internal;

use function array_filter;
use function explode;
use function is_string;

/**
* @internal
*/
Expand Down Expand Up @@ -57,6 +61,16 @@ public function getFullName(): ?string
: null;
}

/**
* @return array<string>
*/
public function getTitlePath(): array
{
return is_string($this->class)
? [...array_filter(explode("\\", $this->class))]
: [];
}

public function getName(): string
{
return $this->getFullName() ?? $this->getTest();
Expand Down
46 changes: 14 additions & 32 deletions src/Internal/TestLifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Qameta\Allure\PHPUnit\Internal;

use PHPUnit\Event\Code\TestMethod;
use Qameta\Allure\AllureLifecycleInterface;
use Qameta\Allure\Model\ResultFactoryInterface;
use Qameta\Allure\Model\Status;
Expand All @@ -16,6 +17,7 @@
use function array_pad;
use function class_exists;
use function explode;
use function is_int;
use function preg_match;

/**
Expand Down Expand Up @@ -146,7 +148,7 @@ public function updateDetectedStatus(
}

#[\Override]
public function switchTo(string $test): self
public function switchTo(TestMethod $test): self
{
$thread = $this->threadDetector->getThread();
$this->lifecycle->switchThread($thread);
Expand All @@ -173,42 +175,22 @@ private function getCurrentTest(): TestInfo
return $this->currentTest ?? throw new RuntimeException("Current test is not set");
}

private function buildTestInfo(string $test, ?string $host = null, ?string $thread = null): TestInfo
private function buildTestInfo(TestMethod $test, ?string $host = null, ?string $thread = null): TestInfo
{
/** @var list<string> $matches */
$classAndMethodMatchResult = preg_match(
'#^(\S+)(.*)$#',
$test,
$matches,
);
[$classAndMethod, $dataSetInfo] = 1 === $classAndMethodMatchResult
? [$matches[1] ?? null, $matches[2] ?? null]
: [$test, null];
$dataLabelMatchResult = isset($dataSetInfo)
? preg_match(
'/^\s+with\s+data\s+set\s+(?:(#\d+)|"(.*)")$/',
$dataSetInfo,
$matches,
)
: false;
$dataLabel = 1 === $dataLabelMatchResult
? $matches[2] ?? $matches[1] ?? null
: null;
if ('' === $dataLabel) {
$dataLabel = null;
}
$className = $test->className();
$methodName = $test->methodName();

/** @psalm-suppress PossiblyUndefinedArrayOffset */
[$class, $method] = isset($classAndMethod)
? array_pad(explode('::', $classAndMethod, 2), 2, null)
: [null, null];
$testData = $test->testData();
$dataSetName = $testData->hasDataFromDataProvider()
? $testData->dataFromDataProvider()->dataSetName()
: null;

/** @psalm-suppress MixedArgument */
return new TestInfo(
test: $test,
class: isset($class) && class_exists($class) ? $class : null,
method: $method,
dataLabel: $dataLabel,
test: $test->nameWithClass(),
class: class_exists($className) ? $className : null,
method: $methodName,
dataLabel: is_int($dataSetName) ? "#" . $dataSetName : $dataSetName,
host: $host,
thread: $thread,
);
Expand Down
3 changes: 2 additions & 1 deletion src/Internal/TestLifecycleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Qameta\Allure\PHPUnit\Internal;

use PHPUnit\Event\Code\TestMethod;
use Qameta\Allure\Model\Status;

interface TestLifecycleInterface
Expand All @@ -28,7 +29,7 @@ public function updateDetectedStatus(
?Status $overrideStatus = null,
): TestLifecycleInterface;

public function switchTo(string $test): TestLifecycleInterface;
public function switchTo(TestMethod $test): TestLifecycleInterface;

public function reset(): TestLifecycleInterface;
}
1 change: 1 addition & 0 deletions src/Internal/TestUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function setInfo(TestResult $testResult, TestInfo $info): void
$testResult
->setName($parser->getDisplayName() ?? $info->getName())
->setFullName($info->getFullName())
->setTitlePath(...$info->getTitlePath())
->setDescriptionHtml($parser->getDescriptionHtml())
->setDescription($parser->getDescription())
->addLabels(
Expand Down
8 changes: 3 additions & 5 deletions test/unit/Event/TestConsideredRiskySubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ public function testNotify_ValidTestMethod_UpdatesStatusAsFailedForSwitchedConte
{
$testLifecycle = $this->createMock(TestLifecycleInterface::class);
$subscriber = new TestConsideredRiskySubscriber($testLifecycle);
$event = $this->createTestConsideredRiskyEvent(
test: $this->createTestMethod(class: 'a', methodName: 'b'),
message: 'c',
);
$test = $this->createTestMethod(class: 'a', methodName: 'b');
$event = $this->createTestConsideredRiskyEvent($test, 'c');

$switched = false;
$testLifecycle
->expects(self::once())
->method('switchTo')
->with(self::identicalTo('a::b'))
->with(self::identicalTo($test))
->willReturnCallback(
function () use (&$switched, $testLifecycle) {
$switched = true;
Expand Down
8 changes: 3 additions & 5 deletions test/unit/Event/TestErroredSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ public function testNotify_ValidTestMethod_UpdatesDetectedStatusAsBrokenForSwitc
{
$testLifecycle = $this->createMock(TestLifecycleInterface::class);
$subscriber = new TestErroredSubscriber($testLifecycle);
$event = $this->createTestErroredEvent(
test: $this->createTestMethod(class: 'a', methodName: 'b'),
message: 'c',
);
$test = $this->createTestMethod(class: 'a', methodName: 'b');
$event = $this->createTestErroredEvent($test, 'c');

$switched = false;
$testLifecycle
->expects(self::once())
->method('switchTo')
->with(self::identicalTo('a::b'))
->with(self::identicalTo($test))
->willReturnCallback(
function () use (&$switched, $testLifecycle) {
$switched = true;
Expand Down
8 changes: 3 additions & 5 deletions test/unit/Event/TestFailedSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ public function testNotify_ValidTestMethod_UpdatesDetectedStatusAsFailedForSwitc
{
$testLifecycle = $this->createMock(TestLifecycleInterface::class);
$subscriber = new TestFailedSubscriber($testLifecycle);
$event = $this->createTestFailedEvent(
test: $this->createTestMethod(class: 'a', methodName: 'b'),
message: 'c',
);
$test = $this->createTestMethod(class: 'a', methodName: 'b');
$event = $this->createTestFailedEvent($test, 'c');

$switched = false;
$testLifecycle
->expects(self::once())
->method('switchTo')
->with(self::identicalTo('a::b'))
->with(self::identicalTo($test))
->willReturnCallback(
function () use (&$switched, $testLifecycle) {
$switched = true;
Expand Down
7 changes: 3 additions & 4 deletions test/unit/Event/TestFinishedSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ public function testNotify_ValidTestMethod_WritesUpdatedStoppedTestAfterSwitchin
{
$testLifecycle = $this->createMock(TestLifecycleInterface::class);
$subscriber = new TestFinishedSubscriber($testLifecycle);
$event = $this->createTestFinishedEvent(
$this->createTestMethod(class: 'a', methodName: 'b'),
);
$test = $this->createTestMethod(class: 'a', methodName: 'b');
$event = $this->createTestFinishedEvent($test);

$lastMethod = null;
$testLifecycle
->expects(self::once())
->method('switchTo')
->with(self::identicalTo('a::b'))
->with(self::identicalTo($test))
->willReturnCallback(
function () use (&$lastMethod, $testLifecycle) {
$lastMethod = "switchTo";
Expand Down
Loading
Loading