Skip to content

Commit a66c28d

Browse files
authored
rft labels from environment utility (fixes #59, via #60)
1 parent 93fec0e commit a66c28d

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

src/Attribute/AttributeReaderInterface.php

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

55
namespace Qameta\Allure\Attribute;
66

7+
use Qameta\Allure\Model\EnvProvider;
78
use ReflectionClass;
89
use ReflectionFunction;
910
use ReflectionMethod;
@@ -42,6 +43,7 @@ public function getFunctionAnnotations(ReflectionFunction $function, ?string $na
4243
/**
4344
* @param array $variables
4445
* @return list<AttributeInterface>
46+
* @deprecated Use {@see EnvProvider} instead.
4547
*/
4648
public function getEnvironmentAnnotations(array $variables): array;
4749
}

src/Model/EnvProvider.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Qameta\Allure\Model;
6+
7+
use function is_scalar;
8+
use function is_string;
9+
use function str_starts_with;
10+
use function strlen;
11+
use function strtolower;
12+
use function substr;
13+
14+
final class EnvProvider implements ModelProviderInterface
15+
{
16+
use ModelProviderTrait;
17+
18+
private const ENV_LABEL_PREFIX = 'ALLURE_LABEL_';
19+
20+
/**
21+
* @var array<string, string>
22+
*/
23+
private array $env;
24+
25+
public function __construct(array $env)
26+
{
27+
$this->env = $this->normalizeEnv($env);
28+
}
29+
30+
/**
31+
* @param array $env
32+
* @return array<string, string>
33+
*/
34+
private function normalizeEnv(array $env): array
35+
{
36+
$normalizedEnv = [];
37+
/** @psalm-var mixed $value */
38+
foreach ($env as $key => $value) {
39+
if (is_string($key) && is_scalar($value)) {
40+
$normalizedEnv[$key] = (string) $value;
41+
}
42+
}
43+
44+
return $normalizedEnv;
45+
}
46+
47+
/**
48+
* @return list<Label>
49+
*/
50+
public function getLabels(): array
51+
{
52+
$labels = [];
53+
$prefixLength = strlen(self::ENV_LABEL_PREFIX);
54+
foreach ($this->env as $key => $value) {
55+
$name = str_starts_with($key, self::ENV_LABEL_PREFIX)
56+
? substr($key, $prefixLength)
57+
: '';
58+
if ('' != $name) {
59+
$labels[] = new Label(strtolower($name), $value);
60+
}
61+
}
62+
63+
return $labels;
64+
}
65+
}

test/Model/EnvProviderTest.php

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Qameta\Allure\Test\Model;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Qameta\Allure\Model\EnvProvider;
9+
10+
use function json_encode;
11+
12+
/**
13+
* @covers \Qameta\Allure\Model\EnvProvider
14+
*/
15+
class EnvProviderTest extends TestCase
16+
{
17+
/**
18+
* @param array $env
19+
* @return void
20+
* @dataProvider providerInvalidLabels
21+
*/
22+
public function testInvalidLabels(array $env): void
23+
{
24+
$provider = new EnvProvider($env);
25+
self::assertEmpty($provider->getLabels());
26+
}
27+
28+
/**
29+
* @return iterable<string, array{array}>
30+
*/
31+
public static function providerInvalidLabels(): iterable
32+
{
33+
return [
34+
'Absent' => [[]],
35+
'Empty name' => [['' => 'a']],
36+
'Nothing after prefix' => [['ALLURE_LABEL_' => 'a']],
37+
'Text before prefix' => [['AALLURE_LABEL_B' => 'c']],
38+
'Prefix with wrong case' => [['Allure_Label_A' => 'b']],
39+
'Non-scalar value' => [['ALLURE_LABEL_A' => []]],
40+
'Null value' => [['ALLURE_LABEL_A' => null]],
41+
];
42+
}
43+
44+
/**
45+
* @param array $env
46+
* @param string $expectedValue
47+
* @return void
48+
* @dataProvider providerValidLabels
49+
*/
50+
public function testValidLabels(array $env, string $expectedValue): void
51+
{
52+
$provider = new EnvProvider($env);
53+
$actualValue = json_encode($provider->getLabels());
54+
self::assertJsonStringEqualsJsonString($expectedValue, $actualValue);
55+
}
56+
57+
/**
58+
* @return iterable<string, array{array, string}>
59+
*/
60+
public static function providerValidLabels(): iterable
61+
{
62+
return [
63+
'Single label' => [
64+
['ALLURE_LABEL_A' => 'b'],
65+
<<<JSON
66+
[
67+
{"name": "a", "value": "b"}
68+
]
69+
JSON,
70+
],
71+
'Two labels' => [
72+
['ALLURE_LABEL_A' => 'b', 'ALLURE_LABEL_C' => 'd'],
73+
<<<JSON
74+
[
75+
{"name": "a", "value": "b"},
76+
{"name": "c", "value": "d"}
77+
]
78+
JSON,
79+
],
80+
];
81+
}
82+
}

0 commit comments

Comments
 (0)