Skip to content
Open
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
@@ -1,7 +1,7 @@
<?php
/**
* Copyright 2021 Adobe
* All Rights Reserved.
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

Expand All @@ -15,6 +15,7 @@
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
use Magento\TestFramework\ScopeSwitcherInterface;
use Throwable;

/**
* Apply and revert data fixtures
Expand Down Expand Up @@ -49,11 +50,19 @@ public function apply(array $fixture): ?DataObject
$fromScope = $this->scopeSwitcher->switch($scope);
try {
$result = $factory->apply($data);
} catch (Throwable $exception) {
echo get_class($exception).' in '.get_class($factory).': '.$exception->getMessage();
$result = null;
} finally {
$this->scopeSwitcher->switch($fromScope);
}
} else {
$result = $factory->apply($data);
try {
$result = $factory->apply($data);
} catch (Throwable $exception) {
echo get_class($exception).' in '.get_class($factory).': '.$exception->getMessage();
$result = null;
}
}

if ($result !== null && !empty($fixture['name'])) {
Expand Down Expand Up @@ -136,94 +145,16 @@ private function resolveVariables(array $data): array
*/
private function parseFixtureKeyValue(string $data)
{
// Check if entire string is a single placeholder
if (preg_match('/^\$\w+(\.\w+)?\$$/', $data)) {
return $this->resolveSinglePlaceholder($data);
}
list($fixtureName, $attribute) = array_pad(explode('.', trim($data, '$')), 2, null);
$fixtureData = DataFixtureStorageManager::getStorage()->get($fixtureName);
if (!$fixtureData) {
throw new \InvalidArgumentException("Unable to resolve fixture reference '$data'");
}

// Check if string contains one or more placeholders, for multi value support
if (preg_match('/\$\w+(\.\w+)?\$/', $data)) {
return $this->resolveMultiplePlaceholders($data);
return $attribute ? $fixtureData->getDataUsingMethod($attribute) : $fixtureData;
}

return false;
}

/**
* Resolve a single fixture placeholder
*
* @param string $data
* @return DataObject|mixed
* @throws \InvalidArgumentException
*/
private function resolveSinglePlaceholder(string $data)
{
list($fixtureName, $attribute) = array_pad(explode('.', trim($data, '$')), 2, null);
$fixtureData = $this->getFixtureData($fixtureName, $data);
return $this->extractValue($fixtureData, $attribute);
}

/**
* Resolve multiple fixture placeholders in a string
*
* @param string $data
* @return string|false
*/
private function resolveMultiplePlaceholders(string $data)
{
$resolved = preg_replace_callback(
'/\$(\w+)(\.\w+)?\$/',
function ($matches) {
return $this->replacePlaceholder($matches);
},
$data
);
return $resolved !== $data ? $resolved : false;
}

/**
* Replace a single placeholder match
*
* @param array $matches
* @return string|mixed
* @throws \InvalidArgumentException
*/
private function replacePlaceholder(array $matches)
{
$fixtureName = $matches[1];
$attribute = isset($matches[2]) ? ltrim($matches[2], '.') : null;
$reference = "\${$fixtureName}" . ($attribute ? ".{$attribute}" : '') . "\$";
$fixtureData = $this->getFixtureData($fixtureName, $reference);
$value = $this->extractValue($fixtureData, $attribute);
return is_scalar($value) ? (string)$value : $value;
}

/**
* Get fixture data from storage
*
* @param string $fixtureName
* @param string $reference
* @return DataObject
* @throws \InvalidArgumentException
*/
private function getFixtureData(string $fixtureName, string $reference): DataObject
{
$fixtureData = DataFixtureStorageManager::getStorage()->get($fixtureName);
if (!$fixtureData) {
throw new \InvalidArgumentException("Unable to resolve fixture reference '{$reference}'");
}
return $fixtureData;
}

/**
* Extract value from fixture data
*
* @param DataObject $fixtureData
* @param string|null $attribute
* @return DataObject|mixed
*/
private function extractValue(DataObject $fixtureData, ?string $attribute)
{
return $attribute ? $fixtureData->getDataUsingMethod($attribute) : $fixtureData;
}
}