Skip to content

Commit 387497c

Browse files
authored
labels are exposed in reverse order (fixes #34, via #57)
1 parent c2b222c commit 387497c

8 files changed

+276
-23
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
},
3636
"require-dev": {
3737
"jetbrains/phpstorm-attributes": "^1",
38-
"phpunit/phpunit": "^9.5.10",
38+
"phpunit/phpunit": "^9.6.8",
3939
"psalm/plugin-phpunit": "^0.18.4",
40-
"squizlabs/php_codesniffer": "^3.7.1",
41-
"vimeo/psalm": "^5.4"
40+
"squizlabs/php_codesniffer": "^3.7.2",
41+
"vimeo/psalm": "^5.12"
4242
},
4343
"autoload": {
4444
"psr-4": {

phpunit.xml.dist

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
55
colors="true"
6+
forceCoversAnnotation="true"
67
defaultTestSuite="unit">
78
<testsuites>
89
<testsuite name="unit">

src/Attribute/AttributeParser.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ReflectionProperty;
1919

2020
use function array_merge;
21+
use function array_reverse;
2122
use function is_string;
2223

2324
final class AttributeParser implements ModelProviderInterface
@@ -161,7 +162,7 @@ private function createLabel(LabelInterface $label): Model\Label
161162
*/
162163
public function getLabels(): array
163164
{
164-
return $this->labels;
165+
return array_reverse($this->labels);
165166
}
166167

167168
private function createParameter(ParameterInterface $parameter): Model\Parameter

src/Model/ModelProviderChain.php

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

77
use function array_map;
88
use function array_merge;
9+
use function array_reverse;
910
use function array_values;
1011

1112
final class ModelProviderChain implements ModelProviderInterface
@@ -40,10 +41,12 @@ public function getLinks(): array
4041
public function getLabels(): array
4142
{
4243
return array_merge(
43-
...array_map(
44-
/** @psalm-return list<Label> */
45-
fn (ModelProviderInterface $p): array => $p->getLabels(),
46-
$this->providers,
44+
...array_reverse(
45+
array_map(
46+
/** @psalm-return list<Label> */
47+
fn (ModelProviderInterface $p): array => $p->getLabels(),
48+
$this->providers,
49+
),
4750
),
4851
);
4952
}

test/AllureLifecycleTest.php

+46-12
Original file line numberDiff line numberDiff line change
@@ -868,24 +868,47 @@ public function testWriteContainer_ExcludedContainerWithNestedResults_RemovesNes
868868
);
869869

870870
$container->setExcluded(true);
871+
$removeAttachmentResults = [];
871872
$resultsWriter
872873
->expects(self::exactly(6))
873874
->method('removeAttachment')
874-
->withConsecutive(
875-
[self::identicalTo($setUpAttachment)],
876-
[self::identicalTo($setUpStepAttachment)],
877-
[self::identicalTo($testAttachment)],
878-
[self::identicalTo($testStepAttachment)],
879-
[self::identicalTo($tearDownAttachment)],
880-
[self::identicalTo($tearDownStepAttachment)],
875+
->with(
876+
self::callback(
877+
function (AttachmentResult $attachmentResult) use (&$removeAttachmentResults): bool {
878+
/** @psalm-var list<AttachmentResult> $removeAttachmentResults */
879+
$removeAttachmentResults[] = $attachmentResult;
880+
881+
return true;
882+
},
883+
),
881884
);
885+
$removeTestResults = [];
882886
$resultsWriter
883887
->expects(self::exactly(1))
884888
->method('removeTest')
885-
->withConsecutive(
886-
[self::identicalTo($test)],
889+
->with(
890+
self::callback(
891+
function (TestResult $testResult) use (&$removeTestResults): bool {
892+
/** @psalm-var list<TestResult> $removeTestResults */
893+
$removeTestResults[] = $testResult;
894+
895+
return true;
896+
},
897+
),
887898
);
888899
$lifecycle->writeContainer('a');
900+
self::assertSame(
901+
[
902+
$setUpAttachment,
903+
$setUpStepAttachment,
904+
$testAttachment,
905+
$testStepAttachment,
906+
$tearDownAttachment,
907+
$tearDownStepAttachment,
908+
],
909+
$removeAttachmentResults,
910+
);
911+
self::assertSame([$test], $removeTestResults);
889912
}
890913

891914
public function testWriteContainer_WriterFailsToRemoveExcludedTest_LogsError(): void
@@ -2722,14 +2745,25 @@ public function testWriteTest_ExcludedTestWithNestedResults_RemovesNestedResults
27222745
);
27232746

27242747
$test->setExcluded(true);
2748+
$removeAttachmentResults = [];
27252749
$resultsWriter
27262750
->expects(self::exactly(2))
27272751
->method('removeAttachment')
2728-
->withConsecutive(
2729-
[self::identicalTo($testAttachment)],
2730-
[self::identicalTo($testStepAttachment)],
2752+
->with(
2753+
self::callback(
2754+
function (AttachmentResult $attachmentResult) use (&$removeAttachmentResults): bool {
2755+
/** @psalm-var list<AttachmentResult> $removeAttachmentResults */
2756+
$removeAttachmentResults[] = $attachmentResult;
2757+
2758+
return true;
2759+
},
2760+
),
27312761
);
27322762
$lifecycle->writeTest('a');
2763+
self::assertSame(
2764+
[$testAttachment, $testStepAttachment],
2765+
$removeAttachmentResults,
2766+
);
27332767
}
27342768

27352769
public function testWriteTest_WriterFailsToRemoveAttachment_LogsError(): void
+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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;
9+
use Qameta\Allure\Attribute\AttributeParser;
10+
use Qameta\Allure\Setup\LinkTemplateCollectionInterface;
11+
12+
use function json_encode;
13+
14+
/**
15+
* @covers \Qameta\Allure\Attribute\AttributeParser
16+
*/
17+
class AttributeParserTest extends TestCase
18+
{
19+
public function testGetLinks_ConstructedWithoutLinkAttributes_ReturnsEmptyList(): void
20+
{
21+
$parser = new AttributeParser(
22+
attributes: [
23+
],
24+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
25+
);
26+
27+
self::assertEmpty($parser->getLinks());
28+
}
29+
30+
public function testGetLinks_ConstructedWithLinkAttributes_ReturnsMatchingLinkModels(): void
31+
{
32+
$parser = new AttributeParser(
33+
attributes: [
34+
new Attribute\Link('a', 'b', Attribute\Link::TMS),
35+
new Attribute\Link('c', 'd', Attribute\Link::ISSUE),
36+
],
37+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
38+
);
39+
40+
$expectedValue = <<<EOF
41+
[
42+
{"name": "a", "url": "b", "type": "tms"},
43+
{"name": "c", "url": "d", "type": "issue"}
44+
]
45+
EOF;
46+
self::assertJsonStringEqualsJsonString(
47+
$expectedValue,
48+
json_encode($parser->getLinks()),
49+
);
50+
}
51+
52+
public function testGetLabels_ConstructedWithoutLabelAttributes_ReturnsEmptyList(): void
53+
{
54+
$parser = new AttributeParser(
55+
attributes: [
56+
],
57+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
58+
);
59+
60+
self::assertEmpty($parser->getLabels());
61+
}
62+
63+
public function testGetLabels_ConstructedWithLabelAttributes_ReturnsMatchingLabelModelsInReverseOrder(): void
64+
{
65+
$parser = new AttributeParser(
66+
attributes: [
67+
new Attribute\Label('a', 'b'),
68+
new Attribute\Label('c', 'd'),
69+
],
70+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
71+
);
72+
73+
$expectedValue = <<<EOF
74+
[
75+
{"name": "c", "value": "d"},
76+
{"name": "a", "value": "b"}
77+
]
78+
EOF;
79+
self::assertJsonStringEqualsJsonString(
80+
$expectedValue,
81+
json_encode($parser->getLabels()),
82+
);
83+
}
84+
85+
public function testGetParameters_ConstructedWithoutParameterAttributes_ReturnsEmptyList(): void
86+
{
87+
$parser = new AttributeParser(
88+
attributes: [
89+
],
90+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
91+
);
92+
93+
self::assertEmpty($parser->getParameters());
94+
}
95+
96+
public function testGetParameters_ConstructedWithParameterAttributes_ReturnsMatchingParamsInReverseOrder(): void
97+
{
98+
$parser = new AttributeParser(
99+
attributes: [
100+
new Attribute\Parameter('a', 'b', mode: Attribute\ParameterMode::HIDDEN),
101+
new Attribute\Parameter('c', 'd', excluded: true),
102+
],
103+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
104+
);
105+
106+
$expectedValue = <<<EOF
107+
[
108+
{"name": "a", "value": "b", "excluded": null, "mode": "hidden"},
109+
{"name": "c", "value": "d", "excluded": true, "mode": null}
110+
]
111+
EOF;
112+
self::assertJsonStringEqualsJsonString(
113+
$expectedValue,
114+
json_encode($parser->getParameters()),
115+
);
116+
}
117+
118+
public function testGetDisplayName_ConstructedWithoutDisplayNameAttribute_ReturnsNull(): void
119+
{
120+
$parser = new AttributeParser(
121+
attributes: [
122+
],
123+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
124+
);
125+
126+
self::assertNull($parser->getDisplayName());
127+
}
128+
129+
public function testGetDisplayName_ConstructedWithDisplayNameAttribute_ReturnsMatchingValue(): void
130+
{
131+
$parser = new AttributeParser(
132+
attributes: [
133+
new Attribute\DisplayName('a'),
134+
],
135+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
136+
);
137+
138+
self::assertSame('a', $parser->getDisplayName());
139+
}
140+
141+
142+
public function testGetDescription_ConstructedWithoutDescriptionAttribute_ReturnsNull(): void
143+
{
144+
$parser = new AttributeParser(
145+
attributes: [
146+
],
147+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
148+
);
149+
150+
self::assertNull($parser->getDescription());
151+
}
152+
153+
public function testGetDescription_ConstructedWithDescriptionAttribute_ReturnsMatchingValue(): void
154+
{
155+
$parser = new AttributeParser(
156+
attributes: [
157+
new Attribute\Description('a'),
158+
],
159+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
160+
);
161+
162+
self::assertSame('a', $parser->getDescription());
163+
}
164+
165+
public function testGetDescription_ConstructedWithDescriptionHtmlAttribute_ReturnsNull(): void
166+
{
167+
$parser = new AttributeParser(
168+
attributes: [
169+
new Attribute\Description('a', isHtml: true),
170+
],
171+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
172+
);
173+
174+
self::assertNull($parser->getDescription());
175+
}
176+
177+
public function testGetDescriptionHtml_ConstructedWithoutDescriptionAttribute_ReturnsNull(): void
178+
{
179+
$parser = new AttributeParser(
180+
attributes: [
181+
],
182+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
183+
);
184+
185+
self::assertNull($parser->getDescriptionHtml());
186+
}
187+
188+
public function testGetDescriptionHtml_ConstructedWithDescriptionHtmlAttribute_ReturnsMatchingValue(): void
189+
{
190+
$parser = new AttributeParser(
191+
attributes: [
192+
new Attribute\Description('a', isHtml: true),
193+
],
194+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
195+
);
196+
197+
self::assertSame('a', $parser->getDescriptionHtml());
198+
}
199+
200+
public function testGetDescriptionHtml_ConstructedWithDescriptionAttribute_ReturnsNull(): void
201+
{
202+
$parser = new AttributeParser(
203+
attributes: [
204+
new Attribute\Description('a', isHtml: false),
205+
],
206+
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
207+
);
208+
209+
self::assertNull($parser->getDescriptionHtml());
210+
}
211+
}

test/Model/ModelProviderChainTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testGetLabels_OnlyFirstProviderReturnsLabels_ReturnsSameLabels()
9696
self::assertSame([$labelA, $labelB], $chain->getLabels());
9797
}
9898

99-
public function testGetLabels_BothProvidersReturnLabels_ReturnsMergedLabels(): void
99+
public function testGetLabels_BothProvidersReturnLabels_ReturnsMergedLabelsInReverseOrder(): void
100100
{
101101
$firstProvider = $this->createStub(ModelProviderInterface::class);
102102
$secondProvider = $this->createStub(ModelProviderInterface::class);
@@ -109,7 +109,7 @@ public function testGetLabels_BothProvidersReturnLabels_ReturnsMergedLabels(): v
109109
->method('getLabels')
110110
->willReturn([$labelB]);
111111
$chain = new ModelProviderChain($firstProvider, $secondProvider);
112-
self::assertSame([$labelA, $labelB], $chain->getLabels());
112+
self::assertSame([$labelB, $labelA], $chain->getLabels());
113113
}
114114

115115
public function testGetParameters_ConstructedWithoutProviders_ReturnsEmptyList(): void

test/Model/TemporaryTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use RuntimeException;
2222
use Throwable;
2323

24+
/**
25+
* @coversNothing
26+
*/
2427
class TemporaryTest extends TestCase
2528
{
2629
public static function setUpBeforeClass(): void

0 commit comments

Comments
 (0)