|
8 | 8 | namespace tests\unit\Magento\FunctionalTestFramework\Allure;
|
9 | 9 |
|
10 | 10 | use Magento\FunctionalTestingFramework\Allure\AllureHelper;
|
11 |
| -use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent; |
12 |
| -use Magento\FunctionalTestingFramework\ObjectManager; |
13 | 11 | use PHPUnit\Framework\TestCase;
|
14 |
| -use ReflectionProperty; |
15 |
| -use Yandex\Allure\Adapter\Allure; |
16 |
| -use Yandex\Allure\Adapter\AllureException; |
17 |
| -use Yandex\Allure\Adapter\Event\StepFinishedEvent; |
18 |
| -use Yandex\Allure\Adapter\Event\StepStartedEvent; |
19 |
| -use Yandex\Allure\Adapter\Model\Attachment; |
| 12 | +use Qameta\Allure\Allure; |
| 13 | +use Qameta\Allure\Io\DataSourceFactory; |
| 14 | +use Qameta\Allure\Model\AttachmentResult; |
| 15 | +use Qameta\Allure\Model\ResultFactoryInterface; |
| 16 | +use Qameta\Allure\Setup\LifecycleBuilderInterface; |
| 17 | +use const STDOUT; |
20 | 18 |
|
| 19 | +/** |
| 20 | + * @covers \Qameta\Allure\Allure |
| 21 | + */ |
21 | 22 | class AllureHelperTest extends TestCase
|
22 | 23 | {
|
23 |
| - private const MOCK_FILENAME = 'filename'; |
24 |
| - |
25 |
| - /** |
26 |
| - * The AddAttachmentToStep should add an attachment to the current step. |
27 |
| - * |
28 |
| - * @return void |
29 |
| - * @throws AllureException |
30 |
| - */ |
31 |
| - public function testAddAttachmentToStep(): void |
| 24 | + public function setUp(): void |
32 | 25 | {
|
33 |
| - $expectedData = 'string'; |
34 |
| - $expectedCaption = 'caption'; |
35 |
| - $this->mockAttachmentWriteEvent($expectedData, $expectedCaption); |
36 |
| - |
37 |
| - //Prepare Allure lifecycle |
38 |
| - Allure::lifecycle()->fire(new StepStartedEvent('firstStep')); |
39 |
| - |
40 |
| - //Call function |
41 |
| - AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption); |
42 |
| - |
43 |
| - // Assert Attachment is created as expected |
44 |
| - $step = Allure::lifecycle()->getStepStorage()->pollLast(); |
45 |
| - $expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null); |
46 |
| - $this->assertEquals($step->getAttachments()[0], $expectedAttachment); |
| 26 | + Allure::reset(); |
47 | 27 | }
|
48 | 28 |
|
49 | 29 | /**
|
50 |
| - * The AddAttachmentToLastStep should add an attachment only to the last step. |
51 |
| - * |
52 |
| - * @return void |
53 |
| - * @throws AllureException |
| 30 | + * @dataProvider providerAttachmentProperties |
54 | 31 | */
|
55 |
| - public function testAddAttachmentToLastStep(): void |
56 |
| - { |
57 |
| - $expectedData = 'string'; |
58 |
| - $expectedCaption = 'caption'; |
59 |
| - $this->mockAttachmentWriteEvent($expectedData, $expectedCaption); |
60 |
| - |
61 |
| - //Prepare Allure lifecycle |
62 |
| - Allure::lifecycle()->fire(new StepStartedEvent('firstStep')); |
63 |
| - Allure::lifecycle()->fire(new StepFinishedEvent('firstStep')); |
64 |
| - Allure::lifecycle()->fire(new StepStartedEvent('secondStep')); |
65 |
| - Allure::lifecycle()->fire(new StepFinishedEvent('secondStep')); |
66 |
| - |
67 |
| - //Call function |
68 |
| - AllureHelper::addAttachmentToLastStep($expectedData, $expectedCaption); |
69 |
| - |
70 |
| - //Continue Allure lifecycle |
71 |
| - Allure::lifecycle()->fire(new StepStartedEvent('thirdStep')); |
72 |
| - Allure::lifecycle()->fire(new StepFinishedEvent('thirdStep')); |
73 |
| - |
74 |
| - // Assert Attachment is created as expected on the right step |
75 |
| - $rootStep = Allure::lifecycle()->getStepStorage()->pollLast(); |
76 |
| - |
77 |
| - $firstStep = $rootStep->getSteps()[0]; |
78 |
| - $secondStep = $rootStep->getSteps()[1]; |
79 |
| - $thirdStep = $rootStep->getSteps()[2]; |
80 |
| - |
81 |
| - $expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null); |
82 |
| - $this->assertEmpty($firstStep->getAttachments()); |
83 |
| - $this->assertEquals($secondStep->getAttachments()[0], $expectedAttachment); |
84 |
| - $this->assertEmpty($thirdStep->getAttachments()); |
| 32 | + public function testDoAddAttachmentMethod( |
| 33 | + string $name, |
| 34 | + $type, |
| 35 | + ?string $fileExtension, |
| 36 | + ): void { |
| 37 | + $attachment = new AttachmentResult('a'); |
| 38 | + Allure::setLifecycleBuilder( |
| 39 | + $this->createLifecycleBuilder($this->createResultFactoryWithAttachment($attachment)), |
| 40 | + ); |
| 41 | + |
| 42 | + AllureHelper::doAddAttachment( |
| 43 | + DataSourceFactory::fromFile('test'), |
| 44 | + 'nameOfTheFile', |
| 45 | + 'typeOfTheFile', |
| 46 | + $fileExtension |
| 47 | + ); |
| 48 | + self::assertSame('nameOfTheFile', $attachment->getName()); |
| 49 | + self::assertSame('typeOfTheFile', $attachment->getType()); |
85 | 50 | }
|
86 | 51 |
|
87 | 52 | /**
|
88 |
| - * The AddAttachment actions should have files with different attachment names. |
89 |
| - * |
90 |
| - * @return void |
91 |
| - * @throws AllureException |
| 53 | + * @dataProvider providerAttachmentProperties |
92 | 54 | */
|
93 |
| - public function testAddAttachmentUniqueName(): void |
94 |
| - { |
95 |
| - $expectedData = 'string'; |
96 |
| - $expectedCaption = 'caption'; |
97 |
| - |
98 |
| - //Prepare Allure lifecycle |
99 |
| - Allure::lifecycle()->fire(new StepStartedEvent('firstStep')); |
100 |
| - |
101 |
| - //Call function twice |
102 |
| - AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption); |
103 |
| - AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption); |
104 |
| - |
105 |
| - // Assert file names for both attachments are not the same. |
106 |
| - $step = Allure::lifecycle()->getStepStorage()->pollLast(); |
107 |
| - $attachmentOne = $step->getAttachments()[0]->getSource(); |
108 |
| - $attachmentTwo = $step->getAttachments()[1]->getSource(); |
109 |
| - $this->assertNotEquals($attachmentOne, $attachmentTwo); |
| 55 | + public function testAddAttachmentToStep( |
| 56 | + string $name, |
| 57 | + ?string $type, |
| 58 | + ?string $fileExtension, |
| 59 | + ): void { |
| 60 | + $attachment = new AttachmentResult('a'); |
| 61 | + Allure::setLifecycleBuilder( |
| 62 | + $this->createLifecycleBuilder($this->createResultFactoryWithAttachment($attachment)), |
| 63 | + ); |
| 64 | + |
| 65 | + Allure::attachment($name, 'nameOfTheFile', $type, $fileExtension); |
| 66 | + self::assertSame($name, $attachment->getName()); |
| 67 | + self::assertSame($type, $attachment->getType()); |
| 68 | + self::assertSame($fileExtension, $attachment->getFileExtension()); |
110 | 69 | }
|
111 | 70 |
|
112 | 71 | /**
|
113 |
| - * Clear Allure Lifecycle. |
114 |
| - * |
115 |
| - * @return void |
| 72 | + * @dataProvider providerAttachmentProperties |
116 | 73 | */
|
117 |
| - protected function tearDown(): void |
118 |
| - { |
119 |
| - Allure::setDefaultLifecycle(); |
120 |
| - |
121 |
| - $objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance'); |
122 |
| - $objectManagerProperty->setAccessible(true); |
123 |
| - $objectManagerProperty->setValue(null); |
| 74 | + public function testAddAttachmentFileToStep( |
| 75 | + string $name, |
| 76 | + ?string $type, |
| 77 | + ?string $fileExtension, |
| 78 | + ): void { |
| 79 | + $attachment = new AttachmentResult('a'); |
| 80 | + Allure::setLifecycleBuilder( |
| 81 | + $this->createLifecycleBuilder($this->createResultFactoryWithAttachment($attachment)), |
| 82 | + ); |
| 83 | + |
| 84 | + Allure::attachmentFile($name, 'b', $type, '.html'); |
| 85 | + self::assertSame('c', $attachment->getName()); |
| 86 | + self::assertSame('.html', $attachment->getFileExtension()); |
124 | 87 | }
|
125 | 88 |
|
126 | 89 | /**
|
127 |
| - * Mock entire attachment writing mechanisms. |
128 |
| - * |
129 |
| - * @param string $filePathOrContents |
130 |
| - * @param string $caption |
131 |
| - * |
132 |
| - * @return void |
| 90 | + * @return iterable<string, array{string, string|null, string|null}> |
133 | 91 | */
|
134 |
| - private function mockAttachmentWriteEvent(string $filePathOrContents, string $caption): void |
| 92 | + public static function providerAttachmentProperties(): iterable |
135 | 93 | {
|
136 |
| - $mockInstance = $this->getMockBuilder(AddUniqueAttachmentEvent::class) |
137 |
| - ->setConstructorArgs([$filePathOrContents, $caption]) |
138 |
| - ->disallowMockingUnknownTypes() |
139 |
| - ->onlyMethods(['getAttachmentFileName']) |
140 |
| - ->getMock(); |
141 |
| - |
142 |
| - $mockInstance |
143 |
| - ->method('getAttachmentFileName') |
144 |
| - ->willReturn(self::MOCK_FILENAME); |
| 94 | + return [ |
| 95 | + 'Only name' => ['c', null, null], |
| 96 | + 'Name and type' => ['c', 'd', null], |
| 97 | + 'Name and file extension' => ['c', null, 'd'], |
| 98 | + 'Name, type and file extension' => ['c', 'd', 'e'], |
| 99 | + ]; |
| 100 | + } |
145 | 101 |
|
146 |
| - $objectManagerMockInstance = $this->createMock(ObjectManager::class); |
147 |
| - $objectManagerMockInstance |
148 |
| - ->method('create') |
149 |
| - ->will( |
150 |
| - $this->returnCallback( |
151 |
| - function (string $class) use ($mockInstance) { |
152 |
| - if ($class === AddUniqueAttachmentEvent::class) { |
153 |
| - return $mockInstance; |
154 |
| - } |
| 102 | + private function createResultFactoryWithAttachment(AttachmentResult $attachment): ResultFactoryInterface |
| 103 | + { |
| 104 | + $resultFactory = $this->createStub(ResultFactoryInterface::class); |
| 105 | + $resultFactory |
| 106 | + ->method('createAttachment') |
| 107 | + ->willReturn($attachment); |
155 | 108 |
|
156 |
| - return null; |
157 |
| - } |
158 |
| - ) |
159 |
| - ); |
| 109 | + return $resultFactory; |
| 110 | + } |
160 | 111 |
|
161 |
| - $objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance'); |
162 |
| - $objectManagerProperty->setAccessible(true); |
163 |
| - $objectManagerProperty->setValue($objectManagerMockInstance, $objectManagerMockInstance); |
| 112 | + private function createLifecycleBuilder( |
| 113 | + ?ResultFactoryInterface $resultFactory = null, |
| 114 | + ?AllureLifecycleInterface $lifecycle = null, |
| 115 | + ?StatusDetectorInterface $statusDetector = null, |
| 116 | + ): LifecycleBuilderInterface { |
| 117 | + $builder = $this->createStub(LifecycleBuilderInterface::class); |
| 118 | + if (isset($resultFactory)) { |
| 119 | + $builder |
| 120 | + ->method('getResultFactory') |
| 121 | + ->willReturn($resultFactory); |
| 122 | + } |
| 123 | + if (isset($lifecycle)) { |
| 124 | + $builder |
| 125 | + ->method('createLifecycle') |
| 126 | + ->willReturn($lifecycle); |
| 127 | + } |
| 128 | + if (isset($statusDetector)) { |
| 129 | + $builder |
| 130 | + ->method('getStatusDetector') |
| 131 | + ->willReturn($statusDetector); |
| 132 | + } |
| 133 | + |
| 134 | + return $builder; |
164 | 135 | }
|
165 | 136 | }
|
0 commit comments