|
| 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\DataGenerator\Handlers; |
| 8 | + |
| 9 | +use AspectMock\Test as AspectMock; |
| 10 | +use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler; |
| 11 | +use Magento\FunctionalTestingFramework\DataGenerator\Handlers\PersistedObjectHandler; |
| 12 | +use Magento\FunctionalTestingFramework\DataGenerator\Parsers\DataProfileSchemaParser; |
| 13 | +use Magento\FunctionalTestingFramework\DataGenerator\Persist\CurlHandler; |
| 14 | +use Magento\FunctionalTestingFramework\ObjectManager; |
| 15 | +use Magento\FunctionalTestingFramework\ObjectManagerFactory; |
| 16 | +use Magento\FunctionalTestingFramework\Util\MagentoTestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class PersistedObjectHandlerTest |
| 20 | + */ |
| 21 | +class PersistedObjectHandlerTest extends MagentoTestCase |
| 22 | +{ |
| 23 | + public function testCreateSimpleEntity() |
| 24 | + { |
| 25 | + // Test Data and Variables |
| 26 | + $entityName = "EntityOne"; |
| 27 | + $entityStepKey = "StepKey"; |
| 28 | + $dataKey = "testKey"; |
| 29 | + $dataValue = "testValue"; |
| 30 | + $scope = PersistedObjectHandler::TEST_SCOPE; |
| 31 | + $parserOutput = [ |
| 32 | + 'entity' => [ |
| 33 | + 'EntityOne' => [ |
| 34 | + 'type' => 'testType', |
| 35 | + 'data' => [ |
| 36 | + 0 => [ |
| 37 | + 'key' => $dataKey, |
| 38 | + 'value' => $dataValue |
| 39 | + ] |
| 40 | + ] |
| 41 | + ] |
| 42 | + ] |
| 43 | + ]; |
| 44 | + $jsonResponse = " |
| 45 | + { |
| 46 | + \"" . strtolower($dataKey) . "\" : \"{$dataValue}\" |
| 47 | + } |
| 48 | + "; |
| 49 | + |
| 50 | + // Mock Classes |
| 51 | + $this->mockDataHandlerWithOutput($parserOutput); |
| 52 | + $this->mockCurlHandler($jsonResponse); |
| 53 | + $handler = PersistedObjectHandler::getInstance(); |
| 54 | + |
| 55 | + // Call method |
| 56 | + $handler->createEntity( |
| 57 | + $entityStepKey, |
| 58 | + $scope, |
| 59 | + $entityName |
| 60 | + ); |
| 61 | + |
| 62 | + $persistedValue = $handler->retrieveEntityField($entityStepKey, $dataKey, $scope); |
| 63 | + $this->assertEquals($dataValue, $persistedValue); |
| 64 | + } |
| 65 | + |
| 66 | + public function testDeleteSimpleEntity() |
| 67 | + { |
| 68 | + // Test Data and Variables |
| 69 | + $entityName = "EntityOne"; |
| 70 | + $entityStepKey = "StepKey"; |
| 71 | + $dataKey = "testKey"; |
| 72 | + $dataValue = "testValue"; |
| 73 | + $scope = PersistedObjectHandler::TEST_SCOPE; |
| 74 | + $parserOutput = [ |
| 75 | + 'entity' => [ |
| 76 | + 'EntityOne' => [ |
| 77 | + 'type' => 'testType', |
| 78 | + 'data' => [ |
| 79 | + 0 => [ |
| 80 | + 'key' => $dataKey, |
| 81 | + 'value' => $dataValue |
| 82 | + ] |
| 83 | + ] |
| 84 | + ] |
| 85 | + ] |
| 86 | + ]; |
| 87 | + $jsonResponse = " |
| 88 | + { |
| 89 | + \"" . strtolower($dataKey) . "\" : \"{$dataValue}\" |
| 90 | + } |
| 91 | + "; |
| 92 | + |
| 93 | + // Mock Classes |
| 94 | + $this->mockDataHandlerWithOutput($parserOutput); |
| 95 | + $this->mockCurlHandler($jsonResponse); |
| 96 | + $handler = PersistedObjectHandler::getInstance(); |
| 97 | + |
| 98 | + // Call method |
| 99 | + $handler->createEntity( |
| 100 | + $entityStepKey, |
| 101 | + $scope, |
| 102 | + $entityName |
| 103 | + ); |
| 104 | + |
| 105 | + $handler->deleteEntity( |
| 106 | + $entityStepKey, |
| 107 | + $scope |
| 108 | + ); |
| 109 | + |
| 110 | + // Handler found and called Delete on existing entity |
| 111 | + $this->addToAssertionCount(1); |
| 112 | + } |
| 113 | + |
| 114 | + public function testGetSimpleEntity() |
| 115 | + { |
| 116 | + // Test Data and Variables |
| 117 | + $entityName = "EntityOne"; |
| 118 | + $entityStepKey = "StepKey"; |
| 119 | + $dataKey = "testKey"; |
| 120 | + $dataValue = "testValue"; |
| 121 | + $scope = PersistedObjectHandler::TEST_SCOPE; |
| 122 | + $parserOutput = [ |
| 123 | + 'entity' => [ |
| 124 | + 'EntityOne' => [ |
| 125 | + 'type' => 'testType', |
| 126 | + 'data' => [ |
| 127 | + 0 => [ |
| 128 | + 'key' => $dataKey, |
| 129 | + 'value' => $dataValue |
| 130 | + ] |
| 131 | + ] |
| 132 | + ] |
| 133 | + ] |
| 134 | + ]; |
| 135 | + $jsonResponse = " |
| 136 | + { |
| 137 | + \"" . strtolower($dataKey) . "\" : \"{$dataValue}\" |
| 138 | + } |
| 139 | + "; |
| 140 | + |
| 141 | + // Mock Classes |
| 142 | + $this->mockDataHandlerWithOutput($parserOutput); |
| 143 | + $this->mockCurlHandler($jsonResponse); |
| 144 | + $handler = PersistedObjectHandler::getInstance(); |
| 145 | + |
| 146 | + // Call method |
| 147 | + $handler->getEntity( |
| 148 | + $entityStepKey, |
| 149 | + $scope, |
| 150 | + $entityName |
| 151 | + ); |
| 152 | + |
| 153 | + $persistedValue = $handler->retrieveEntityField($entityStepKey, $dataKey, $scope); |
| 154 | + $this->assertEquals($dataValue, $persistedValue); |
| 155 | + } |
| 156 | + |
| 157 | + public function testUpdateSimpleEntity() |
| 158 | + { |
| 159 | + $this->markTestSkipped("Potential Bug in DataPersistenceHandler class"); |
| 160 | + // Test Data and Variables |
| 161 | + $entityName = "EntityOne"; |
| 162 | + $entityStepKey = "StepKey"; |
| 163 | + $dataKey = "testKey"; |
| 164 | + $dataValue = "testValue"; |
| 165 | + $updateName = "EntityTwo"; |
| 166 | + $updateValue = "newValue"; |
| 167 | + $scope = PersistedObjectHandler::TEST_SCOPE; |
| 168 | + $parserOutput = [ |
| 169 | + 'entity' => [ |
| 170 | + $entityName => [ |
| 171 | + 'type' => 'testType', |
| 172 | + 'data' => [ |
| 173 | + 0 => [ |
| 174 | + 'key' => $dataKey, |
| 175 | + 'value' => $dataValue |
| 176 | + ] |
| 177 | + ] |
| 178 | + ], |
| 179 | + $updateName => [ |
| 180 | + 'type' => 'testType', |
| 181 | + 'data' => [ |
| 182 | + 0 => [ |
| 183 | + 'key' => $dataKey, |
| 184 | + 'value' => $updateValue |
| 185 | + ] |
| 186 | + ] |
| 187 | + ] |
| 188 | + ] |
| 189 | + ]; |
| 190 | + $jsonResponse = " |
| 191 | + { |
| 192 | + \"" . strtolower($dataKey) . "\" : \"{$dataValue}\" |
| 193 | + } |
| 194 | + "; |
| 195 | + $updatedResponse = " |
| 196 | + { |
| 197 | + \"" . strtolower($dataKey) . "\" : \"{$updateValue}\" |
| 198 | + } |
| 199 | + "; |
| 200 | + |
| 201 | + // Mock Classes |
| 202 | + $this->mockDataHandlerWithOutput($parserOutput); |
| 203 | + $this->mockCurlHandler($jsonResponse); |
| 204 | + $handler = PersistedObjectHandler::getInstance(); |
| 205 | + $handler->createEntity( |
| 206 | + $entityStepKey, |
| 207 | + $scope, |
| 208 | + $entityName |
| 209 | + ); |
| 210 | + $this->mockCurlHandler($updatedResponse); |
| 211 | + |
| 212 | + // Call method |
| 213 | + $handler->updateEntity( |
| 214 | + $entityStepKey, |
| 215 | + $scope, |
| 216 | + $updateName |
| 217 | + ); |
| 218 | + |
| 219 | + $persistedValue = $handler->retrieveEntityField($entityStepKey, $dataKey, $scope); |
| 220 | + $this->assertEquals($updateValue, $persistedValue); |
| 221 | + } |
| 222 | + |
| 223 | + public function testRetrieveEntityAcrossScopes() |
| 224 | + { |
| 225 | + // Test Data and Variables |
| 226 | + $entityNameOne = "EntityOne"; |
| 227 | + $entityStepKeyOne = "StepKeyOne"; |
| 228 | + $dataKeyOne = "testKeyOne"; |
| 229 | + $dataValueOne = "testValueOne"; |
| 230 | + $entityNameTwo = "EntityTwo"; |
| 231 | + $entityStepKeyTwo = "StepKeyTwo"; |
| 232 | + $dataKeyTwo = "testKeyTwo"; |
| 233 | + $dataValueTwo = "testValueTwo"; |
| 234 | + $entityNameThree = "EntityThree"; |
| 235 | + $entityStepKeyThree = "StepKeyThree"; |
| 236 | + $dataKeyThree = "testKeyThree"; |
| 237 | + $dataValueThree = "testValueThree"; |
| 238 | + |
| 239 | + $parserOutputOne = [ |
| 240 | + 'entity' => [ |
| 241 | + $entityNameOne => [ |
| 242 | + 'type' => 'testType', |
| 243 | + 'data' => [ |
| 244 | + 0 => [ |
| 245 | + 'key' => $dataKeyOne, |
| 246 | + 'value' => $dataValueOne |
| 247 | + ] |
| 248 | + ] |
| 249 | + ], |
| 250 | + $entityNameTwo => [ |
| 251 | + 'type' => 'testType', |
| 252 | + 'data' => [ |
| 253 | + 0 => [ |
| 254 | + 'key' => $dataKeyTwo, |
| 255 | + 'value' => $dataValueTwo |
| 256 | + ] |
| 257 | + ] |
| 258 | + ], |
| 259 | + $entityNameThree => [ |
| 260 | + 'type' => 'testType', |
| 261 | + 'data' => [ |
| 262 | + 0 => [ |
| 263 | + 'key' => $dataKeyThree, |
| 264 | + 'value' => $dataValueThree |
| 265 | + ] |
| 266 | + ] |
| 267 | + ] |
| 268 | + ] |
| 269 | + ]; |
| 270 | + $jsonReponseOne = " |
| 271 | + { |
| 272 | + \"" . strtolower($dataKeyOne) . "\" : \"{$dataValueOne}\" |
| 273 | + } |
| 274 | + "; |
| 275 | + $jsonReponseTwo = " |
| 276 | + { |
| 277 | + \"" . strtolower($dataKeyTwo) . "\" : \"{$dataValueTwo}\" |
| 278 | + } |
| 279 | + "; |
| 280 | + $jsonReponseThree = " |
| 281 | + { |
| 282 | + \"" . strtolower($dataKeyThree) . "\" : \"{$dataValueThree}\" |
| 283 | + } |
| 284 | + "; |
| 285 | + |
| 286 | + // Mock Classes and Create Entities |
| 287 | + $handler = PersistedObjectHandler::getInstance(); |
| 288 | + |
| 289 | + $this->mockDataHandlerWithOutput($parserOutputOne); |
| 290 | + $this->mockCurlHandler($jsonReponseOne); |
| 291 | + $handler->createEntity( |
| 292 | + $entityStepKeyOne, |
| 293 | + PersistedObjectHandler::TEST_SCOPE, |
| 294 | + $entityNameOne |
| 295 | + ); |
| 296 | + |
| 297 | + $this->mockCurlHandler($jsonReponseTwo); |
| 298 | + $handler->createEntity( |
| 299 | + $entityStepKeyTwo, |
| 300 | + PersistedObjectHandler::HOOK_SCOPE, |
| 301 | + $entityNameTwo |
| 302 | + ); |
| 303 | + |
| 304 | + $this->mockCurlHandler($jsonReponseThree); |
| 305 | + $handler->createEntity( |
| 306 | + $entityStepKeyThree, |
| 307 | + PersistedObjectHandler::SUITE_SCOPE, |
| 308 | + $entityNameThree |
| 309 | + ); |
| 310 | + |
| 311 | + // Call method |
| 312 | + $retrievedFromTest = $handler->retrieveEntityField( |
| 313 | + $entityStepKeyOne, |
| 314 | + $dataKeyOne, |
| 315 | + PersistedObjectHandler::HOOK_SCOPE |
| 316 | + ); |
| 317 | + $retrievedFromHook = $handler->retrieveEntityField( |
| 318 | + $entityStepKeyTwo, |
| 319 | + $dataKeyTwo, |
| 320 | + PersistedObjectHandler::SUITE_SCOPE |
| 321 | + ); |
| 322 | + $retrievedFromSuite = $handler->retrieveEntityField( |
| 323 | + $entityStepKeyThree, |
| 324 | + $dataKeyThree, |
| 325 | + PersistedObjectHandler::TEST_SCOPE |
| 326 | + ); |
| 327 | + |
| 328 | + $this->assertEquals($dataValueOne, $retrievedFromTest); |
| 329 | + $this->assertEquals($dataValueTwo, $retrievedFromHook); |
| 330 | + $this->assertEquals($dataValueThree, $retrievedFromSuite); |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * Mocks DataObjectHandler to use given output to create |
| 335 | + * @param $parserOutput |
| 336 | + * @throws \Exception |
| 337 | + */ |
| 338 | + public function mockDataHandlerWithOutput($parserOutput) |
| 339 | + { |
| 340 | + // Clear DataObjectHandler singleton if already set |
| 341 | + $property = new \ReflectionProperty(DataObjectHandler::class, "INSTANCE"); |
| 342 | + $property->setAccessible(true); |
| 343 | + $property->setValue(null); |
| 344 | + |
| 345 | + $mockDataProfileSchemaParser = AspectMock::double(DataProfileSchemaParser::class, [ |
| 346 | + 'readDataProfiles' => $parserOutput |
| 347 | + ])->make(); |
| 348 | + |
| 349 | + $mockObjectManager = AspectMock::double(ObjectManager::class, [ |
| 350 | + 'create' => $mockDataProfileSchemaParser |
| 351 | + ])->make(); |
| 352 | + |
| 353 | + AspectMock::double(ObjectManagerFactory::class, [ |
| 354 | + 'getObjectManager' => $mockObjectManager |
| 355 | + ]); |
| 356 | + } |
| 357 | + |
| 358 | + public function mockCurlHandler($response) |
| 359 | + { |
| 360 | + AspectMock::double(CurlHandler::class, [ |
| 361 | + "__construct" => null, |
| 362 | + "executeRequest" => $response, |
| 363 | + "getRequestDataArray" => [], |
| 364 | + "isContentTypeJson" => true |
| 365 | + ]); |
| 366 | + } |
| 367 | + |
| 368 | + public function tearDown() |
| 369 | + { |
| 370 | + // Clear out Singleton between tests |
| 371 | + $property = new \ReflectionProperty(PersistedObjectHandler::class, "INSTANCE"); |
| 372 | + $property->setAccessible(true); |
| 373 | + $property->setValue(null); |
| 374 | + |
| 375 | + parent::tearDown(); // TODO: Change the autogenerated stub |
| 376 | + } |
| 377 | +} |
0 commit comments