|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Tests\unit\Magento\FunctionalTestingFramework\Allure; |
| 7 | + |
| 8 | +use Magento\FunctionalTestingFramework\Allure\AllureHelper; |
| 9 | +use Yandex\Allure\Adapter\Allure; |
| 10 | +use Yandex\Allure\Adapter\Event\AddAttachmentEvent; |
| 11 | +use Yandex\Allure\Adapter\Event\StepFinishedEvent; |
| 12 | +use Yandex\Allure\Adapter\Event\StepStartedEvent; |
| 13 | +use Yandex\Allure\Adapter\Model\Attachment; |
| 14 | +use AspectMock\Test as AspectMock; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class AllureHelperTest extends TestCase |
| 18 | +{ |
| 19 | + const MOCK_FILENAME = 'filename'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Clear Allure Lifecycle |
| 23 | + */ |
| 24 | + public function tearDown() |
| 25 | + { |
| 26 | + Allure::setDefaultLifecycle(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * AddAtachmentToStep should add an attachment to the current step |
| 31 | + * @throws \Yandex\Allure\Adapter\AllureException |
| 32 | + */ |
| 33 | + public function testAddAttachmentToStep() |
| 34 | + { |
| 35 | + $this->mockAttachmentWriteEvent(); |
| 36 | + $expectedData = "string"; |
| 37 | + $expectedCaption = "caption"; |
| 38 | + |
| 39 | + //Prepare Allure lifecycle |
| 40 | + Allure::lifecycle()->fire(new StepStartedEvent('firstStep')); |
| 41 | + |
| 42 | + //Call function |
| 43 | + AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption); |
| 44 | + |
| 45 | + // Assert Attachment is created as expected |
| 46 | + $step = Allure::lifecycle()->getStepStorage()->pollLast(); |
| 47 | + $expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null); |
| 48 | + $this->assertEquals($step->getAttachments()[0], $expectedAttachment); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * AddAttachmentToLastStep should add an attachment only to the last step |
| 53 | + * @throws \Yandex\Allure\Adapter\AllureException |
| 54 | + */ |
| 55 | + public function testAddAttachmentToLastStep() |
| 56 | + { |
| 57 | + $this->mockAttachmentWriteEvent(); |
| 58 | + $expectedData = "string"; |
| 59 | + $expectedCaption = "caption"; |
| 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()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Mock file system manipulation function |
| 89 | + * @throws \Exception |
| 90 | + */ |
| 91 | + public function mockAttachmentWriteEvent() |
| 92 | + { |
| 93 | + AspectMock::double(AddAttachmentEvent::class, [ |
| 94 | + "getAttachmentFileName" => self::MOCK_FILENAME |
| 95 | + ]); |
| 96 | + } |
| 97 | +} |
0 commit comments