|
| 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\StaticCheck; |
| 8 | + |
| 9 | +use AspectMock\Test as AspectMock; |
| 10 | +use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler; |
| 11 | +use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject; |
| 12 | +use Magento\FunctionalTestingFramework\Page\Objects\ElementObject; |
| 13 | +use Magento\FunctionalTestingFramework\Page\Objects\PageObject; |
| 14 | +use Magento\FunctionalTestingFramework\Page\Objects\SectionObject; |
| 15 | +use Magento\FunctionalTestingFramework\StaticCheck\DeprecatedEntityUsageCheck; |
| 16 | +use Magento\FunctionalTestingFramework\Test\Objects\TestObject; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use tests\unit\Util\MagentoTestCase; |
| 19 | +use ReflectionClass; |
| 20 | +use InvalidArgumentException; |
| 21 | +use tests\unit\Util\ObjectHandlerUtil; |
| 22 | + |
| 23 | +class DeprecatedEntityUsageCheckTest extends MagentoTestCase |
| 24 | +{ |
| 25 | + /** @var DeprecatedEntityUsageCheck */ |
| 26 | + private $staticCheck; |
| 27 | + |
| 28 | + /** @var ReflectionClass*/ |
| 29 | + private $staticCheckClass; |
| 30 | + |
| 31 | + public function setUp(): void |
| 32 | + { |
| 33 | + $this->staticCheck = new DeprecatedEntityUsageCheck(); |
| 34 | + $this->staticCheckClass = new \ReflectionClass($this->staticCheck); |
| 35 | + } |
| 36 | + |
| 37 | + public function tearDown(): void |
| 38 | + { |
| 39 | + AspectMock::clean(); |
| 40 | + } |
| 41 | + |
| 42 | + public function testInvalidPathOption() |
| 43 | + { |
| 44 | + $input = $this->getMockBuilder(InputInterface::class) |
| 45 | + ->disableOriginalConstructor() |
| 46 | + ->getMock(); |
| 47 | + |
| 48 | + $input->method('getOption') |
| 49 | + ->with('path') |
| 50 | + ->willReturn('/invalidPath'); |
| 51 | + |
| 52 | + $loadAllXmlFiles = $this->staticCheckClass->getMethod('loadAllXMLFiles'); |
| 53 | + $loadAllXmlFiles->setAccessible(true); |
| 54 | + |
| 55 | + $this->expectException(InvalidArgumentException::class); |
| 56 | + $loadAllXmlFiles->invoke($this->staticCheck, $input); |
| 57 | + } |
| 58 | + |
| 59 | + public function testViolatingElementReferences() |
| 60 | + { |
| 61 | + //variables for assertions |
| 62 | + $elementName = 'elementOne'; |
| 63 | + $sectionName = 'SectionOne'; |
| 64 | + $fileName = 'section.xml'; |
| 65 | + |
| 66 | + $element = new ElementObject($elementName, 'type', '#selector1', null, '41', false, 'deprecated'); |
| 67 | + $section = new SectionObject($sectionName, [$element], $fileName); |
| 68 | + $elementRef = $sectionName . '.' . $elementName; |
| 69 | + $references = [$elementRef => $element, $sectionName => $section]; |
| 70 | + $actual = $this->callViolatingReferences($references); |
| 71 | + $expected = [ |
| 72 | + 'Deprecated Element(s)' => [ |
| 73 | + 0 => [ |
| 74 | + 'name' => $elementRef, |
| 75 | + 'file' => $fileName |
| 76 | + ] |
| 77 | + ] |
| 78 | + ]; |
| 79 | + $this->assertEquals($actual, $expected); |
| 80 | + } |
| 81 | + |
| 82 | + public function testViolatingPageReferences() |
| 83 | + { |
| 84 | + //Page variables for assertions |
| 85 | + $pageName = 'Page'; |
| 86 | + $fileName = 'page.xml'; |
| 87 | + |
| 88 | + $page = new PageObject($pageName, '/url.html', 'Test', [], false, "test", $fileName, 'deprecated'); |
| 89 | + $references = ['Page' => $page]; |
| 90 | + $actual = $this->callViolatingReferences($references); |
| 91 | + $expected = [ |
| 92 | + 'Deprecated Page(s)' => [ |
| 93 | + 0 => [ |
| 94 | + 'name' => $pageName, |
| 95 | + 'file' => $fileName |
| 96 | + ] |
| 97 | + ] |
| 98 | + ]; |
| 99 | + $this->assertEquals($actual, $expected); |
| 100 | + } |
| 101 | + |
| 102 | + public function testViolatingDataReferences() |
| 103 | + { |
| 104 | + //Data entity variables for assertions |
| 105 | + $entityName = 'EntityOne'; |
| 106 | + $fileName = 'entity.xml'; |
| 107 | + |
| 108 | + $entity = new EntityDataObject( |
| 109 | + $entityName, |
| 110 | + 'testType', |
| 111 | + ['testkey' => 'testValue'], |
| 112 | + [], |
| 113 | + null, |
| 114 | + [], |
| 115 | + null, |
| 116 | + $fileName, |
| 117 | + 'deprecated' |
| 118 | + ); |
| 119 | + $references = [$entityName => $entity]; |
| 120 | + $actual = $this->callViolatingReferences($references); |
| 121 | + $expected = [ |
| 122 | + 'Deprecated Data(s)' => [ |
| 123 | + 0 => [ |
| 124 | + 'name' => $entityName, |
| 125 | + 'file' => $fileName |
| 126 | + ] |
| 127 | + ] |
| 128 | + ]; |
| 129 | + $this->assertEquals($actual, $expected); |
| 130 | + } |
| 131 | + |
| 132 | + public function testViolatingTestReferences() |
| 133 | + { |
| 134 | + // test variables for assertions |
| 135 | + $testName = 'Test1'; |
| 136 | + $fileName = 'test.xml'; |
| 137 | + |
| 138 | + $test = new TestObject($testName, [], [], [], $fileName, null, 'deprecated'); |
| 139 | + $references = ['Test1' => $test]; |
| 140 | + $actual = $this->callViolatingReferences($references); |
| 141 | + $expected = [ |
| 142 | + 'Deprecated Test(s)' => [ |
| 143 | + 0 => [ |
| 144 | + 'name' => $testName, |
| 145 | + 'file' => $fileName |
| 146 | + ] |
| 147 | + ] |
| 148 | + ]; |
| 149 | + $this->assertEquals($actual, $expected); |
| 150 | + } |
| 151 | + |
| 152 | + public function testViolatingMetaDataReferences() |
| 153 | + { |
| 154 | + // Data Variables for Assertions |
| 155 | + $dataType1 = "type1"; |
| 156 | + $operationType1 = "create"; |
| 157 | + $operationType2 = "update"; |
| 158 | + |
| 159 | + /** |
| 160 | + * Parser Output. |
| 161 | + * operationName |
| 162 | + * createType1 |
| 163 | + * has field |
| 164 | + * key=id, value=integer |
| 165 | + * updateType1 |
| 166 | + * has field |
| 167 | + * key=id, value=integer |
| 168 | + */ |
| 169 | + $mockData = [OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG => [ |
| 170 | + "testOperationName" => [ |
| 171 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $dataType1, |
| 172 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE => $operationType1, |
| 173 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_AUTH => "auth", |
| 174 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_URL => "V1/Type1", |
| 175 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_METHOD => "POST", |
| 176 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY => [ |
| 177 | + 0 => [ |
| 178 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_KEY => "id", |
| 179 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => "integer" |
| 180 | + ], |
| 181 | + ], |
| 182 | + OperationDefinitionObjectHandler::OBJ_DEPRECATED => 'deprecated' |
| 183 | + ],[ |
| 184 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $dataType1, |
| 185 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE => $operationType2, |
| 186 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_AUTH => "auth", |
| 187 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_URL => "V1/Type1/{id}", |
| 188 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_METHOD => "PUT", |
| 189 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY => [ |
| 190 | + 0 => [ |
| 191 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_KEY => "id", |
| 192 | + OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => "integer" |
| 193 | + ], |
| 194 | + ] |
| 195 | + ]]]; |
| 196 | + |
| 197 | + ObjectHandlerUtil::getInstance()->setMockOperationParserOutput($mockData); |
| 198 | + $dataName = 'dataName1'; |
| 199 | + $references = [ |
| 200 | + $dataName => [ |
| 201 | + $dataType1 => [ |
| 202 | + $operationType1, |
| 203 | + $operationType2 |
| 204 | + ] |
| 205 | + ] |
| 206 | + ]; |
| 207 | + |
| 208 | + $expected = [ |
| 209 | + '"'.$dataName.'" references deprecated' => [ |
| 210 | + 0 => [ |
| 211 | + 'name' => $dataType1, |
| 212 | + 'file' => 'metadata xml file' |
| 213 | + ] |
| 214 | + ] |
| 215 | + ]; |
| 216 | + $property = $this->staticCheckClass->getMethod('findViolatingMetadataReferences'); |
| 217 | + $property->setAccessible(true); |
| 218 | + $actual = $property->invoke($this->staticCheck, $references); |
| 219 | + $this->assertEquals($actual, $expected); |
| 220 | + } |
| 221 | + |
| 222 | + public function testIsDeprecated() |
| 223 | + { |
| 224 | + // Test Data |
| 225 | + $contents = '<tests> |
| 226 | + <test name="test" deprecated="true"> |
| 227 | + <comment userInput="input1" stepKey="key1"/> |
| 228 | + <comment userInput="input2" stepKey="key1"/> |
| 229 | + </test> |
| 230 | + </tests> |
| 231 | + '; |
| 232 | + |
| 233 | + $property = $this->staticCheckClass->getMethod('isDeprecated'); |
| 234 | + $property->setAccessible(true); |
| 235 | + $output = $property->invoke($this->staticCheck, $contents); |
| 236 | + $this->assertTrue($output); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Invoke findViolatingReferences |
| 241 | + * @param $references |
| 242 | + * @return mixed |
| 243 | + * @throws \ReflectionException |
| 244 | + */ |
| 245 | + public function callViolatingReferences($references) |
| 246 | + { |
| 247 | + $property = $this->staticCheckClass->getMethod('findViolatingReferences'); |
| 248 | + $property->setAccessible(true); |
| 249 | + return $property->invoke($this->staticCheck, $references); |
| 250 | + } |
| 251 | +} |
0 commit comments