Skip to content

Commit 93fec0e

Browse files
authored
added attributes for standard labels and attribute sets support (fixes #33, via #58)
1 parent 387497c commit 93fec0e

39 files changed

+694
-72
lines changed

README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,56 @@ In order to use this API you simply need to add the following to **composer.json
1919
}
2020
```
2121

22-
## Usage examples
23-
See [allure-phpunit](https://github.com/allure-framework/allure-phpunit) project.
22+
## Custom attributes
23+
You can easily implement custom attributes and use them with your test framework. In most cases you would like
24+
to implement [`Qameta\Allure\Attribute\AttributeSetInterface`](./src/Attribute/AttributeSetInterface.php) that allows to set several attributes at once:
25+
26+
```php
27+
<?php
28+
29+
use Qameta\Allure\Attribute\AttributeSetInterface;
30+
use Qameta\Allure\Attribute\DisplayName;
31+
use Qameta\Allure\Attribute\Tag;
32+
33+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
34+
class MyAttribute implements AttributeSetInterface
35+
{
36+
private array $tags;
37+
38+
public function __construct(
39+
private string $displayName,
40+
string ...$tags,
41+
) {
42+
$this->tags = $tags;
43+
}
44+
45+
public function getAttributes() : array
46+
{
47+
return [
48+
new DisplayName($this->displayName),
49+
...array_map(
50+
fn (string $tag): Tag => new Tag($tag),
51+
$this->tags,
52+
),
53+
];
54+
}
55+
}
56+
57+
// Example of usage
58+
#[MyAttribute('Test name', 'tag 1', 'tag 2')]
59+
class MyTestClass
60+
{
61+
}
62+
```
63+
64+
You can also implement particular attribute interfaces instead of using one of the standard implementations:
65+
66+
- [`Qameta\Allure\Attribute\DescriptionInterface`](./src/Attribute/DescriptionInterface.php)
67+
- [`Qameta\Allure\Attribute\DisplayNameInterface`](./src/Attribute/DisplayNameInterface.php)
68+
- [`Qameta\Allure\Attribute\LabelInterface`](./src/Attribute/LabelInterface.php)
69+
- [`Qameta\Allure\Attribute\LinkInterface`](./src/Attribute/LinkInterface.php)
70+
- [`Qameta\Allure\Attribute\ParameterInterface`](./src/Attribute/ParameterInterface.php)
71+
72+
## Other usage examples
73+
See [allure-phpunit](https://github.com/allure-framework/allure-phpunit)
74+
and [allure-codeception](https://github.com/allure-framework/allure-codeception) projects.

src/Allure.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ public static function package(string $value): void
193193
self::getInstance()->doLabel(Label::package($value));
194194
}
195195

196+
public static function layer(string $value): void
197+
{
198+
self::getInstance()->doLabel(Label::layer($value));
199+
}
200+
196201
public static function label(string $name, string $value): void
197202
{
198203
self::getInstance()->doLabel(

src/Attribute/AttributeParser.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use ReflectionProperty;
1919

2020
use function array_merge;
21+
use function array_pop;
22+
use function array_push;
2123
use function array_reverse;
2224
use function is_string;
2325

@@ -107,7 +109,12 @@ public static function createForChain(
107109

108110
private function processAnnotations(AttributeInterface ...$attributes): void
109111
{
110-
foreach ($attributes as $attribute) {
112+
while (!empty($attributes)) {
113+
$attribute = array_shift($attributes);
114+
if ($attribute instanceof AttributeSetInterface) {
115+
array_unshift($attributes, ...$attribute->getAttributes());
116+
continue;
117+
}
111118
if ($attribute instanceof DisplayNameInterface) {
112119
$this->displayName = $attribute->getValue();
113120
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Qameta\Allure\Attribute;
6+
7+
interface AttributeSetInterface extends AttributeInterface
8+
{
9+
/**
10+
* @return list<AttributeInterface>
11+
*/
12+
public function getAttributes(): array;
13+
}

src/Attribute/Label.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ final class Label extends AbstractLabel
2121
public const TAG = Model\Label::TAG;
2222
public const OWNER = Model\Label::OWNER;
2323
public const LEAD = Model\Label::LEAD;
24+
public const PACKAGE = Model\Label::PACKAGE;
25+
public const LAYER = Model\Label::LAYER;
26+
27+
// Technical labels, set by framework automatically
2428
public const HOST = Model\Label::HOST;
2529
public const THREAD = Model\Label::THREAD;
2630
public const TEST_METHOD = Model\Label::TEST_METHOD;
2731
public const TEST_CLASS = Model\Label::TEST_CLASS;
28-
public const PACKAGE = Model\Label::PACKAGE;
2932
public const FRAMEWORK = Model\Label::FRAMEWORK;
3033
public const LANGUAGE = Model\Label::LANGUAGE;
3134
}

src/Attribute/Layer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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)]
10+
final class Layer extends AbstractLabel
11+
{
12+
public function __construct(string $value)
13+
{
14+
parent::__construct(Label::LAYER, $value);
15+
}
16+
}

src/Attribute/Lead.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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::IS_REPEATABLE)]
10+
final class Lead extends AbstractLabel
11+
{
12+
public function __construct(string $value)
13+
{
14+
parent::__construct(Label::LEAD, $value);
15+
}
16+
}

src/Attribute/Owner.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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::IS_REPEATABLE)]
10+
final class Owner extends AbstractLabel
11+
{
12+
public function __construct(string $value)
13+
{
14+
parent::__construct(Label::OWNER, $value);
15+
}
16+
}

src/Attribute/Package.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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)]
10+
final class Package extends AbstractLabel
11+
{
12+
public function __construct(string $value)
13+
{
14+
parent::__construct(Label::PACKAGE, $value);
15+
}
16+
}

src/Attribute/ParentSuite.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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::IS_REPEATABLE)]
10+
final class ParentSuite extends AbstractLabel
11+
{
12+
public function __construct(string $value)
13+
{
14+
parent::__construct(Label::PARENT_SUITE, $value);
15+
}
16+
}

0 commit comments

Comments
 (0)