|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace tests\unit\Magento\FunctionalTestFramework\Test\Util; |
| 7 | + |
| 8 | +use Magento\FunctionalTestingFramework\Test\Objects\ActionObject; |
| 9 | +use Magento\FunctionalTestingFramework\Test\Util\ActionObjectExtractor; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class ActionObjectExtractorTest extends TestCase |
| 13 | +{ |
| 14 | + /** @var ActionObjectExtractor */ |
| 15 | + private $testActionObjectExtractor; |
| 16 | + |
| 17 | + /** |
| 18 | + * Setup method |
| 19 | + */ |
| 20 | + public function setUp() |
| 21 | + { |
| 22 | + $this->testActionObjectExtractor = new ActionObjectExtractor(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Tests basic action object extraction with a valid parser array. |
| 27 | + */ |
| 28 | + public function testBasicActionObjectExtration() |
| 29 | + { |
| 30 | + $actionObjects = $this->testActionObjectExtractor->extractActions($this->createBasicActionObjectArray()); |
| 31 | + $this->assertCount(1, $actionObjects); |
| 32 | + |
| 33 | + /** @var ActionObject $firstElement */ |
| 34 | + $firstElement = array_values($actionObjects)[0]; |
| 35 | + $this->assertEquals('testAction1', $firstElement->getStepKey()); |
| 36 | + $this->assertCount(1, $firstElement->getCustomActionAttributes()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Tests an invalid merge order reference (i.e. a step referencing itself). |
| 41 | + */ |
| 42 | + public function testInvalidMergeOrderReference() |
| 43 | + { |
| 44 | + $invalidArray = $this->createBasicActionObjectArray('invalidTestAction1', 'invalidTestAction1'); |
| 45 | + |
| 46 | + $this->expectException('\Magento\FunctionalTestingFramework\Exceptions\TestReferenceException'); |
| 47 | + $expectedExceptionMessage = "Invalid ordering configuration in test TestWithSelfReferencingStepKey with step" . |
| 48 | + " key(s):\n\tinvalidTestAction1\n"; |
| 49 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 50 | + |
| 51 | + $this->testActionObjectExtractor->extractActions($invalidArray, 'TestWithSelfReferencingStepKey'); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Validates a warning is printed to the console when multiple actions reference the same actions for merging. |
| 56 | + */ |
| 57 | + public function testAmbiguousMergeOrderRefernece() |
| 58 | + { |
| 59 | + $ambiguousArray = $this->createBasicActionObjectArray('testAction1'); |
| 60 | + $ambiguousArray = array_merge( |
| 61 | + $ambiguousArray, |
| 62 | + $this->createBasicActionObjectArray('testAction2', 'testAction1') |
| 63 | + ); |
| 64 | + |
| 65 | + $ambiguousArray = array_merge( |
| 66 | + $ambiguousArray, |
| 67 | + $this->createBasicActionObjectArray('testAction3', null, 'testAction1') |
| 68 | + ); |
| 69 | + |
| 70 | + $outputString = "multiple actions referencing step key testAction1 in test AmbiguousRefTest:\n" . |
| 71 | + "\ttestAction2\n" . |
| 72 | + "\ttestAction3\n"; |
| 73 | + |
| 74 | + $this->expectOutputString($outputString); |
| 75 | + $this->testActionObjectExtractor->extractActions($ambiguousArray, 'AmbiguousRefTest'); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Utility function to return mock parser output for testing extraction into ActionObjects. |
| 80 | + * |
| 81 | + * @param string $stepKey |
| 82 | + * @param string $before |
| 83 | + * @param string $after |
| 84 | + * @return array |
| 85 | + */ |
| 86 | + private function createBasicActionObjectArray($stepKey = 'testAction1', $before = null, $after = null) |
| 87 | + { |
| 88 | + $baseArray = [ |
| 89 | + $stepKey => [ |
| 90 | + "nodeName" => "sampleAction", |
| 91 | + "stepKey" => $stepKey, |
| 92 | + "someAttribute" => "someAttributeValue" |
| 93 | + ] |
| 94 | + ]; |
| 95 | + |
| 96 | + if ($before) { |
| 97 | + $baseArray[$stepKey] = array_merge($baseArray[$stepKey], ['before' => $before]); |
| 98 | + } |
| 99 | + |
| 100 | + if ($after) { |
| 101 | + $baseArray[$stepKey] = array_merge($baseArray[$stepKey], ['after' => $after]); |
| 102 | + } |
| 103 | + |
| 104 | + return $baseArray; |
| 105 | + } |
| 106 | +} |
0 commit comments