Skip to content

Commit 12e405e

Browse files
authored
Merge pull request #66 from magento-commerce/imported-magento-magento2-functional-testing-framework-842
[Imported] 33295: Eliminate AspectMock from FileStorageTest
2 parents e847e7e + 9dc2923 commit 12e405e

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/SecretStorage/FileStorageTest.php

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,38 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace tests\unit\Magento\FunctionalTestFramework\DataGenerator\Handlers\SecretStorage;
89

910
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\SecretStorage\FileStorage;
11+
use ReflectionClass;
1012
use tests\unit\Util\MagentoTestCase;
11-
use AspectMock\Test as AspectMock;
1213

1314
class FileStorageTest extends MagentoTestCase
1415
{
15-
1616
/**
1717
* Test basic encryption/decryption functionality in FileStorage class.
1818
*/
19-
public function testBasicEncryptDecrypt()
19+
public function testBasicEncryptDecrypt(): void
2020
{
2121
$testKey = 'magento/myKey';
2222
$testValue = 'myValue';
23-
24-
AspectMock::double(FileStorage::class, [
25-
'readInCredentialsFile' => ["$testKey=$testValue"]
26-
]);
23+
$creds = ["$testKey=$testValue"];
2724

2825
$fileStorage = new FileStorage();
26+
$reflection = new ReflectionClass(FileStorage::class);
27+
28+
// Emulate initialize() function result with the test credentials
29+
$reflectionMethod = $reflection->getMethod('encryptCredFileContents');
30+
$reflectionMethod->setAccessible(true);
31+
$secretData = $reflectionMethod->invokeArgs($fileStorage, [$creds]);
32+
33+
// Set encrypted test credentials to the private 'secretData' property
34+
$reflectionProperty = $reflection->getProperty('secretData');
35+
$reflectionProperty->setAccessible(true);
36+
$reflectionProperty->setValue($fileStorage, $secretData);
37+
2938
$encryptedCred = $fileStorage->getEncryptedValue($testKey);
3039

3140
// assert the value we've gotten is in fact not identical to our test value

src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/CredentialStore.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ private function initializeCredentialStorage()
221221
*
222222
* @return void
223223
*/
224-
private function initializeFileStorage()
224+
private function initializeFileStorage(): void
225225
{
226226
// Initialize file storage
227227
try {
228-
$this->credStorage[self::ARRAY_KEY_FOR_FILE] = new FileStorage();
228+
$fileStorage = new FileStorage();
229+
$fileStorage->initialize();
230+
$this->credStorage[self::ARRAY_KEY_FOR_FILE] = $fileStorage;
229231
} catch (TestFrameworkException $e) {
230232
// Print error message in console
231233
print_r($e->getMessage());

src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/FileStorage.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,30 @@ class FileStorage extends BaseStorage
2121
private $secretData = [];
2222

2323
/**
24-
* FileStorage constructor
24+
* Initialize secret data value which represents encrypted credentials
25+
*
26+
* @return void
2527
* @throws TestFrameworkException
2628
*/
27-
public function __construct()
29+
public function initialize(): void
2830
{
29-
parent::__construct();
30-
$creds = $this->readInCredentialsFile();
31-
$this->secretData = $this->encryptCredFileContents($creds);
31+
if (!$this->secretData) {
32+
$creds = $this->readInCredentialsFile();
33+
$this->secretData = $this->encryptCredFileContents($creds);
34+
}
3235
}
3336

3437
/**
3538
* Returns the value of a secret based on corresponding key
3639
*
3740
* @param string $key
3841
* @return string|null
42+
* @throws TestFrameworkException
3943
*/
40-
public function getEncryptedValue($key)
44+
public function getEncryptedValue($key): ?string
4145
{
42-
$value = null;
46+
$this->initialize();
47+
4348
// Check if secret is in cached array
4449
if (null !== ($value = parent::getEncryptedValue($key))) {
4550
return $value;

0 commit comments

Comments
 (0)