Skip to content

Commit 78abc21

Browse files
authored
rft labels and statuses api (fixes #11, fixes #12, fixes #13, via #14)
1 parent e6d2b76 commit 78abc21

20 files changed

+745
-127
lines changed

src/Allure.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,6 @@ public static function package(string $value): void
209209
self::getInstance()->doLabel(Label::package($value));
210210
}
211211

212-
public static function framework(string $value): void
213-
{
214-
self::getInstance()->doLabel(Label::framework($value));
215-
}
216-
217-
public static function language(string $value): void
218-
{
219-
self::getInstance()->doLabel(Label::language($value));
220-
}
221-
222212
public static function label(string $name, string $value): void
223213
{
224214
self::getInstance()->doLabel(
@@ -247,12 +237,12 @@ public static function parameter(
247237
);
248238
}
249239

250-
public static function issue(string $name, string $url): void
240+
public static function issue(string $name, ?string $url = null): void
251241
{
252242
self::getInstance()->doLink(Link::issue($name, $url));
253243
}
254244

255-
public static function tms(string $name, string $url): void
245+
public static function tms(string $name, ?string $url = null): void
256246
{
257247
self::getInstance()->doLink(Link::tms($name, $url));
258248
}
@@ -268,6 +258,13 @@ public static function link(string $url, ?string $name = null, ?LinkType $type =
268258
);
269259
}
270260

261+
public static function displayName(string $name): void
262+
{
263+
self::getLifecycle()->updateExecutionContext(
264+
fn (ExecutionContextInterface $context) => $context->setName($name),
265+
);
266+
}
267+
271268
public static function description(string $description): void
272269
{
273270
self::getLifecycle()->updateExecutionContext(
@@ -376,7 +373,7 @@ private function doRunStep(callable $callable, ?string $name = null): mixed
376373
$parser = $this->readCallableAttributes($callable);
377374
$this->doGetLifecycle()->updateStep(
378375
fn (StepResult $s): StepResult => $s
379-
->setName($name ?? $parser->getTitle() ?? $this->defaultStepName)
376+
->setName($name ?? $parser->getDisplayName() ?? $this->defaultStepName)
380377
->addParameters(...$parser->getParameters()),
381378
$step->getUuid(),
382379
);

src/Attribute/AbstractTitle.php renamed to src/Attribute/AbstractDisplayName.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Qameta\Allure\Attribute;
66

7-
abstract class AbstractTitle implements TitleInterface
7+
abstract class AbstractDisplayName implements DisplayNameInterface
88
{
99
public function __construct(private string $value)
1010
{

src/Attribute/AttributeParser.php

+73-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,24 @@
44

55
namespace Qameta\Allure\Attribute;
66

7+
use Closure;
8+
use Doctrine\Common\Annotations\AnnotationReader;
9+
use Qameta\Allure\Exception\InvalidMethodNameException;
710
use Qameta\Allure\Model;
11+
use Qameta\Allure\Model\ModelProviderInterface;
12+
use ReflectionClass;
13+
use ReflectionException;
14+
use ReflectionFunction;
15+
use ReflectionMethod;
16+
use ReflectionProperty;
817

9-
class AttributeParser
18+
use function array_merge;
19+
use function is_string;
20+
21+
class AttributeParser implements ModelProviderInterface
1022
{
1123

12-
private ?string $title = null;
24+
private ?string $displayName = null;
1325

1426
private ?string $description = null;
1527

@@ -41,11 +53,59 @@ public function __construct(
4153
$this->processAnnotations(...$attributes);
4254
}
4355

56+
/**
57+
* @param class-string|object|null $classOrObject
58+
* @param callable-string|Closure|null $methodOrFunction
59+
* @param string|null $property
60+
* @param array<string, LinkTemplateInterface> $linkTemplates
61+
* @return list<ModelProviderInterface>
62+
* @throws ReflectionException
63+
*/
64+
public static function createForChain(
65+
string|object|null $classOrObject,
66+
string|Closure|null $methodOrFunction = null,
67+
?string $property = null,
68+
array $linkTemplates = [],
69+
): array {
70+
$reader = new LegacyAttributeReader(
71+
new AnnotationReader(),
72+
new AttributeReader(),
73+
);
74+
$annotations = [];
75+
76+
if (isset($classOrObject)) {
77+
$annotations[] = $reader->getClassAnnotations(new ReflectionClass($classOrObject));
78+
if (isset($property)) {
79+
$annotations[] = $reader->getPropertyAnnotations(new ReflectionProperty($classOrObject, $property));
80+
}
81+
}
82+
83+
if (isset($methodOrFunction)) {
84+
$annotations[] = isset($classOrObject)
85+
? $reader->getMethodAnnotations(
86+
new ReflectionMethod(
87+
$classOrObject,
88+
is_string($methodOrFunction)
89+
? $methodOrFunction
90+
: throw new InvalidMethodNameException($methodOrFunction),
91+
),
92+
)
93+
: $reader->getFunctionAnnotations(new ReflectionFunction($methodOrFunction));
94+
}
95+
96+
return [
97+
new self(
98+
array_merge(...$annotations),
99+
$linkTemplates,
100+
)
101+
];
102+
}
103+
44104
private function processAnnotations(AttributeInterface ...$attributes): void
45105
{
46106
foreach ($attributes as $attribute) {
47-
if ($attribute instanceof TitleInterface) {
48-
$this->title = $attribute->getValue();
107+
if ($attribute instanceof DisplayNameInterface) {
108+
$this->displayName = $attribute->getValue();
49109
}
50110
if ($attribute instanceof DescriptionInterface) {
51111
if ($attribute->isHtml()) {
@@ -126,9 +186,17 @@ public function getParameters(): array
126186
return $this->parameters;
127187
}
128188

189+
/**
190+
* @deprecated Please use {@see getDisplayName()} method.
191+
*/
129192
public function getTitle(): ?string
130193
{
131-
return $this->title;
194+
return $this->getDisplayName();
195+
}
196+
197+
public function getDisplayName(): ?string
198+
{
199+
return $this->displayName;
132200
}
133201

134202
public function getDescription(): ?string

src/Attribute/DisplayName.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Qameta\Allure\Attribute;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)]
10+
final class DisplayName extends AbstractDisplayName
11+
{
12+
}

src/Attribute/TitleInterface.php renamed to src/Attribute/DisplayNameInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Qameta\Allure\Attribute;
66

7-
interface TitleInterface extends AttributeInterface
7+
interface DisplayNameInterface extends AttributeInterface
88
{
99

1010
public function getValue(): string;

src/Attribute/Title.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
use Attribute;
88

9+
/**
10+
* @deprecated Please use {@see DisplayName} attribute instead.`
11+
*/
912
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)]
10-
final class Title extends AbstractTitle
13+
final class Title extends AbstractDisplayName
1114
{
1215
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Qameta\Allure\Exception;
6+
7+
use Throwable;
8+
9+
use function gettype;
10+
use function is_object;
11+
12+
final class InvalidMethodNameException extends \DomainException
13+
{
14+
15+
public function __construct(
16+
private mixed $methodName,
17+
Throwable $previous = null,
18+
) {
19+
$methodDescription = gettype($this->methodName);
20+
if (is_object($this->methodName)) {
21+
$methodDescription .= '(' . $this->methodName::class . ')';
22+
}
23+
24+
parent::__construct("Invalid method name: <$methodDescription>", 0, $previous);
25+
}
26+
27+
public function getMethodName(): mixed
28+
{
29+
return $this->methodName;
30+
}
31+
}

src/Legacy/Annotation/Title.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace Yandex\Allure\Adapter\Annotation;
66

77
use Doctrine\Common\Annotations\Annotation\Required;
8-
use Qameta\Allure\Attribute\Title as QametaTitle;
8+
use Qameta\Allure\Attribute\DisplayName as QametaDisplayName;
99
use Qameta\Allure\Legacy\Annotation\LegacyAnnotationInterface;
1010

1111
/**
1212
* @Annotation
1313
* @Target({"CLASS", "METHOD"})
14-
* @deprecated Use native PHP attribute {@see \Qameta\Allure\Attribute\Title}
14+
* @deprecated Use native PHP attribute {@see \Qameta\Allure\Attribute\DisplayName}
1515
* @psalm-suppress MissingConstructor
1616
*/
1717
class Title implements LegacyAnnotationInterface
@@ -21,8 +21,8 @@ class Title implements LegacyAnnotationInterface
2121
*/
2222
public string $value;
2323

24-
public function convert(): QametaTitle
24+
public function convert(): QametaDisplayName
2525
{
26-
return new QametaTitle($this->value);
26+
return new QametaDisplayName($this->value);
2727
}
2828
}

src/Model/Label.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
use JsonSerializable;
88

9+
use function preg_match;
10+
11+
use const PHP_VERSION;
12+
913
final class Label implements JsonSerializable
1014
{
1115
use JsonSerializableTrait;
@@ -175,10 +179,19 @@ public static function language(?string $value): self
175179
{
176180
return new self(
177181
name: self::LANGUAGE,
178-
value: $value,
182+
value: $value ?? self::buildPhpVersion(),
179183
);
180184
}
181185

186+
private static function buildPhpVersion(): string
187+
{
188+
$version = 1 === preg_match('#^\d+\.\d+#', PHP_VERSION, $matches)
189+
? $matches[0]
190+
: '?.?';
191+
192+
return "PHP $version";
193+
}
194+
182195
public function getName(): ?string
183196
{
184197
return $this->name;

0 commit comments

Comments
 (0)