|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Qameta\Allure\Test\Attribute; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Qameta\Allure\Attribute\AttributeInterface; |
| 9 | +use Qameta\Allure\Attribute\AttributeReader; |
| 10 | +use Qameta\Allure\Attribute\Label; |
| 11 | +use Qameta\Allure\Attribute\LabelInterface; |
| 12 | + |
| 13 | +use function array_map; |
| 14 | +use function array_values; |
| 15 | + |
| 16 | +/** |
| 17 | + * @covers \Qameta\Allure\Attribute\AttributeReader |
| 18 | + */ |
| 19 | +class AttributeReaderTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @param string $envLabelPrefix |
| 23 | + * @param array $variables |
| 24 | + * @return void |
| 25 | + * @dataProvider providerVariablesWithoutLabels |
| 26 | + */ |
| 27 | + public function testGetEnvironmentAnnotations_VariablesWithoutLabels_ReturnsEmptyList( |
| 28 | + string $envLabelPrefix, |
| 29 | + array $variables, |
| 30 | + ): void { |
| 31 | + $reader = new AttributeReader($envLabelPrefix); |
| 32 | + $attributes = $reader->getEnvironmentAnnotations($variables); |
| 33 | + self::assertEmpty($attributes); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return iterable<string, array{string, array}> |
| 38 | + */ |
| 39 | + public static function providerVariablesWithoutLabels(): iterable |
| 40 | + { |
| 41 | + return [ |
| 42 | + 'No variables' => ['a', []], |
| 43 | + 'Variables with no matching prefix' => ['a', ['b' => 'c', 'd' => 'e']], |
| 44 | + 'Variable with full name as prefix' => ['a', ['a' => 'b']], |
| 45 | + ]; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param string $envLabelPrefix |
| 50 | + * @param array $variables |
| 51 | + * @param list<array> $expectedData |
| 52 | + * @return void |
| 53 | + * @dataProvider providerVariablesWithLabels |
| 54 | + */ |
| 55 | + public function testGetEnvironment_VariablesWithLabels_ReturnMatchingLabels( |
| 56 | + string $envLabelPrefix, |
| 57 | + array $variables, |
| 58 | + array $expectedData, |
| 59 | + ): void { |
| 60 | + $reader = new AttributeReader($envLabelPrefix); |
| 61 | + $attributes = $reader->getEnvironmentAnnotations($variables); |
| 62 | + self::assertSame($expectedData, $this->exportAttributes(...$attributes)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return iterable<string, array{string, array, list<array>}> |
| 67 | + */ |
| 68 | + public static function providerVariablesWithLabels(): iterable |
| 69 | + { |
| 70 | + return [ |
| 71 | + 'Variable with double prefix' => [ |
| 72 | + 'a', |
| 73 | + ['aa' => 'b'], |
| 74 | + [['class' => Label::class, 'name' => 'a', 'value' => 'b']], |
| 75 | + ], |
| 76 | + 'Variable with non-string name' => [ |
| 77 | + '1', |
| 78 | + [12 => 'a'], |
| 79 | + [['class' => Label::class, 'name' => '2', 'value' => 'a']], |
| 80 | + ], |
| 81 | + 'Variable with non-string value' => [ |
| 82 | + 'a', |
| 83 | + ['ab' => 1], |
| 84 | + [['class' => Label::class, 'name' => 'b', 'value' => '1']], |
| 85 | + ], |
| 86 | + 'Variable with uppercase name' => [ |
| 87 | + 'a', |
| 88 | + ['aB' => 1], |
| 89 | + [['class' => Label::class, 'name' => 'B', 'value' => '1']], |
| 90 | + ], |
| 91 | + 'First variable with matching prefix' => [ |
| 92 | + 'a', |
| 93 | + ['ab' => 'c', 'd' => 'e'], |
| 94 | + [['class' => Label::class, 'name' => 'b', 'value' => 'c']], |
| 95 | + ], |
| 96 | + 'Second variable with matching prefix' => [ |
| 97 | + 'a', |
| 98 | + ['b' => 'c', 'ad' => 'e'], |
| 99 | + [['class' => Label::class, 'name' => 'd', 'value' => 'e']], |
| 100 | + ], |
| 101 | + 'Two variables with matching prefix' => [ |
| 102 | + 'a', |
| 103 | + ['ab' => 'c', 'ad' => 'e'], |
| 104 | + [ |
| 105 | + ['class' => Label::class, 'name' => 'b', 'value' => 'c'], |
| 106 | + ['class' => Label::class, 'name' => 'd', 'value' => 'e'], |
| 107 | + ], |
| 108 | + ], |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param AttributeInterface ...$attributes |
| 114 | + * @return list<array> |
| 115 | + */ |
| 116 | + private function exportAttributes(AttributeInterface ...$attributes): array |
| 117 | + { |
| 118 | + return array_map([$this, 'exportAttribute'], array_values($attributes)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param AttributeInterface $attribute |
| 123 | + * @return array<string, mixed> |
| 124 | + */ |
| 125 | + private function exportAttribute(AttributeInterface $attribute): array |
| 126 | + { |
| 127 | + $data = ['class' => $attribute::class]; |
| 128 | + if ($attribute instanceof LabelInterface) { |
| 129 | + $data['name'] = $attribute->getName(); |
| 130 | + $data['value'] = $attribute->getValue(); |
| 131 | + } |
| 132 | + |
| 133 | + return $data; |
| 134 | + } |
| 135 | +} |
0 commit comments