Skip to content

Commit

Permalink
labels are exposed in reverse order (fixes #34, via #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
remorhaz authored May 25, 2023
1 parent c2b222c commit 387497c
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 23 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1",
"phpunit/phpunit": "^9.5.10",
"phpunit/phpunit": "^9.6.8",
"psalm/plugin-phpunit": "^0.18.4",
"squizlabs/php_codesniffer": "^3.7.1",
"vimeo/psalm": "^5.4"
"squizlabs/php_codesniffer": "^3.7.2",
"vimeo/psalm": "^5.12"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
colors="true"
forceCoversAnnotation="true"
defaultTestSuite="unit">
<testsuites>
<testsuite name="unit">
Expand Down
3 changes: 2 additions & 1 deletion src/Attribute/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ReflectionProperty;

use function array_merge;
use function array_reverse;
use function is_string;

final class AttributeParser implements ModelProviderInterface
Expand Down Expand Up @@ -161,7 +162,7 @@ private function createLabel(LabelInterface $label): Model\Label
*/
public function getLabels(): array
{
return $this->labels;
return array_reverse($this->labels);
}

private function createParameter(ParameterInterface $parameter): Model\Parameter
Expand Down
11 changes: 7 additions & 4 deletions src/Model/ModelProviderChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use function array_map;
use function array_merge;
use function array_reverse;
use function array_values;

final class ModelProviderChain implements ModelProviderInterface
Expand Down Expand Up @@ -40,10 +41,12 @@ public function getLinks(): array
public function getLabels(): array
{
return array_merge(
...array_map(
/** @psalm-return list<Label> */
fn (ModelProviderInterface $p): array => $p->getLabels(),
$this->providers,
...array_reverse(
array_map(
/** @psalm-return list<Label> */
fn (ModelProviderInterface $p): array => $p->getLabels(),
$this->providers,
),
),
);
}
Expand Down
58 changes: 46 additions & 12 deletions test/AllureLifecycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,24 +868,47 @@ public function testWriteContainer_ExcludedContainerWithNestedResults_RemovesNes
);

$container->setExcluded(true);
$removeAttachmentResults = [];
$resultsWriter
->expects(self::exactly(6))
->method('removeAttachment')
->withConsecutive(
[self::identicalTo($setUpAttachment)],
[self::identicalTo($setUpStepAttachment)],
[self::identicalTo($testAttachment)],
[self::identicalTo($testStepAttachment)],
[self::identicalTo($tearDownAttachment)],
[self::identicalTo($tearDownStepAttachment)],
->with(
self::callback(
function (AttachmentResult $attachmentResult) use (&$removeAttachmentResults): bool {
/** @psalm-var list<AttachmentResult> $removeAttachmentResults */
$removeAttachmentResults[] = $attachmentResult;

return true;
},
),
);
$removeTestResults = [];
$resultsWriter
->expects(self::exactly(1))
->method('removeTest')
->withConsecutive(
[self::identicalTo($test)],
->with(
self::callback(
function (TestResult $testResult) use (&$removeTestResults): bool {
/** @psalm-var list<TestResult> $removeTestResults */
$removeTestResults[] = $testResult;

return true;
},
),
);
$lifecycle->writeContainer('a');
self::assertSame(
[
$setUpAttachment,
$setUpStepAttachment,
$testAttachment,
$testStepAttachment,
$tearDownAttachment,
$tearDownStepAttachment,
],
$removeAttachmentResults,
);
self::assertSame([$test], $removeTestResults);
}

public function testWriteContainer_WriterFailsToRemoveExcludedTest_LogsError(): void
Expand Down Expand Up @@ -2722,14 +2745,25 @@ public function testWriteTest_ExcludedTestWithNestedResults_RemovesNestedResults
);

$test->setExcluded(true);
$removeAttachmentResults = [];
$resultsWriter
->expects(self::exactly(2))
->method('removeAttachment')
->withConsecutive(
[self::identicalTo($testAttachment)],
[self::identicalTo($testStepAttachment)],
->with(
self::callback(
function (AttachmentResult $attachmentResult) use (&$removeAttachmentResults): bool {
/** @psalm-var list<AttachmentResult> $removeAttachmentResults */
$removeAttachmentResults[] = $attachmentResult;

return true;
},
),
);
$lifecycle->writeTest('a');
self::assertSame(
[$testAttachment, $testStepAttachment],
$removeAttachmentResults,
);
}

public function testWriteTest_WriterFailsToRemoveAttachment_LogsError(): void
Expand Down
211 changes: 211 additions & 0 deletions test/Attribute/AttributeParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

declare(strict_types=1);

namespace Qameta\Allure\Test\Attribute;

use PHPUnit\Framework\TestCase;
use Qameta\Allure\Attribute;
use Qameta\Allure\Attribute\AttributeParser;
use Qameta\Allure\Setup\LinkTemplateCollectionInterface;

use function json_encode;

/**
* @covers \Qameta\Allure\Attribute\AttributeParser
*/
class AttributeParserTest extends TestCase
{
public function testGetLinks_ConstructedWithoutLinkAttributes_ReturnsEmptyList(): void
{
$parser = new AttributeParser(
attributes: [
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertEmpty($parser->getLinks());
}

public function testGetLinks_ConstructedWithLinkAttributes_ReturnsMatchingLinkModels(): void
{
$parser = new AttributeParser(
attributes: [
new Attribute\Link('a', 'b', Attribute\Link::TMS),
new Attribute\Link('c', 'd', Attribute\Link::ISSUE),
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

$expectedValue = <<<EOF
[
{"name": "a", "url": "b", "type": "tms"},
{"name": "c", "url": "d", "type": "issue"}
]
EOF;
self::assertJsonStringEqualsJsonString(
$expectedValue,
json_encode($parser->getLinks()),
);
}

public function testGetLabels_ConstructedWithoutLabelAttributes_ReturnsEmptyList(): void
{
$parser = new AttributeParser(
attributes: [
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertEmpty($parser->getLabels());
}

public function testGetLabels_ConstructedWithLabelAttributes_ReturnsMatchingLabelModelsInReverseOrder(): void
{
$parser = new AttributeParser(
attributes: [
new Attribute\Label('a', 'b'),
new Attribute\Label('c', 'd'),
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

$expectedValue = <<<EOF
[
{"name": "c", "value": "d"},
{"name": "a", "value": "b"}
]
EOF;
self::assertJsonStringEqualsJsonString(
$expectedValue,
json_encode($parser->getLabels()),
);
}

public function testGetParameters_ConstructedWithoutParameterAttributes_ReturnsEmptyList(): void
{
$parser = new AttributeParser(
attributes: [
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertEmpty($parser->getParameters());
}

public function testGetParameters_ConstructedWithParameterAttributes_ReturnsMatchingParamsInReverseOrder(): void
{
$parser = new AttributeParser(
attributes: [
new Attribute\Parameter('a', 'b', mode: Attribute\ParameterMode::HIDDEN),
new Attribute\Parameter('c', 'd', excluded: true),
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

$expectedValue = <<<EOF
[
{"name": "a", "value": "b", "excluded": null, "mode": "hidden"},
{"name": "c", "value": "d", "excluded": true, "mode": null}
]
EOF;
self::assertJsonStringEqualsJsonString(
$expectedValue,
json_encode($parser->getParameters()),
);
}

public function testGetDisplayName_ConstructedWithoutDisplayNameAttribute_ReturnsNull(): void
{
$parser = new AttributeParser(
attributes: [
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertNull($parser->getDisplayName());
}

public function testGetDisplayName_ConstructedWithDisplayNameAttribute_ReturnsMatchingValue(): void
{
$parser = new AttributeParser(
attributes: [
new Attribute\DisplayName('a'),
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertSame('a', $parser->getDisplayName());
}


public function testGetDescription_ConstructedWithoutDescriptionAttribute_ReturnsNull(): void
{
$parser = new AttributeParser(
attributes: [
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertNull($parser->getDescription());
}

public function testGetDescription_ConstructedWithDescriptionAttribute_ReturnsMatchingValue(): void
{
$parser = new AttributeParser(
attributes: [
new Attribute\Description('a'),
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertSame('a', $parser->getDescription());
}

public function testGetDescription_ConstructedWithDescriptionHtmlAttribute_ReturnsNull(): void
{
$parser = new AttributeParser(
attributes: [
new Attribute\Description('a', isHtml: true),
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertNull($parser->getDescription());
}

public function testGetDescriptionHtml_ConstructedWithoutDescriptionAttribute_ReturnsNull(): void
{
$parser = new AttributeParser(
attributes: [
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertNull($parser->getDescriptionHtml());
}

public function testGetDescriptionHtml_ConstructedWithDescriptionHtmlAttribute_ReturnsMatchingValue(): void
{
$parser = new AttributeParser(
attributes: [
new Attribute\Description('a', isHtml: true),
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertSame('a', $parser->getDescriptionHtml());
}

public function testGetDescriptionHtml_ConstructedWithDescriptionAttribute_ReturnsNull(): void
{
$parser = new AttributeParser(
attributes: [
new Attribute\Description('a', isHtml: false),
],
linkTemplates: $this->createStub(LinkTemplateCollectionInterface::class),
);

self::assertNull($parser->getDescriptionHtml());
}
}
4 changes: 2 additions & 2 deletions test/Model/ModelProviderChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testGetLabels_OnlyFirstProviderReturnsLabels_ReturnsSameLabels()
self::assertSame([$labelA, $labelB], $chain->getLabels());
}

public function testGetLabels_BothProvidersReturnLabels_ReturnsMergedLabels(): void
public function testGetLabels_BothProvidersReturnLabels_ReturnsMergedLabelsInReverseOrder(): void
{
$firstProvider = $this->createStub(ModelProviderInterface::class);
$secondProvider = $this->createStub(ModelProviderInterface::class);
Expand All @@ -109,7 +109,7 @@ public function testGetLabels_BothProvidersReturnLabels_ReturnsMergedLabels(): v
->method('getLabels')
->willReturn([$labelB]);
$chain = new ModelProviderChain($firstProvider, $secondProvider);
self::assertSame([$labelA, $labelB], $chain->getLabels());
self::assertSame([$labelB, $labelA], $chain->getLabels());
}

public function testGetParameters_ConstructedWithoutProviders_ReturnsEmptyList(): void
Expand Down
3 changes: 3 additions & 0 deletions test/Model/TemporaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use RuntimeException;
use Throwable;

/**
* @coversNothing
*/
class TemporaryTest extends TestCase
{
public static function setUpBeforeClass(): void
Expand Down

0 comments on commit 387497c

Please sign in to comment.