Skip to content

Commit e419d75

Browse files
authored
fix link template and model provider issues (fixes #16, fixes #18, via #19)
1 parent dd3ec99 commit e419d75

19 files changed

+264
-92
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
},
3636
"require-dev": {
3737
"jetbrains/phpstorm-attributes": "^1",
38-
"phpunit/phpunit": "^9.5.9",
38+
"phpunit/phpunit": "^9.5.10",
3939
"psalm/plugin-phpunit": "^0.16.1",
40-
"squizlabs/php_codesniffer": "^3.6",
40+
"squizlabs/php_codesniffer": "^3.6.1",
4141
"vimeo/psalm": "^4.10"
4242
},
4343
"autoload": {

src/Allure.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Qameta\Allure\Model\LinkType;
2020
use Qameta\Allure\Model\Parameter;
2121
use Qameta\Allure\Model\ParameterMode;
22-
use Qameta\Allure\Model\ResultFactoryInterface;
2322
use Qameta\Allure\Model\Severity;
2423
use Qameta\Allure\Model\Status;
2524
use Qameta\Allure\Model\StepResult;
@@ -28,7 +27,6 @@
2827
use Qameta\Allure\Setup\LifecycleConfigInterface;
2928
use Qameta\Allure\Setup\LifecycleConfiguratorInterface;
3029
use Qameta\Allure\Setup\LifecycleFactoryInterface;
31-
use Qameta\Allure\Setup\StatusDetectorInterface;
3230
use ReflectionException;
3331
use ReflectionFunction;
3432
use ReflectionMethod;
@@ -72,14 +70,9 @@ public static function getLifecycleConfigurator(): LifecycleConfiguratorInterfac
7270
return self::getInstance()->getLifecycleBuilder();
7371
}
7472

75-
public static function getResultFactory(): ResultFactoryInterface
73+
public static function getConfig(): LifecycleConfigInterface
7674
{
77-
return self::getInstance()->getLifecycleConfig()->getResultFactory();
78-
}
79-
80-
public static function getStatusDetector(): StatusDetectorInterface
81-
{
82-
return self::getInstance()->getLifecycleConfig()->getStatusDetector();
75+
return self::getInstance()->getLifecycleConfig();
8376
}
8477

8578
/**
@@ -239,12 +232,30 @@ public static function parameter(
239232

240233
public static function issue(string $name, ?string $url = null): void
241234
{
242-
self::getInstance()->doLink(Link::issue($name, $url));
235+
self::getInstance()->doLink(
236+
Link::issue(
237+
$name,
238+
$url ?? self::getInstance()
239+
->getLifecycleConfig()
240+
->getLinkTemplates()
241+
->get(LinkType::issue())
242+
?->buildUrl($name),
243+
),
244+
);
243245
}
244246

245247
public static function tms(string $name, ?string $url = null): void
246248
{
247-
self::getInstance()->doLink(Link::tms($name, $url));
249+
self::getInstance()->doLink(
250+
Link::tms(
251+
$name,
252+
$url ?? self::getInstance()
253+
->getLifecycleConfig()
254+
->getLinkTemplates()
255+
->get(LinkType::tms())
256+
?->buildUrl($name),
257+
),
258+
);
248259
}
249260

250261
public static function link(string $url, ?string $name = null, ?LinkType $type = null): void
@@ -414,7 +425,7 @@ private function readCallableAttributes(callable $callable): AttributeParser
414425
$attributes = $attributeReader->getFunctionAnnotations(new ReflectionFunction($callable));
415426
}
416427

417-
return new AttributeParser($attributes);
428+
return new AttributeParser($attributes, $this->getLifecycleConfig()->getLinkTemplates());
418429
}
419430

420431
private function doLabel(Label $label): void

src/Attribute/AttributeParser.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Qameta\Allure\Exception\InvalidMethodNameException;
1010
use Qameta\Allure\Model;
1111
use Qameta\Allure\Model\ModelProviderInterface;
12+
use Qameta\Allure\Setup\LinkTemplateCollection;
13+
use Qameta\Allure\Setup\LinkTemplateCollectionInterface;
1214
use ReflectionClass;
1315
use ReflectionException;
1416
use ReflectionFunction;
@@ -18,8 +20,9 @@
1820
use function array_merge;
1921
use function is_string;
2022

21-
class AttributeParser implements ModelProviderInterface
23+
final class AttributeParser implements ModelProviderInterface
2224
{
25+
use Model\ModelProviderTrait;
2326

2427
private ?string $displayName = null;
2528

@@ -43,12 +46,12 @@ class AttributeParser implements ModelProviderInterface
4346
private array $parameters = [];
4447

4548
/**
46-
* @param array<AttributeInterface> $attributes
47-
* @param array<string, LinkTemplateInterface> $linkTemplates
49+
* @param array<AttributeInterface> $attributes
50+
* @param LinkTemplateCollectionInterface $linkTemplates
4851
*/
4952
public function __construct(
5053
array $attributes,
51-
private array $linkTemplates = [],
54+
private LinkTemplateCollectionInterface $linkTemplates,
5255
) {
5356
$this->processAnnotations(...$attributes);
5457
}
@@ -57,15 +60,15 @@ public function __construct(
5760
* @param class-string|object|null $classOrObject
5861
* @param callable-string|Closure|null $methodOrFunction
5962
* @param string|null $property
60-
* @param array<string, LinkTemplateInterface> $linkTemplates
63+
* @param LinkTemplateCollectionInterface|null $linkTemplates
6164
* @return list<ModelProviderInterface>
6265
* @throws ReflectionException
6366
*/
6467
public static function createForChain(
6568
string|object|null $classOrObject,
6669
string|Closure|null $methodOrFunction = null,
6770
?string $property = null,
68-
array $linkTemplates = [],
71+
?LinkTemplateCollectionInterface $linkTemplates = null,
6972
): array {
7073
$reader = new LegacyAttributeReader(
7174
new AnnotationReader(),
@@ -96,7 +99,7 @@ public static function createForChain(
9699
return [
97100
new self(
98101
array_merge(...$annotations),
99-
$linkTemplates,
102+
$linkTemplates ?? new LinkTemplateCollection(),
100103
)
101104
];
102105
}
@@ -128,22 +131,15 @@ private function processAnnotations(AttributeInterface ...$attributes): void
128131

129132
private function createLink(LinkInterface $link): Model\Link
130133
{
131-
$linkType = $link->getType();
134+
$linkType = Model\LinkType::fromOptionalString($link->getType());
132135

133136
return new Model\Link(
134137
name: $link->getName(),
135-
url: $link->getUrl() ?? $this->getLinkUrl($link->getName(), $linkType),
136-
type: Model\LinkType::fromOptionalString($linkType),
138+
url: $link->getUrl() ?? $this->linkTemplates->get($linkType)?->buildUrl($link->getName()),
139+
type: $linkType,
137140
);
138141
}
139142

140-
private function getLinkUrl(?string $name, ?string $type): ?string
141-
{
142-
return isset($type, $this->linkTemplates[$type])
143-
? $this->linkTemplates[$type]->buildUrl($name)
144-
: $name;
145-
}
146-
147143
/**
148144
* @return list<Model\Link>
149145
*/
@@ -186,14 +182,6 @@ public function getParameters(): array
186182
return $this->parameters;
187183
}
188184

189-
/**
190-
* @deprecated Please use {@see getDisplayName()} method.
191-
*/
192-
public function getTitle(): ?string
193-
{
194-
return $this->getDisplayName();
195-
}
196-
197185
public function getDisplayName(): ?string
198186
{
199187
return $this->displayName;

src/Attribute/LinkTemplateInterface.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Attribute/ParameterMode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Qameta\Allure\Model;
88

9-
class ParameterMode
9+
final class ParameterMode
1010
{
1111

1212
public const HIDDEN = Model\ParameterMode::HIDDEN;

src/Attribute/Title.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Internal/LifecycleBuilder.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
use Qameta\Allure\Io\FileSystemResultsWriter;
1414
use Qameta\Allure\Io\ResultsWriterInterface;
1515
use Qameta\Allure\Io\SystemClock;
16+
use Qameta\Allure\Model\LinkType;
1617
use Qameta\Allure\Model\ResultFactory;
1718
use Qameta\Allure\Model\ResultFactoryInterface;
1819
use Qameta\Allure\Setup\DefaultStatusDetector;
1920
use Qameta\Allure\Setup\LifecycleBuilderInterface;
21+
use Qameta\Allure\Setup\LifecycleConfiguratorInterface;
22+
use Qameta\Allure\Setup\LinkTemplateCollection;
23+
use Qameta\Allure\Setup\LinkTemplateCollectionInterface;
24+
use Qameta\Allure\Setup\LinkTemplateInterface;
2025
use Qameta\Allure\Setup\StatusDetectorInterface;
2126
use Ramsey\Uuid\UuidFactory;
2227
use Ramsey\Uuid\UuidFactoryInterface;
@@ -39,6 +44,13 @@ final class LifecycleBuilder implements LifecycleBuilderInterface
3944
*/
4045
private array $lifecycleHooks = [];
4146

47+
/**
48+
* @var array<string, LinkTemplateInterface>
49+
*/
50+
private array $linkTemplates = [];
51+
52+
private ?LinkTemplateCollectionInterface $linkTemplateCollection = null;
53+
4254
private ?StatusDetectorInterface $statusDetector = null;
4355

4456
public function createLifecycle(ResultsWriterInterface $resultsWriter): AllureLifecycleInterface
@@ -84,6 +96,18 @@ public function addHooks(
8496
return $this;
8597
}
8698

99+
public function addLinkTemplate(LinkType $type, LinkTemplateInterface $template): LifecycleConfiguratorInterface
100+
{
101+
$this->linkTemplates[(string) $type] = $template;
102+
103+
return $this;
104+
}
105+
106+
public function getLinkTemplates(): LinkTemplateCollectionInterface
107+
{
108+
return $this->linkTemplateCollection ??= new LinkTemplateCollection($this->linkTemplates);
109+
}
110+
87111
public function setStatusDetector(StatusDetectorInterface $statusDetector): self
88112
{
89113
$this->statusDetector = $statusDetector;

src/Model/ModelProviderChain.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,14 @@ public function getDescriptionHtml(): ?string
100100

101101
return $descriptionHtml;
102102
}
103+
104+
public function getFullName(): ?string
105+
{
106+
$fullName = null;
107+
foreach ($this->providers as $provider) {
108+
$fullName ??= $provider->getFullName();
109+
}
110+
111+
return $fullName;
112+
}
103113
}

src/Model/ModelProviderInterface.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ public function getLabels(): array;
2222
*/
2323
public function getParameters(): array;
2424

25-
/**
26-
* @deprecated Please use {@see getDisplayName()} method.
27-
*/
28-
public function getTitle(): ?string;
29-
3025
public function getDisplayName(): ?string;
3126

3227
public function getDescription(): ?string;
3328

3429
public function getDescriptionHtml(): ?string;
30+
31+
public function getFullName(): ?string;
3532
}

src/Model/ModelProviderTrait.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Qameta\Allure\Model;
6+
7+
trait ModelProviderTrait
8+
{
9+
10+
/**
11+
* @return list<Link>
12+
*/
13+
public function getLinks(): array
14+
{
15+
return [];
16+
}
17+
18+
/**
19+
* @return list<Label>
20+
*/
21+
public function getLabels(): array
22+
{
23+
return [];
24+
}
25+
26+
/**
27+
* @return list<Parameter>
28+
*/
29+
public function getParameters(): array
30+
{
31+
return [];
32+
}
33+
34+
public function getDisplayName(): ?string
35+
{
36+
return null;
37+
}
38+
39+
public function getDescription(): ?string
40+
{
41+
return null;
42+
}
43+
44+
public function getDescriptionHtml(): ?string
45+
{
46+
return null;
47+
}
48+
49+
public function getFullName(): ?string
50+
{
51+
return null;
52+
}
53+
}

0 commit comments

Comments
 (0)