Skip to content

33295: Eliminate AspectMock from FileStorageTest #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,30 @@ class FileStorage extends BaseStorage
private $secretData = [];

/**
* FileStorage constructor
* Initialize secret data value which represents encrypted credentials
*
* @return void
* @throws TestFrameworkException
*/
public function __construct()
public function initialize(): void
{
parent::__construct();
$creds = $this->readInCredentialsFile();
$this->secretData = $this->encryptCredFileContents($creds);
if (!$this->secretData) {
$creds = $this->readInCredentialsFile();
$this->secretData = $this->encryptCredFileContents($creds);
}
}

/**
* Returns the value of a secret based on corresponding key
*
* @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;
Expand Down