From e8bbf77f2b1639604560507c7733df21deee904c Mon Sep 17 00:00:00 2001 From: Karyna Date: Thu, 1 Jul 2021 14:04:37 +0300 Subject: [PATCH 1/2] Eliminate AspectMock from FileStorageTest --- .../SecretStorage/FileStorageTest.php | 23 +++++++++++++------ .../Handlers/SecretStorage/FileStorage.php | 19 +++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/SecretStorage/FileStorageTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/SecretStorage/FileStorageTest.php index 62cb9798e..be97e04ab 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/SecretStorage/FileStorageTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/SecretStorage/FileStorageTest.php @@ -3,29 +3,38 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace tests\unit\Magento\FunctionalTestFramework\DataGenerator\Handlers\SecretStorage; use Magento\FunctionalTestingFramework\DataGenerator\Handlers\SecretStorage\FileStorage; +use ReflectionClass; use tests\unit\Util\MagentoTestCase; -use AspectMock\Test as AspectMock; class FileStorageTest extends MagentoTestCase { - /** * Test basic encryption/decryption functionality in FileStorage class. */ - public function testBasicEncryptDecrypt() + public function testBasicEncryptDecrypt(): void { $testKey = 'magento/myKey'; $testValue = 'myValue'; - - AspectMock::double(FileStorage::class, [ - 'readInCredentialsFile' => ["$testKey=$testValue"] - ]); + $creds = ["$testKey=$testValue"]; $fileStorage = new FileStorage(); + $reflection = new ReflectionClass(FileStorage::class); + + // Emulate initialize() function result with the test credentials + $reflectionMethod = $reflection->getMethod('encryptCredFileContents'); + $reflectionMethod->setAccessible(true); + $secretData = $reflectionMethod->invokeArgs($fileStorage, [$creds]); + + // Set encrypted test credentials to the private 'secretData' property + $reflectionProperty = $reflection->getProperty('secretData'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($fileStorage, $secretData); + $encryptedCred = $fileStorage->getEncryptedValue($testKey); // assert the value we've gotten is in fact not identical to our test value diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php index be77a6de2..62ecfbedc 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php @@ -21,14 +21,17 @@ class FileStorage extends BaseStorage private $secretData = []; /** - * FileStorage constructor + * Initialize secret data value which represents encrypted credentials + * + * @return void * @throws TestFrameworkException */ - public function __construct() + private function initialize(): void { - parent::__construct(); - $creds = $this->readInCredentialsFile(); - $this->secretData = $this->encryptCredFileContents($creds); + if (!$this->secretData) { + $creds = $this->readInCredentialsFile(); + $this->secretData = $this->encryptCredFileContents($creds); + } } /** @@ -36,10 +39,12 @@ public function __construct() * * @param string $key * @return string|null + * @throws TestFrameworkException */ - public function getEncryptedValue($key) + public function getEncryptedValue($key): ?string { - $value = null; + $this->initialize(); + // Check if secret is in cached array if (null !== ($value = parent::getEncryptedValue($key))) { return $value; From 6563d302b9d6b35018a90356a4ef4904250e98d4 Mon Sep 17 00:00:00 2001 From: Karyna Date: Wed, 28 Jul 2021 12:15:40 +0300 Subject: [PATCH 2/2] Refactor CredentialStorage after FileStorage constructor elimination --- .../DataGenerator/Handlers/CredentialStore.php | 6 ++++-- .../DataGenerator/Handlers/SecretStorage/FileStorage.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/CredentialStore.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/CredentialStore.php index d6e6b9a69..36b2daf41 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/CredentialStore.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/CredentialStore.php @@ -221,11 +221,13 @@ private function initializeCredentialStorage() * * @return void */ - private function initializeFileStorage() + private function initializeFileStorage(): void { // Initialize file storage try { - $this->credStorage[self::ARRAY_KEY_FOR_FILE] = new FileStorage(); + $fileStorage = new FileStorage(); + $fileStorage->initialize(); + $this->credStorage[self::ARRAY_KEY_FOR_FILE] = $fileStorage; } catch (TestFrameworkException $e) { // Print error message in console print_r($e->getMessage()); diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php index 62ecfbedc..479c608e4 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php @@ -26,7 +26,7 @@ class FileStorage extends BaseStorage * @return void * @throws TestFrameworkException */ - private function initialize(): void + public function initialize(): void { if (!$this->secretData) { $creds = $this->readInCredentialsFile();