Skip to content

Commit ad0e4aa

Browse files
authored
Merge pull request #436 from magento/MQE-1633+1514
MQE-1633 + MQE-1514
2 parents 04fa585 + a448a0f commit ad0e4aa

File tree

5 files changed

+175
-7
lines changed

5 files changed

+175
-7
lines changed

dev/tests/_bootstrap.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
$kernel = \AspectMock\Kernel::getInstance();
1818
$kernel->init([
1919
'debug' => true,
20-
'includePaths' => [PROJECT_ROOT . DIRECTORY_SEPARATOR . 'src'],
20+
'includePaths' => [
21+
PROJECT_ROOT . DIRECTORY_SEPARATOR . 'src',
22+
PROJECT_ROOT . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'allure-framework'
23+
],
2124
'cacheDir' => PROJECT_ROOT .
2225
DIRECTORY_SEPARATOR .
2326
'dev' .
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\FunctionalTestingFramework\Allure;
7+
8+
use Yandex\Allure\Adapter\Allure;
9+
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
10+
11+
class AllureHelper
12+
{
13+
/**
14+
* Adds attachment to the current step
15+
* @param mixed $data
16+
* @param string $caption
17+
* @throws \Yandex\Allure\Adapter\AllureException
18+
* @return void
19+
*/
20+
public static function addAttachmentToCurrentStep($data, $caption)
21+
{
22+
Allure::lifecycle()->fire(new AddAttachmentEvent($data, $caption));
23+
}
24+
25+
/**
26+
* Adds Attachment to the last executed step.
27+
* Use this when adding attachments outside of an $I->doSomething() step/context.
28+
* @param mixed $data
29+
* @param string $caption
30+
* @return void
31+
*/
32+
public static function addAttachmentToLastStep($data, $caption)
33+
{
34+
$rootStep = Allure::lifecycle()->getStepStorage()->getLast();
35+
$trueLastStep = array_last($rootStep->getSteps());
36+
37+
$attachmentEvent = new AddAttachmentEvent($data, $caption);
38+
$attachmentEvent->process($trueLastStep);
39+
}
40+
}

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/CurlHandler.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\FunctionalTestingFramework\DataGenerator\Persist;
77

8+
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
89
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\AdminExecutor;
910
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\FrontendExecutor;
1011
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\WebapiExecutor;
@@ -166,6 +167,14 @@ public function executeRequest($dependentEntities)
166167
$response = $executor->read($successRegex, $returnRegex, $returnIndex);
167168
$executor->close();
168169

170+
AllureHelper::addAttachmentToLastStep($apiUrl, 'API Endpoint');
171+
AllureHelper::addAttachmentToLastStep(json_encode($headers, JSON_PRETTY_PRINT), 'Request Headers');
172+
AllureHelper::addAttachmentToLastStep(json_encode($this->requestData, JSON_PRETTY_PRINT), 'Request Body');
173+
AllureHelper::addAttachmentToLastStep(
174+
json_encode(json_decode($response, true), JSON_PRETTY_PRINT+JSON_UNESCAPED_UNICODE+JSON_UNESCAPED_SLASHES),
175+
'Response Data'
176+
);
177+
169178
return $response;
170179
}
171180

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Codeception\Module\WebDriver;
1010
use Codeception\Test\Descriptor;
1111
use Codeception\TestInterface;
12+
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
1213
use Facebook\WebDriver\Interactions\WebDriverActions;
1314
use Codeception\Exception\ModuleConfigException;
1415
use Codeception\Exception\ModuleException;
@@ -198,7 +199,10 @@ public function _getCurrentUri()
198199
*/
199200
public function dontSeeCurrentUrlEquals($url)
200201
{
201-
$this->assertNotEquals($url, $this->webDriver->getCurrentURL());
202+
$actualUrl = $this->webDriver->getCurrentURL();
203+
$comparison = "Expected: $url\nActual: $actualUrl";
204+
AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison');
205+
$this->assertNotEquals($url, $actualUrl);
202206
}
203207

204208
/**
@@ -209,7 +213,10 @@ public function dontSeeCurrentUrlEquals($url)
209213
*/
210214
public function dontSeeCurrentUrlMatches($regex)
211215
{
212-
$this->assertNotRegExp($regex, $this->webDriver->getCurrentURL());
216+
$actualUrl = $this->webDriver->getCurrentURL();
217+
$comparison = "Expected: $regex\nActual: $actualUrl";
218+
AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison');
219+
$this->assertNotRegExp($regex, $actualUrl);
213220
}
214221

215222
/**
@@ -220,7 +227,10 @@ public function dontSeeCurrentUrlMatches($regex)
220227
*/
221228
public function dontSeeInCurrentUrl($needle)
222229
{
223-
$this->assertNotContains($needle, $this->webDriver->getCurrentURL());
230+
$actualUrl = $this->webDriver->getCurrentURL();
231+
$comparison = "Expected: $needle\nActual: $actualUrl";
232+
AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison');
233+
$this->assertNotContains($needle, $actualUrl);
224234
}
225235

226236
/**
@@ -254,7 +264,10 @@ public function grabFromCurrentUrl($regex = null)
254264
*/
255265
public function seeCurrentUrlEquals($url)
256266
{
257-
$this->assertEquals($url, $this->webDriver->getCurrentURL());
267+
$actualUrl = $this->webDriver->getCurrentURL();
268+
$comparison = "Expected: $url\nActual: $actualUrl";
269+
AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison');
270+
$this->assertEquals($url, $actualUrl);
258271
}
259272

260273
/**
@@ -265,7 +278,10 @@ public function seeCurrentUrlEquals($url)
265278
*/
266279
public function seeCurrentUrlMatches($regex)
267280
{
268-
$this->assertRegExp($regex, $this->webDriver->getCurrentURL());
281+
$actualUrl = $this->webDriver->getCurrentURL();
282+
$comparison = "Expected: $regex\nActual: $actualUrl";
283+
AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison');
284+
$this->assertRegExp($regex, $actualUrl);
269285
}
270286

271287
/**
@@ -276,7 +292,10 @@ public function seeCurrentUrlMatches($regex)
276292
*/
277293
public function seeInCurrentUrl($needle)
278294
{
279-
$this->assertContains($needle, $this->webDriver->getCurrentURL());
295+
$actualUrl = $this->webDriver->getCurrentURL();
296+
$comparison = "Expected: $needle\nActual: $actualUrl";
297+
AllureHelper::addAttachmentToCurrentStep($comparison, 'Comparison');
298+
$this->assertContains($needle, $actualUrl);
280299
}
281300

282301
/**

0 commit comments

Comments
 (0)