forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllureHelperTest.php
165 lines (140 loc) · 5.75 KB
/
AllureHelperTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace tests\unit\Magento\FunctionalTestFramework\Allure;
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
use Magento\FunctionalTestingFramework\ObjectManager;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Yandex\Allure\Adapter\Allure;
use Yandex\Allure\Adapter\AllureException;
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
use Yandex\Allure\Adapter\Event\StepStartedEvent;
use Yandex\Allure\Adapter\Model\Attachment;
class AllureHelperTest extends TestCase
{
private const MOCK_FILENAME = 'filename';
/**
* The AddAttachmentToStep should add an attachment to the current step.
*
* @return void
* @throws AllureException
*/
public function testAddAttachmentToStep(): void
{
$expectedData = 'string';
$expectedCaption = 'caption';
$this->mockAttachmentWriteEvent($expectedData, $expectedCaption);
//Prepare Allure lifecycle
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
//Call function
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
// Assert Attachment is created as expected
$step = Allure::lifecycle()->getStepStorage()->pollLast();
$expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null);
$this->assertEquals($step->getAttachments()[0], $expectedAttachment);
}
/**
* The AddAttachmentToLastStep should add an attachment only to the last step.
*
* @return void
* @throws AllureException
*/
public function testAddAttachmentToLastStep(): void
{
$expectedData = 'string';
$expectedCaption = 'caption';
$this->mockAttachmentWriteEvent($expectedData, $expectedCaption);
//Prepare Allure lifecycle
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
Allure::lifecycle()->fire(new StepFinishedEvent('firstStep'));
Allure::lifecycle()->fire(new StepStartedEvent('secondStep'));
Allure::lifecycle()->fire(new StepFinishedEvent('secondStep'));
//Call function
AllureHelper::addAttachmentToLastStep($expectedData, $expectedCaption);
//Continue Allure lifecycle
Allure::lifecycle()->fire(new StepStartedEvent('thirdStep'));
Allure::lifecycle()->fire(new StepFinishedEvent('thirdStep'));
// Assert Attachment is created as expected on the right step
$rootStep = Allure::lifecycle()->getStepStorage()->pollLast();
$firstStep = $rootStep->getSteps()[0];
$secondStep = $rootStep->getSteps()[1];
$thirdStep = $rootStep->getSteps()[2];
$expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null);
$this->assertEmpty($firstStep->getAttachments());
$this->assertEquals($secondStep->getAttachments()[0], $expectedAttachment);
$this->assertEmpty($thirdStep->getAttachments());
}
/**
* The AddAttachment actions should have files with different attachment names.
*
* @return void
* @throws AllureException
*/
public function testAddAttachmentUniqueName(): void
{
$expectedData = 'string';
$expectedCaption = 'caption';
//Prepare Allure lifecycle
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
//Call function twice
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
// Assert file names for both attachments are not the same.
$step = Allure::lifecycle()->getStepStorage()->pollLast();
$attachmentOne = $step->getAttachments()[0]->getSource();
$attachmentTwo = $step->getAttachments()[1]->getSource();
$this->assertNotEquals($attachmentOne, $attachmentTwo);
}
/**
* Clear Allure Lifecycle.
*
* @return void
*/
protected function tearDown(): void
{
Allure::setDefaultLifecycle();
$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
$objectManagerProperty->setAccessible(true);
$objectManagerProperty->setValue(null);
}
/**
* Mock entire attachment writing mechanisms.
*
* @param string $filePathOrContents
* @param string $caption
*
* @return void
*/
private function mockAttachmentWriteEvent(string $filePathOrContents, string $caption): void
{
$mockInstance = $this->getMockBuilder(AddUniqueAttachmentEvent::class)
->setConstructorArgs([$filePathOrContents, $caption])
->disallowMockingUnknownTypes()
->onlyMethods(['getAttachmentFileName'])
->getMock();
$mockInstance
->method('getAttachmentFileName')
->willReturn(self::MOCK_FILENAME);
$objectManagerMockInstance = $this->createMock(ObjectManager::class);
$objectManagerMockInstance
->method('create')
->will(
$this->returnCallback(
function (string $class) use ($mockInstance) {
if ($class === AddUniqueAttachmentEvent::class) {
return $mockInstance;
}
return null;
}
)
);
$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
$objectManagerProperty->setAccessible(true);
$objectManagerProperty->setValue($objectManagerMockInstance, $objectManagerMockInstance);
}
}