|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace tests\unit\Magento\FunctionalTestFramework\Test\Objects; |
| 8 | + |
| 9 | +use AspectMock\Test as AspectMock; |
| 10 | +use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler; |
| 11 | +use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException; |
| 12 | +use Magento\FunctionalTestingFramework\Page\Handlers\SectionObjectHandler; |
| 13 | +use Magento\FunctionalTestingFramework\Page\Objects\ElementObject; |
| 14 | +use Magento\FunctionalTestingFramework\Page\Objects\SectionObject; |
| 15 | +use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject; |
| 16 | +use Magento\FunctionalTestingFramework\Test\Objects\ActionObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use tests\unit\Util\ActionGroupObjectBuilder; |
| 19 | +use tests\unit\Util\EntityDataObjectBuilder; |
| 20 | + |
| 21 | +class ActionGroupObjectTest extends TestCase |
| 22 | +{ |
| 23 | + const ACTION_GROUP_MERGE_KEY = 'testKey'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Tests a string literal in an action group |
| 27 | + */ |
| 28 | + public function testGetStepsWithDefaultCase() |
| 29 | + { |
| 30 | + $entity = (new EntityDataObjectBuilder()) |
| 31 | + ->withDataFields(['field1' => 'testValue']) |
| 32 | + ->build(); |
| 33 | + $this->setEntityObjectHandlerReturn($entity); |
| 34 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder())->build(); |
| 35 | + $steps = $actionGroupUnderTest->getSteps(null, self::ACTION_GROUP_MERGE_KEY); |
| 36 | + $this->assertOnMergeKeyAndActionValue($steps, ['userInput' => 'literal']); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Tests a data reference in an action group, replaced by the user |
| 41 | + */ |
| 42 | + public function testGetStepsWithCustomArgs() |
| 43 | + { |
| 44 | + $this->setEntityObjectHandlerReturn(function ($entityName) { |
| 45 | + if ($entityName == "data2") { |
| 46 | + return (new EntityDataObjectBuilder())->withDataFields(['field2' => 'testValue2'])->build(); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder()) |
| 51 | + ->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{arg1.field2}}'])]) |
| 52 | + ->build(); |
| 53 | + |
| 54 | + $steps = $actionGroupUnderTest->getSteps(['arg1' => 'data2'], self::ACTION_GROUP_MERGE_KEY); |
| 55 | + $this->assertOnMergeKeyAndActionValue($steps, ['userInput' => 'testValue2']); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Tests a data reference in an action group replaced with a persisted reference. |
| 60 | + */ |
| 61 | + public function testGetStepsWithPersistedArgs() |
| 62 | + { |
| 63 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder()) |
| 64 | + ->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{arg1.field2}}'])]) |
| 65 | + ->build(); |
| 66 | + |
| 67 | + $steps = $actionGroupUnderTest->getSteps(['arg1' => '$data3$'], self::ACTION_GROUP_MERGE_KEY); |
| 68 | + $this->assertOnMergeKeyAndActionValue($steps, ['userInput' => '$data3.field2$']); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Tests a data reference in an action group replaced with a data.field reference. |
| 73 | + */ |
| 74 | + public function testGetStepsWithNoFieldArg() |
| 75 | + { |
| 76 | + $this->setEntityObjectHandlerReturn(function ($entityName) { |
| 77 | + if ($entityName == "data2") { |
| 78 | + return (new EntityDataObjectBuilder())->withDataFields(['field2' => 'testValue2'])->build(); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder()) |
| 83 | + ->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{arg1}}'])]) |
| 84 | + ->build(); |
| 85 | + |
| 86 | + $steps = $actionGroupUnderTest->getSteps(['arg1' => 'data2.field2'], self::ACTION_GROUP_MERGE_KEY); |
| 87 | + $this->assertOnMergeKeyAndActionValue($steps, ['userInput' => 'testValue2']); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Tests a data reference in an action group resolved with its default state. |
| 92 | + */ |
| 93 | + public function testGetStepsWithNoArgs() |
| 94 | + { |
| 95 | + $this->setEntityObjectHandlerReturn(function ($entityName) { |
| 96 | + if ($entityName == "data1") { |
| 97 | + return (new EntityDataObjectBuilder())->withDataFields(['field1' => 'testValue'])->build(); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder()) |
| 102 | + ->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{data1.field1}}'])]) |
| 103 | + ->build(); |
| 104 | + |
| 105 | + $steps = $actionGroupUnderTest->getSteps(null, self::ACTION_GROUP_MERGE_KEY); |
| 106 | + $this->assertOnMergeKeyAndActionValue($steps, ['userInput' => 'testValue']); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Tests a parameterized section reference in an action group resolved with user args. |
| 111 | + */ |
| 112 | + public function testGetStepsWithParameterizedArg() |
| 113 | + { |
| 114 | + // mock the section object handler response |
| 115 | + $element = new ElementObject("element1", "textArea", ".selector {{var1}}", null, null, true); |
| 116 | + $section = new SectionObject("testSection", ["element1" => $element]); |
| 117 | + // bypass the private constructor |
| 118 | + $sectionInstance = AspectMock::double(SectionObjectHandler::class, ['getObject' => $section])->make(); |
| 119 | + AspectMock::double(SectionObjectHandler::class, ['getInstance' => $sectionInstance]); |
| 120 | + |
| 121 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder()) |
| 122 | + ->withActionObjects( |
| 123 | + [new ActionObject('action1', 'testAction', ['selector' => '{{section1.element1(arg1.field1)}}'])] |
| 124 | + ) |
| 125 | + ->build(); |
| 126 | + |
| 127 | + $steps = $actionGroupUnderTest->getSteps(['arg1' => '$someData$'], self::ACTION_GROUP_MERGE_KEY); |
| 128 | + $this->assertOnMergeKeyAndActionValue($steps, ['selector' => '.selector $someData.field1$']); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Tests a data reference in an action group resolved with a persisted reference used in another function. |
| 133 | + */ |
| 134 | + public function testGetStepsWithOuterScopePersistence() |
| 135 | + { |
| 136 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder()) |
| 137 | + ->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{arg1.field1}}'])]) |
| 138 | + ->build(); |
| 139 | + |
| 140 | + $steps = $actionGroupUnderTest->getSteps(['arg1' => '$$someData$$'], self::ACTION_GROUP_MERGE_KEY); |
| 141 | + $this->assertOnMergeKeyAndActionValue($steps, ['userInput' => '$$someData.field1$$']); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Tests an action group with mismatching args. |
| 146 | + */ |
| 147 | + public function testExceptionOnMissingActions() |
| 148 | + { |
| 149 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder()) |
| 150 | + ->withArguments(['arg1' => null]) |
| 151 | + ->build(); |
| 152 | + |
| 153 | + $this->expectException(TestReferenceException::class); |
| 154 | + $this->expectExceptionMessageRegExp('/Argument\(s\) missed .* for actionGroup/'); |
| 155 | + $actionGroupUnderTest->getSteps(['arg2' => 'data1'], self::ACTION_GROUP_MERGE_KEY); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Tests an action group with missing args. |
| 160 | + */ |
| 161 | + public function testExceptionOnMissingArguments() |
| 162 | + { |
| 163 | + $actionGroupUnderTest = (new ActionGroupObjectBuilder()) |
| 164 | + ->withArguments(['arg1' => null]) |
| 165 | + ->build(); |
| 166 | + |
| 167 | + $this->expectException(TestReferenceException::class); |
| 168 | + $this->expectExceptionMessageRegExp('/Not enough arguments given for actionGroup .*/'); |
| 169 | + $actionGroupUnderTest->getSteps(null, self::ACTION_GROUP_MERGE_KEY); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * This function takes a desired return for the EntityObjectHandler mock and performs set up of the mock for the |
| 174 | + * duration of a single test case. |
| 175 | + * |
| 176 | + * @param mixed $return |
| 177 | + * @return void |
| 178 | + */ |
| 179 | + private function setEntityObjectHandlerReturn($return) |
| 180 | + { |
| 181 | + $instance = AspectMock::double(DataObjectHandler::class, ['getObject' => $return]) |
| 182 | + ->make(); // bypass the private constructor |
| 183 | + AspectMock::double(DataObjectHandler::class, ['getInstance' => $instance]); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Given a set of steps and an expected custom attribute value, this function performs a set of asserts to validate |
| 188 | + * information such as step key and step attribute value. |
| 189 | + * |
| 190 | + * @param array $actions |
| 191 | + * @param array $expectedValue |
| 192 | + * @param string $expectedMergeKey |
| 193 | + * @return void |
| 194 | + */ |
| 195 | + private function assertOnMergeKeyAndActionValue($actions, $expectedValue, $expectedMergeKey = null) |
| 196 | + { |
| 197 | + $expectedMergeKey = $expectedMergeKey ?? |
| 198 | + ActionGroupObjectBuilder::DEFAULT_ACTION_OBJECT_NAME . self::ACTION_GROUP_MERGE_KEY; |
| 199 | + $this->assertArrayHasKey($expectedMergeKey, $actions); |
| 200 | + |
| 201 | + $action = $actions[$expectedMergeKey]; |
| 202 | + $this->assertEquals($expectedMergeKey, $action->getStepKey()); |
| 203 | + $this->assertEquals($expectedValue, $action->getCustomActionAttributes()); |
| 204 | + } |
| 205 | +} |
0 commit comments