|
| 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\Handlers; |
| 8 | + |
| 9 | +use AspectMock\Test as AspectMock; |
| 10 | + |
| 11 | +use Go\Aop\Aspect; |
| 12 | +use Magento\FunctionalTestingFramework\ObjectManager; |
| 13 | +use Magento\FunctionalTestingFramework\ObjectManagerFactory; |
| 14 | +use Magento\FunctionalTestingFramework\Test\Handlers\CestObjectHandler; |
| 15 | +use Magento\FunctionalTestingFramework\Test\Objects\ActionObject; |
| 16 | +use Magento\FunctionalTestingFramework\Test\Objects\CestHookObject; |
| 17 | +use Magento\FunctionalTestingFramework\Test\Objects\CestObject; |
| 18 | +use Magento\FunctionalTestingFramework\Test\Objects\TestObject; |
| 19 | +use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser; |
| 20 | +use Magento\FunctionalTestingFramework\Test\Util\ActionObjectExtractor; |
| 21 | +use Magento\FunctionalTestingFramework\Test\Util\CestObjectExtractor; |
| 22 | +use Magento\FunctionalTestingFramework\Test\Util\TestObjectExtractor; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +class CestObjectHandlerTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Mock cest name |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + private $testCestName = 'testCest'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Mock test name |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + private $testTestName = 'testTest'; |
| 40 | + |
| 41 | + /** |
| 42 | + * Mock before action name |
| 43 | + * |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + private $testActionBeforeName = 'testActionBefore'; |
| 47 | + |
| 48 | + /** |
| 49 | + * Mock after action name |
| 50 | + * |
| 51 | + * @var string |
| 52 | + */ |
| 53 | + private $testActionAfterName = 'testActionAfter'; |
| 54 | + |
| 55 | + /** |
| 56 | + * Mock test action in test name |
| 57 | + * |
| 58 | + * @var string |
| 59 | + */ |
| 60 | + private $testTestActionName = 'testActionInTest'; |
| 61 | + |
| 62 | + /** |
| 63 | + * Mock test action type |
| 64 | + * |
| 65 | + * @var string |
| 66 | + */ |
| 67 | + private $testActionType = 'testAction'; |
| 68 | + |
| 69 | + /** |
| 70 | + * Basic test to validate array => test object conversion |
| 71 | + */ |
| 72 | + public function testGetCestObject() |
| 73 | + { |
| 74 | + // set up mock data |
| 75 | + $mockData = [CestObjectExtractor::CEST_ROOT => [ |
| 76 | + $this->testCestName => [ |
| 77 | + CestObjectExtractor::NAME => $this->testCestName, |
| 78 | + CestObjectExtractor::CEST_BEFORE_HOOK => [ |
| 79 | + $this->testActionBeforeName => [ |
| 80 | + ActionObjectExtractor::NODE_NAME => $this->testActionType, |
| 81 | + ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testActionBeforeName |
| 82 | + ] |
| 83 | + ], |
| 84 | + CestObjectExtractor::CEST_AFTER_HOOK => [ |
| 85 | + $this->testActionAfterName => [ |
| 86 | + ActionObjectExtractor::NODE_NAME => $this->testActionType, |
| 87 | + ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testActionAfterName |
| 88 | + |
| 89 | + ] |
| 90 | + ], |
| 91 | + CestObjectExtractor::CEST_ANNOTATIONS => [ |
| 92 | + 'group' => [['value' => 'test']] |
| 93 | + ], |
| 94 | + $this->testTestName => [ |
| 95 | + $this->testTestActionName => [ |
| 96 | + ActionObjectExtractor::NODE_NAME => $this->testActionType, |
| 97 | + ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testTestActionName |
| 98 | + ], |
| 99 | + ] |
| 100 | + ] |
| 101 | + ] |
| 102 | + ]; |
| 103 | + |
| 104 | + $this->setMockParserOutput($mockData); |
| 105 | + |
| 106 | + // run object handler method |
| 107 | + $coh = CestObjectHandler::getInstance(); |
| 108 | + $actualCestObject = $coh->getObject($this->testCestName); |
| 109 | + |
| 110 | + // perform asserts |
| 111 | + $expectedBeforeActionObject = new ActionObject($this->testActionBeforeName, $this->testActionType, []); |
| 112 | + $expectedAfterActionObject = new ActionObject($this->testActionAfterName, $this->testActionType, []); |
| 113 | + $expectedBeforeHookObject = new CestHookObject( |
| 114 | + CestObjectExtractor::CEST_BEFORE_HOOK, |
| 115 | + $this->testCestName, |
| 116 | + [$expectedBeforeActionObject], |
| 117 | + [] |
| 118 | + ); |
| 119 | + $expectedAfterHookObject = new CestHookObject( |
| 120 | + CestObjectExtractor::CEST_AFTER_HOOK, |
| 121 | + $this->testCestName, |
| 122 | + [$expectedAfterActionObject], |
| 123 | + [] |
| 124 | + ); |
| 125 | + |
| 126 | + $expectedTestActionObject = new ActionObject($this->testTestActionName, $this->testActionType, []); |
| 127 | + $expectedTestObject = new TestObject($this->testTestName, [$expectedTestActionObject], [], []); |
| 128 | + |
| 129 | + $expectedCestObject = new CestObject( |
| 130 | + $this->testCestName, |
| 131 | + [ |
| 132 | + 'group' => ['test'] |
| 133 | + ], |
| 134 | + [ |
| 135 | + $this->testTestName => $expectedTestObject |
| 136 | + ], |
| 137 | + [ |
| 138 | + CestObjectExtractor::CEST_BEFORE_HOOK => $expectedBeforeHookObject, |
| 139 | + CestObjectExtractor::CEST_AFTER_HOOK => $expectedAfterHookObject |
| 140 | + ] |
| 141 | + ); |
| 142 | + |
| 143 | + $this->assertEquals($expectedCestObject, $actualCestObject); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Tests the function used to get a series of relevant tests/cests by group |
| 148 | + */ |
| 149 | + public function testGetCestsByGroup() |
| 150 | + { |
| 151 | + // set up mock data |
| 152 | + $mockData = [CestObjectExtractor::CEST_ROOT => [ |
| 153 | + $this->testCestName => [ |
| 154 | + CestObjectExtractor::NAME => $this->testCestName, |
| 155 | + CestObjectExtractor::CEST_ANNOTATIONS => [ |
| 156 | + 'group' => [['value' => 'test']] |
| 157 | + ], |
| 158 | + $this->testTestName => [ |
| 159 | + $this->testTestActionName => [ |
| 160 | + ActionObjectExtractor::NODE_NAME => $this->testActionType, |
| 161 | + ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testTestActionName |
| 162 | + ], |
| 163 | + ] |
| 164 | + ], |
| 165 | + $this->testCestName . '2' => [ |
| 166 | + CestObjectExtractor::NAME => $this->testCestName . '2', |
| 167 | + $this->testTestName . 'Include' => [ |
| 168 | + TestObjectExtractor::TEST_ANNOTATIONS => [ |
| 169 | + 'group' => [['value' => 'test']] |
| 170 | + ], |
| 171 | + $this->testTestActionName => [ |
| 172 | + ActionObjectExtractor::NODE_NAME => $this->testActionType, |
| 173 | + ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testTestActionName |
| 174 | + ], |
| 175 | + ], |
| 176 | + $this->testTestName . 'Exclude' => [ |
| 177 | + $this->testTestActionName => [ |
| 178 | + ActionObjectExtractor::NODE_NAME => $this->testActionType, |
| 179 | + ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testTestActionName |
| 180 | + ], |
| 181 | + ] |
| 182 | + ] |
| 183 | + ]]; |
| 184 | + |
| 185 | + $this->setMockParserOutput($mockData); |
| 186 | + |
| 187 | + // execute test method |
| 188 | + $coh = CestObjectHandler::getInstance(); |
| 189 | + $cests = $coh->getCestsByGroup('test'); |
| 190 | + |
| 191 | + // perform asserts |
| 192 | + $this->assertCount(2, $cests); |
| 193 | + $this->assertArrayHasKey($this->testCestName . '2', $cests); |
| 194 | + $actualTests = $cests[$this->testCestName . '2']->getTests(); |
| 195 | + $this->assertArrayHasKey($this->testTestName . 'Include', $actualTests); |
| 196 | + $this->assertArrayNotHasKey($this->testTestName . 'Exclude', $actualTests); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Function used to set mock for parser return and force init method to run between tests. |
| 201 | + * |
| 202 | + * @param array $data |
| 203 | + */ |
| 204 | + private function setMockParserOutput($data) |
| 205 | + { |
| 206 | + // clear cest object handler value to inject parsed content |
| 207 | + $property = new \ReflectionProperty(CestObjectHandler::class, 'cestObjectHandler'); |
| 208 | + $property->setAccessible(true); |
| 209 | + $property->setValue(null); |
| 210 | + |
| 211 | + $mockDataParser = AspectMock::double(TestDataParser::class, ['readTestData' => $data])->make(); |
| 212 | + $instance = AspectMock::double(ObjectManager::class, ['create' => $mockDataParser]) |
| 213 | + ->make(); // bypass the private constructor |
| 214 | + AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); |
| 215 | + } |
| 216 | +} |
0 commit comments