Skip to content

Commit e6d2b76

Browse files
authored
add legacy traits (fixes #9, via #10)
1 parent b1ae884 commit e6d2b76

File tree

7 files changed

+486
-6
lines changed

7 files changed

+486
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![Version](http://poser.pugx.org/allure-framework/allure-php-commons/version)](https://packagist.org/packages/allure-framework/allure-php-commons)
44
[![Build](https://github.com/allure-framework/allure-php-commons2/actions/workflows/build.yml/badge.svg)](https://github.com/allure-framework/allure-php-commons/actions/workflows/build.yml)
5+
[![Type Coverage](https://shepherd.dev/github/allure-framework/allure-php-commons2/coverage.svg)](https://shepherd.dev/github/allure-framework/allure-php-commons2)
6+
[![Psalm Level](https://shepherd.dev/github/allure-framework/allure-php-commons2/level.svg)](https://shepherd.dev/github/allure-framework/allure-php-commons2)
57
[![License](http://poser.pugx.org/allure-framework/allure-php-commons/license)](https://packagist.org/packages/allure-framework/allure-php-commons)
68

79
This repository contains PHP API for Allure framework. The main idea is to reuse this API when creating adapters for different test frameworks.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"scripts": {
5555
"test-cs": "vendor/bin/phpcs -sp",
5656
"test-unit": "vendor/bin/phpunit --log-junit=build/log/junit.xml --coverage-clover=build/coverage/clover.xml --coverage-text",
57-
"test-psalm": "vendor/bin/psalm",
57+
"test-psalm": "vendor/bin/psalm --shepherd",
5858
"test": [
5959
"@test-cs",
6060
"@test-unit",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yandex\Allure\Adapter\Support;
6+
7+
use Qameta\Allure\Allure;
8+
9+
use function file_exists;
10+
use function is_file;
11+
12+
/**
13+
* @deprecated Please use {@see Allure::attachment()} and {@see Allure::attachmentFile()} methods directly instead
14+
* of this trait.
15+
*/
16+
trait AttachmentSupport
17+
{
18+
19+
/**
20+
* Adds a new attachment to report
21+
*
22+
* @param string $filePathOrContents either a string with file contents or file path to copy
23+
* @param string $caption
24+
* @param string|null $type
25+
*/
26+
public function addAttachment(string $filePathOrContents, string $caption, ?string $type = null): void
27+
{
28+
if (@file_exists($filePathOrContents) && is_file($filePathOrContents)) {
29+
Allure::attachmentFile($caption, $filePathOrContents, $type);
30+
} else {
31+
Allure::attachment($caption, $filePathOrContents, $type);
32+
}
33+
}
34+
}

src/Legacy/Support/StepSupport.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yandex\Allure\Adapter\Support;
6+
7+
use Qameta\Allure\Allure;
8+
use Qameta\Allure\StepContextInterface;
9+
use Throwable;
10+
11+
/**
12+
* @deprecated Please use {@see Allure::runStep()} method directly instead of this trait.
13+
*/
14+
trait StepSupport
15+
{
16+
17+
/**
18+
* Adds a simple step to current test case
19+
*
20+
* @param string $name step name
21+
* @param callable(StepContextInterface):mixed $logic anonymous function containing the entire step logic.
22+
* @param string|null $title an optional title for the step
23+
* @return mixed
24+
* @throws Throwable
25+
*/
26+
public function executeStep(string $name, callable $logic, ?string $title = null): mixed
27+
{
28+
return Allure::runStep($logic, $title ?? $name);
29+
}
30+
}

src/Model/Parameter.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,4 @@ public function setMode(?ParameterMode $mode): self
6565

6666
return $this;
6767
}
68-
69-
protected function excludeFromSerialization(): array
70-
{
71-
return ['excluded', 'mode'];
72-
}
7368
}
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Qameta\Allure\Test\Legacy\Support;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Qameta\Allure\Allure;
9+
use Qameta\Allure\AllureLifecycleInterface;
10+
use Qameta\Allure\Io\DataSourceInterface;
11+
use Qameta\Allure\Model\AttachmentResult;
12+
use Qameta\Allure\Model\ResultFactoryInterface;
13+
use Qameta\Allure\Setup\LifecycleBuilderInterface;
14+
use Yandex\Allure\Adapter\Support\AttachmentSupport;
15+
16+
use function fclose;
17+
use function fread;
18+
19+
/**
20+
* @covers \Yandex\Allure\Adapter\Support\AttachmentSupport
21+
*/
22+
class AttachmentSupportTest extends TestCase
23+
{
24+
25+
public function setUp(): void
26+
{
27+
Allure::reset();
28+
}
29+
30+
public function testAddAttachment_ResultFactoryProvidesAttachment_LifecycleAddsSameAttachment(): void
31+
{
32+
$attachment = new AttachmentResult('a');
33+
$lifecycle = $this->createMock(AllureLifecycleInterface::class);
34+
Allure::setLifecycleBuilder(
35+
$this->createLifecycleBuilder(
36+
$this->createResultFactoryWithAttachment($attachment),
37+
$lifecycle,
38+
),
39+
);
40+
Allure::setOutputDirectory('b');
41+
42+
/**
43+
* @psalm-suppress DeprecatedTrait
44+
*/
45+
$object = new class () {
46+
use AttachmentSupport;
47+
};
48+
$lifecycle
49+
->expects(self::once())
50+
->method('addAttachment')
51+
->with(self::identicalTo($attachment), self::isInstanceOf(DataSourceInterface::class));
52+
$object->addAttachment('c', 'd');
53+
}
54+
55+
public function testAddAttachment_GivenCaption_AttachmentHasSameName(): void
56+
{
57+
$attachment = new AttachmentResult('a');
58+
$lifecycle = $this->createMock(AllureLifecycleInterface::class);
59+
Allure::setLifecycleBuilder(
60+
$this->createLifecycleBuilder(
61+
$this->createResultFactoryWithAttachment($attachment),
62+
$lifecycle,
63+
),
64+
);
65+
Allure::setOutputDirectory('b');
66+
67+
/**
68+
* @psalm-suppress DeprecatedTrait
69+
*/
70+
$object = new class () {
71+
use AttachmentSupport;
72+
};
73+
$object->addAttachment('c', 'd');
74+
self::assertSame('d', $attachment->getName());
75+
}
76+
77+
public function testAddAttachment_NoTypeGiven_AttachmentHasNullType(): void
78+
{
79+
$attachment = new AttachmentResult('a');
80+
$lifecycle = $this->createMock(AllureLifecycleInterface::class);
81+
Allure::setLifecycleBuilder(
82+
$this->createLifecycleBuilder(
83+
$this->createResultFactoryWithAttachment($attachment),
84+
$lifecycle,
85+
),
86+
);
87+
Allure::setOutputDirectory('b');
88+
89+
/**
90+
* @psalm-suppress DeprecatedTrait
91+
*/
92+
$object = new class () {
93+
use AttachmentSupport;
94+
};
95+
$object->addAttachment('c', 'd');
96+
self::assertNull($attachment->getType());
97+
}
98+
99+
public function testAddAttachment_GivenType_AttachmentHasSameType(): void
100+
{
101+
$attachment = new AttachmentResult('a');
102+
$lifecycle = $this->createMock(AllureLifecycleInterface::class);
103+
Allure::setLifecycleBuilder(
104+
$this->createLifecycleBuilder(
105+
$this->createResultFactoryWithAttachment($attachment),
106+
$lifecycle,
107+
),
108+
);
109+
Allure::setOutputDirectory('b');
110+
111+
/**
112+
* @psalm-suppress DeprecatedTrait
113+
*/
114+
$object = new class () {
115+
use AttachmentSupport;
116+
};
117+
$object->addAttachment('c', 'd', 'e');
118+
self::assertSame('e', $attachment->getType());
119+
}
120+
121+
public function testAddAttachment_GivenFile_FileContentPassedToLifecycle(): void
122+
{
123+
$attachment = new AttachmentResult('a');
124+
$lifecycle = $this->createMock(AllureLifecycleInterface::class);
125+
Allure::setLifecycleBuilder(
126+
$this->createLifecycleBuilder(
127+
$this->createResultFactoryWithAttachment($attachment),
128+
$lifecycle,
129+
),
130+
);
131+
Allure::setOutputDirectory('b');
132+
133+
/**
134+
* @psalm-suppress DeprecatedTrait
135+
*/
136+
$object = new class () {
137+
use AttachmentSupport;
138+
};
139+
$lifecycle
140+
->expects(self::once())
141+
->method('addAttachment')
142+
->with(
143+
self::anything(),
144+
self::callback(
145+
function (DataSourceInterface $dataSource): bool {
146+
$resource = $dataSource->createStream();
147+
try {
148+
$data = fread($resource, 5);
149+
} finally {
150+
fclose($resource);
151+
}
152+
153+
return '<?php' === $data;
154+
}
155+
),
156+
);
157+
158+
$object->addAttachment(__FILE__, 'd');
159+
}
160+
161+
public function testAddAttachment_GivenString_StringContentPassedToLifecycle(): void
162+
{
163+
$attachment = new AttachmentResult('a');
164+
$lifecycle = $this->createMock(AllureLifecycleInterface::class);
165+
Allure::setLifecycleBuilder(
166+
$this->createLifecycleBuilder(
167+
$this->createResultFactoryWithAttachment($attachment),
168+
$lifecycle,
169+
),
170+
);
171+
Allure::setOutputDirectory('b');
172+
173+
/**
174+
* @psalm-suppress DeprecatedTrait
175+
*/
176+
$object = new class () {
177+
use AttachmentSupport;
178+
};
179+
$lifecycle
180+
->expects(self::once())
181+
->method('addAttachment')
182+
->with(
183+
self::anything(),
184+
self::callback(
185+
function (DataSourceInterface $dataSource): bool {
186+
$resource = $dataSource->createStream();
187+
try {
188+
$data = fread($resource, 4);
189+
} finally {
190+
fclose($resource);
191+
}
192+
193+
return 'cdef' === $data;
194+
}
195+
),
196+
);
197+
198+
$object->addAttachment('cdef', 'g');
199+
}
200+
201+
private function createLifecycleBuilder(
202+
?ResultFactoryInterface $resultFactory = null,
203+
?AllureLifecycleInterface $lifecycle = null,
204+
): LifecycleBuilderInterface {
205+
$builder = $this->createStub(LifecycleBuilderInterface::class);
206+
if (isset($resultFactory)) {
207+
$builder
208+
->method('getResultFactory')
209+
->willReturn($resultFactory);
210+
}
211+
if (isset($lifecycle)) {
212+
$builder
213+
->method('createLifecycle')
214+
->willReturn($lifecycle);
215+
}
216+
217+
return $builder;
218+
}
219+
220+
private function createResultFactoryWithAttachment(AttachmentResult $attachment): ResultFactoryInterface
221+
{
222+
$resultFactory = $this->createStub(ResultFactoryInterface::class);
223+
$resultFactory
224+
->method('createAttachment')
225+
->willReturn($attachment);
226+
227+
return $resultFactory;
228+
}
229+
}

0 commit comments

Comments
 (0)