Skip to content

Commit

Permalink
rft labels and statuses api (fixes #11, fixes #12, fixes #13, via #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
remorhaz authored Oct 15, 2021
1 parent e6d2b76 commit 78abc21
Show file tree
Hide file tree
Showing 20 changed files with 745 additions and 127 deletions.
23 changes: 10 additions & 13 deletions src/Allure.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,6 @@ public static function package(string $value): void
self::getInstance()->doLabel(Label::package($value));
}

public static function framework(string $value): void
{
self::getInstance()->doLabel(Label::framework($value));
}

public static function language(string $value): void
{
self::getInstance()->doLabel(Label::language($value));
}

public static function label(string $name, string $value): void
{
self::getInstance()->doLabel(
Expand Down Expand Up @@ -247,12 +237,12 @@ public static function parameter(
);
}

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

public static function tms(string $name, string $url): void
public static function tms(string $name, ?string $url = null): void
{
self::getInstance()->doLink(Link::tms($name, $url));
}
Expand All @@ -268,6 +258,13 @@ public static function link(string $url, ?string $name = null, ?LinkType $type =
);
}

public static function displayName(string $name): void
{
self::getLifecycle()->updateExecutionContext(
fn (ExecutionContextInterface $context) => $context->setName($name),
);
}

public static function description(string $description): void
{
self::getLifecycle()->updateExecutionContext(
Expand Down Expand Up @@ -376,7 +373,7 @@ private function doRunStep(callable $callable, ?string $name = null): mixed
$parser = $this->readCallableAttributes($callable);
$this->doGetLifecycle()->updateStep(
fn (StepResult $s): StepResult => $s
->setName($name ?? $parser->getTitle() ?? $this->defaultStepName)
->setName($name ?? $parser->getDisplayName() ?? $this->defaultStepName)
->addParameters(...$parser->getParameters()),
$step->getUuid(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Qameta\Allure\Attribute;

abstract class AbstractTitle implements TitleInterface
abstract class AbstractDisplayName implements DisplayNameInterface
{
public function __construct(private string $value)
{
Expand Down
78 changes: 73 additions & 5 deletions src/Attribute/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@

namespace Qameta\Allure\Attribute;

use Closure;
use Doctrine\Common\Annotations\AnnotationReader;
use Qameta\Allure\Exception\InvalidMethodNameException;
use Qameta\Allure\Model;
use Qameta\Allure\Model\ModelProviderInterface;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionProperty;

class AttributeParser
use function array_merge;
use function is_string;

class AttributeParser implements ModelProviderInterface
{

private ?string $title = null;
private ?string $displayName = null;

private ?string $description = null;

Expand Down Expand Up @@ -41,11 +53,59 @@ public function __construct(
$this->processAnnotations(...$attributes);
}

/**
* @param class-string|object|null $classOrObject
* @param callable-string|Closure|null $methodOrFunction
* @param string|null $property
* @param array<string, LinkTemplateInterface> $linkTemplates
* @return list<ModelProviderInterface>
* @throws ReflectionException
*/
public static function createForChain(
string|object|null $classOrObject,
string|Closure|null $methodOrFunction = null,
?string $property = null,
array $linkTemplates = [],
): array {
$reader = new LegacyAttributeReader(
new AnnotationReader(),
new AttributeReader(),
);
$annotations = [];

if (isset($classOrObject)) {
$annotations[] = $reader->getClassAnnotations(new ReflectionClass($classOrObject));
if (isset($property)) {
$annotations[] = $reader->getPropertyAnnotations(new ReflectionProperty($classOrObject, $property));
}
}

if (isset($methodOrFunction)) {
$annotations[] = isset($classOrObject)
? $reader->getMethodAnnotations(
new ReflectionMethod(
$classOrObject,
is_string($methodOrFunction)
? $methodOrFunction
: throw new InvalidMethodNameException($methodOrFunction),
),
)
: $reader->getFunctionAnnotations(new ReflectionFunction($methodOrFunction));
}

return [
new self(
array_merge(...$annotations),
$linkTemplates,
)
];
}

private function processAnnotations(AttributeInterface ...$attributes): void
{
foreach ($attributes as $attribute) {
if ($attribute instanceof TitleInterface) {
$this->title = $attribute->getValue();
if ($attribute instanceof DisplayNameInterface) {
$this->displayName = $attribute->getValue();
}
if ($attribute instanceof DescriptionInterface) {
if ($attribute->isHtml()) {
Expand Down Expand Up @@ -126,9 +186,17 @@ public function getParameters(): array
return $this->parameters;
}

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

public function getDisplayName(): ?string
{
return $this->displayName;
}

public function getDescription(): ?string
Expand Down
12 changes: 12 additions & 0 deletions src/Attribute/DisplayName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Qameta\Allure\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)]
final class DisplayName extends AbstractDisplayName
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Qameta\Allure\Attribute;

interface TitleInterface extends AttributeInterface
interface DisplayNameInterface extends AttributeInterface
{

public function getValue(): string;
Expand Down
5 changes: 4 additions & 1 deletion src/Attribute/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Attribute;

/**
* @deprecated Please use {@see DisplayName} attribute instead.`
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)]
final class Title extends AbstractTitle
final class Title extends AbstractDisplayName
{
}
31 changes: 31 additions & 0 deletions src/Exception/InvalidMethodNameException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Qameta\Allure\Exception;

use Throwable;

use function gettype;
use function is_object;

final class InvalidMethodNameException extends \DomainException
{

public function __construct(
private mixed $methodName,
Throwable $previous = null,
) {
$methodDescription = gettype($this->methodName);
if (is_object($this->methodName)) {
$methodDescription .= '(' . $this->methodName::class . ')';
}

parent::__construct("Invalid method name: <$methodDescription>", 0, $previous);
}

public function getMethodName(): mixed
{
return $this->methodName;
}
}
8 changes: 4 additions & 4 deletions src/Legacy/Annotation/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace Yandex\Allure\Adapter\Annotation;

use Doctrine\Common\Annotations\Annotation\Required;
use Qameta\Allure\Attribute\Title as QametaTitle;
use Qameta\Allure\Attribute\DisplayName as QametaDisplayName;
use Qameta\Allure\Legacy\Annotation\LegacyAnnotationInterface;

/**
* @Annotation
* @Target({"CLASS", "METHOD"})
* @deprecated Use native PHP attribute {@see \Qameta\Allure\Attribute\Title}
* @deprecated Use native PHP attribute {@see \Qameta\Allure\Attribute\DisplayName}
* @psalm-suppress MissingConstructor
*/
class Title implements LegacyAnnotationInterface
Expand All @@ -21,8 +21,8 @@ class Title implements LegacyAnnotationInterface
*/
public string $value;

public function convert(): QametaTitle
public function convert(): QametaDisplayName
{
return new QametaTitle($this->value);
return new QametaDisplayName($this->value);
}
}
15 changes: 14 additions & 1 deletion src/Model/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use JsonSerializable;

use function preg_match;

use const PHP_VERSION;

final class Label implements JsonSerializable
{
use JsonSerializableTrait;
Expand Down Expand Up @@ -175,10 +179,19 @@ public static function language(?string $value): self
{
return new self(
name: self::LANGUAGE,
value: $value,
value: $value ?? self::buildPhpVersion(),
);
}

private static function buildPhpVersion(): string
{
$version = 1 === preg_match('#^\d+\.\d+#', PHP_VERSION, $matches)
? $matches[0]
: '?.?';

return "PHP $version";
}

public function getName(): ?string
{
return $this->name;
Expand Down
Loading

0 comments on commit 78abc21

Please sign in to comment.