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

Fixing LinkTemplate and ModelProvider issues #19

Merged
merged 19 commits into from
Oct 21, 2021
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1",
"phpunit/phpunit": "^9.5.9",
"phpunit/phpunit": "^9.5.10",
"psalm/plugin-phpunit": "^0.16.1",
"squizlabs/php_codesniffer": "^3.6",
"squizlabs/php_codesniffer": "^3.6.1",
"vimeo/psalm": "^4.10"
},
"autoload": {
Expand Down
35 changes: 23 additions & 12 deletions src/Allure.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Qameta\Allure\Model\LinkType;
use Qameta\Allure\Model\Parameter;
use Qameta\Allure\Model\ParameterMode;
use Qameta\Allure\Model\ResultFactoryInterface;
use Qameta\Allure\Model\Severity;
use Qameta\Allure\Model\Status;
use Qameta\Allure\Model\StepResult;
Expand All @@ -28,7 +27,6 @@
use Qameta\Allure\Setup\LifecycleConfigInterface;
use Qameta\Allure\Setup\LifecycleConfiguratorInterface;
use Qameta\Allure\Setup\LifecycleFactoryInterface;
use Qameta\Allure\Setup\StatusDetectorInterface;
use ReflectionException;
use ReflectionFunction;
use ReflectionMethod;
Expand Down Expand Up @@ -72,14 +70,9 @@ public static function getLifecycleConfigurator(): LifecycleConfiguratorInterfac
return self::getInstance()->getLifecycleBuilder();
}

public static function getResultFactory(): ResultFactoryInterface
public static function getConfig(): LifecycleConfigInterface
{
return self::getInstance()->getLifecycleConfig()->getResultFactory();
}

public static function getStatusDetector(): StatusDetectorInterface
{
return self::getInstance()->getLifecycleConfig()->getStatusDetector();
return self::getInstance()->getLifecycleConfig();
}

/**
Expand Down Expand Up @@ -239,12 +232,30 @@ public static function parameter(

public static function issue(string $name, ?string $url = null): void
{
self::getInstance()->doLink(Link::issue($name, $url));
self::getInstance()->doLink(
Link::issue(
$name,
$url ?? self::getInstance()
->getLifecycleConfig()
->getLinkTemplates()
->get(LinkType::issue())
?->buildUrl($name),
),
);
}

public static function tms(string $name, ?string $url = null): void
{
self::getInstance()->doLink(Link::tms($name, $url));
self::getInstance()->doLink(
Link::tms(
$name,
$url ?? self::getInstance()
->getLifecycleConfig()
->getLinkTemplates()
->get(LinkType::tms())
?->buildUrl($name),
),
);
}

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

return new AttributeParser($attributes);
return new AttributeParser($attributes, $this->getLifecycleConfig()->getLinkTemplates());
}

private function doLabel(Label $label): void
Expand Down
38 changes: 13 additions & 25 deletions src/Attribute/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Qameta\Allure\Exception\InvalidMethodNameException;
use Qameta\Allure\Model;
use Qameta\Allure\Model\ModelProviderInterface;
use Qameta\Allure\Setup\LinkTemplateCollection;
use Qameta\Allure\Setup\LinkTemplateCollectionInterface;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
Expand All @@ -18,8 +20,9 @@
use function array_merge;
use function is_string;

class AttributeParser implements ModelProviderInterface
final class AttributeParser implements ModelProviderInterface
{
use Model\ModelProviderTrait;

private ?string $displayName = null;

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

/**
* @param array<AttributeInterface> $attributes
* @param array<string, LinkTemplateInterface> $linkTemplates
* @param array<AttributeInterface> $attributes
* @param LinkTemplateCollectionInterface $linkTemplates
*/
public function __construct(
array $attributes,
private array $linkTemplates = [],
private LinkTemplateCollectionInterface $linkTemplates,
) {
$this->processAnnotations(...$attributes);
}
Expand All @@ -57,15 +60,15 @@ public function __construct(
* @param class-string|object|null $classOrObject
* @param callable-string|Closure|null $methodOrFunction
* @param string|null $property
* @param array<string, LinkTemplateInterface> $linkTemplates
* @param LinkTemplateCollectionInterface|null $linkTemplates
* @return list<ModelProviderInterface>
* @throws ReflectionException
*/
public static function createForChain(
string|object|null $classOrObject,
string|Closure|null $methodOrFunction = null,
?string $property = null,
array $linkTemplates = [],
?LinkTemplateCollectionInterface $linkTemplates = null,
): array {
$reader = new LegacyAttributeReader(
new AnnotationReader(),
Expand Down Expand Up @@ -96,7 +99,7 @@ public static function createForChain(
return [
new self(
array_merge(...$annotations),
$linkTemplates,
$linkTemplates ?? new LinkTemplateCollection(),
)
];
}
Expand Down Expand Up @@ -128,22 +131,15 @@ private function processAnnotations(AttributeInterface ...$attributes): void

private function createLink(LinkInterface $link): Model\Link
{
$linkType = $link->getType();
$linkType = Model\LinkType::fromOptionalString($link->getType());

return new Model\Link(
name: $link->getName(),
url: $link->getUrl() ?? $this->getLinkUrl($link->getName(), $linkType),
type: Model\LinkType::fromOptionalString($linkType),
url: $link->getUrl() ?? $this->linkTemplates->get($linkType)?->buildUrl($link->getName()),
type: $linkType,
);
}

private function getLinkUrl(?string $name, ?string $type): ?string
{
return isset($type, $this->linkTemplates[$type])
? $this->linkTemplates[$type]->buildUrl($name)
: $name;
}

/**
* @return list<Model\Link>
*/
Expand Down Expand Up @@ -186,14 +182,6 @@ public function getParameters(): array
return $this->parameters;
}

/**
* @deprecated Please use {@see getDisplayName()} method.
*/
public function getTitle(): ?string
{
return $this->getDisplayName();
}

public function getDisplayName(): ?string
{
return $this->displayName;
Expand Down
11 changes: 0 additions & 11 deletions src/Attribute/LinkTemplateInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Attribute/ParameterMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Qameta\Allure\Model;

class ParameterMode
final class ParameterMode
{

public const HIDDEN = Model\ParameterMode::HIDDEN;
Expand Down
15 changes: 0 additions & 15 deletions src/Attribute/Title.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Internal/LifecycleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
use Qameta\Allure\Io\FileSystemResultsWriter;
use Qameta\Allure\Io\ResultsWriterInterface;
use Qameta\Allure\Io\SystemClock;
use Qameta\Allure\Model\LinkType;
use Qameta\Allure\Model\ResultFactory;
use Qameta\Allure\Model\ResultFactoryInterface;
use Qameta\Allure\Setup\DefaultStatusDetector;
use Qameta\Allure\Setup\LifecycleBuilderInterface;
use Qameta\Allure\Setup\LifecycleConfiguratorInterface;
use Qameta\Allure\Setup\LinkTemplateCollection;
use Qameta\Allure\Setup\LinkTemplateCollectionInterface;
use Qameta\Allure\Setup\LinkTemplateInterface;
use Qameta\Allure\Setup\StatusDetectorInterface;
use Ramsey\Uuid\UuidFactory;
use Ramsey\Uuid\UuidFactoryInterface;
Expand All @@ -39,6 +44,13 @@ final class LifecycleBuilder implements LifecycleBuilderInterface
*/
private array $lifecycleHooks = [];

/**
* @var array<string, LinkTemplateInterface>
*/
private array $linkTemplates = [];

private ?LinkTemplateCollectionInterface $linkTemplateCollection = null;

private ?StatusDetectorInterface $statusDetector = null;

public function createLifecycle(ResultsWriterInterface $resultsWriter): AllureLifecycleInterface
Expand Down Expand Up @@ -84,6 +96,18 @@ public function addHooks(
return $this;
}

public function addLinkTemplate(LinkType $type, LinkTemplateInterface $template): LifecycleConfiguratorInterface
{
$this->linkTemplates[(string) $type] = $template;

return $this;
}

public function getLinkTemplates(): LinkTemplateCollectionInterface
{
return $this->linkTemplateCollection ??= new LinkTemplateCollection($this->linkTemplates);
}

public function setStatusDetector(StatusDetectorInterface $statusDetector): self
{
$this->statusDetector = $statusDetector;
Expand Down
10 changes: 10 additions & 0 deletions src/Model/ModelProviderChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,14 @@ public function getDescriptionHtml(): ?string

return $descriptionHtml;
}

public function getFullName(): ?string
{
$fullName = null;
foreach ($this->providers as $provider) {
$fullName ??= $provider->getFullName();
}

return $fullName;
}
}
7 changes: 2 additions & 5 deletions src/Model/ModelProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ public function getLabels(): array;
*/
public function getParameters(): array;

/**
* @deprecated Please use {@see getDisplayName()} method.
*/
public function getTitle(): ?string;

public function getDisplayName(): ?string;

public function getDescription(): ?string;

public function getDescriptionHtml(): ?string;

public function getFullName(): ?string;
}
53 changes: 53 additions & 0 deletions src/Model/ModelProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Qameta\Allure\Model;

trait ModelProviderTrait
{

/**
* @return list<Link>
*/
public function getLinks(): array
{
return [];
}

/**
* @return list<Label>
*/
public function getLabels(): array
{
return [];
}

/**
* @return list<Parameter>
*/
public function getParameters(): array
{
return [];
}

public function getDisplayName(): ?string
{
return null;
}

public function getDescription(): ?string
{
return null;
}

public function getDescriptionHtml(): ?string
{
return null;
}

public function getFullName(): ?string
{
return null;
}
}
2 changes: 2 additions & 0 deletions src/Setup/LifecycleConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ interface LifecycleConfigInterface
public function getResultFactory(): ResultFactoryInterface;

public function getStatusDetector(): StatusDetectorInterface;

public function getLinkTemplates(): LinkTemplateCollectionInterface;
}
3 changes: 3 additions & 0 deletions src/Setup/LifecycleConfiguratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerInterface;
use Qameta\Allure\Hook\LifecycleHookInterface;
use Qameta\Allure\Io\ClockInterface;
use Qameta\Allure\Model\LinkType;
use Qameta\Allure\Model\ResultFactoryInterface;
use Ramsey\Uuid\UuidFactoryInterface;

Expand All @@ -27,4 +28,6 @@ public function addHooks(
LifecycleHookInterface $hook,
LifecycleHookInterface ...$moreHooks,
): self;

public function addLinkTemplate(LinkType $type, LinkTemplateInterface $template): self;
}
Loading