Skip to content

Commit 3505340

Browse files
committed
ACP2E-2098: addressed CR comments
1 parent c7d9e81 commit 3505340

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

app/code/Magento/QuoteGraphQl/Plugin/ProductAttributesExtender.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ class ProductAttributesExtender
2626
*/
2727
private $attributeCollectionFactory;
2828

29-
/**
30-
* @var string
31-
*/
32-
private $fieldsHash = '';
33-
3429
/**
3530
* @var array
3631
*/
@@ -58,15 +53,11 @@ public function __construct(
5853
*/
5954
public function afterGetProductAttributes(QuoteConfig $subject, array $result): array
6055
{
61-
62-
$fieldsUsedInQuery = $this->fields->getFieldsUsedInQuery();
63-
$fieldsHash = hash('sha256', json_encode($fieldsUsedInQuery));
64-
if (!$this->fieldsHash || $this->fieldsHash !== $fieldsHash) {
65-
$this->fieldsHash = hash('sha256', json_encode($fieldsUsedInQuery));
56+
if (!$this->attributes) {
6657
$attributeCollection = $this->attributeCollectionFactory->create()
6758
->removeAllFieldsFromSelect()
6859
->addFieldToSelect('attribute_code')
69-
->setCodeFilter($fieldsUsedInQuery)
60+
->setCodeFilter($this->fields->getFieldsUsedInQuery())
7061
->load();
7162
$this->attributes = $attributeCollection->getColumnValues('attribute_code');
7263
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddProductsToCartWithVariablesTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ protected function setUp(): void
5757
*/
5858
#[
5959
DataFixture(AttributeFixture::class, ['is_visible_on_front' => true], as: 'attr'),
60-
DataFixture(ProductFixture::class, ['attribute_set_id' => 4], as: 'product'),
60+
DataFixture(ProductFixture::class, ['attribute_set_id' => 4, '$attr.attribute_code$' => 'default_value'], as: 'product'),
6161
DataFixture(GuestCartFixture::class, as: 'cart'),
6262
]
6363
public function testAddProductsToEmptyCartWithVariables(): void
6464
{
6565
$attribute = $this->fixtures->get('attr');
6666
$product = $this->fixtures->get('product');
67-
$product->setCustomAttribute($attribute->getAttributeCode(), 'default_value');
68-
$this->productRepository->save($product);
6967

7068
$this->cleanCache();
7169

dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureSetup.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\TestFramework\Annotation;
99

1010
use Magento\Framework\DataObject;
11+
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213
use Magento\Framework\Registry;
1314
use Magento\TestFramework\Fixture\DataFixtureFactory;
@@ -37,6 +38,7 @@ public function __construct(
3738
*
3839
* @param array $fixture
3940
* @return DataObject|null
41+
* @throws LocalizedException
4042
*/
4143
public function apply(array $fixture): ?DataObject
4244
{
@@ -96,25 +98,52 @@ public function revert(array $fixture): void
9698
*
9799
* @param array $data
98100
* @return array
99-
* @throws \Magento\Framework\Exception\LocalizedException
101+
* @throws LocalizedException
100102
*/
101103
private function resolveVariables(array $data): array
102104
{
103105
foreach ($data as $key => $value) {
104106
if (is_array($value)) {
105107
$data[$key] = $this->resolveVariables($value);
106108
} else {
107-
if (is_string($value) && preg_match('/^\$\w+(\.\w+)?\$$/', $value)) {
108-
list($fixtureName, $attribute) = array_pad(explode('.', trim($value, '$')), 2, null);
109-
$fixtureData = DataFixtureStorageManager::getStorage()->get($fixtureName);
110-
if (!$fixtureData) {
111-
throw new \InvalidArgumentException("Unable to resolve fixture reference '$value'");
109+
if (is_string($value)) {
110+
$value = $this->parseFixtureKeyValue($value);
111+
if ($value) {
112+
$data[$key] = $value;
112113
}
113-
$data[$key] = $attribute ? $fixtureData->getDataUsingMethod($attribute) : $fixtureData;
114+
}
115+
}
116+
117+
if (is_string($key)) {
118+
$newKey = $this->parseFixtureKeyValue($key);
119+
if (is_string($newKey)) {
120+
$value = $data[$key];
121+
unset($data[$key]);
122+
$data[$newKey] = $value;
114123
}
115124
}
116125
}
117126

118127
return $data;
119128
}
129+
130+
/**
131+
* Parse either key or value of the fixture data
132+
*
133+
* @param string $data
134+
* @return DataObject|mixed|void
135+
* @throws LocalizedException
136+
*/
137+
private function parseFixtureKeyValue(string $data)
138+
{
139+
if (preg_match('/^\$\w+(\.\w+)?\$$/', $data)) {
140+
list($fixtureName, $attribute) = array_pad(explode('.', trim($data, '$')), 2, null);
141+
$fixtureData = DataFixtureStorageManager::getStorage()->get($fixtureName);
142+
if (!$fixtureData) {
143+
throw new \InvalidArgumentException("Unable to resolve fixture reference '$data'");
144+
}
145+
return $attribute ? $fixtureData->getDataUsingMethod($attribute) : $fixtureData;
146+
}
147+
return false;
148+
}
120149
}

lib/internal/Magento/Framework/GraphQl/Test/Unit/Query/FieldsTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ class FieldsTest extends TestCase
2727

2828
protected function setUp(): void
2929
{
30-
$objectManager = new ObjectManager($this);
31-
$this->queryParser = $objectManager->getObject(
32-
QueryParser::class
33-
);
30+
$this->queryParser = new QueryParser();
3431

3532
$this->fields = new Fields(
3633
$this->queryParser

0 commit comments

Comments
 (0)