Skip to content

Commit af12f0f

Browse files
ENGCOM-6922: Swatches options: eliminate objects instantiation since only their data needed #26897
- Merge Pull Request #26897 from ilnytskyi/magento2:swatch-options-use-data - Merged commits: 1. eb81164 2. 6ed0d87 3. 023b240
2 parents a25c107 + 023b240 commit af12f0f

File tree

2 files changed

+37
-50
lines changed

2 files changed

+37
-50
lines changed

app/code/Magento/Swatches/Helper/Data.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Data
3535
const EMPTY_IMAGE_VALUE = 'no_selection';
3636

3737
/**
38-
* Default store ID
38+
* The int value of the Default store ID
3939
*/
4040
const DEFAULT_STORE_ID = 0;
4141

@@ -471,13 +471,13 @@ public function getSwatchesByOptionsId(array $optionIds)
471471
$swatches = [];
472472
$fallbackValues = [];
473473
$currentStoreId = $this->storeManager->getStore()->getId();
474-
foreach ($swatchCollection as $item) {
474+
foreach ($swatchCollection->getData() as $item) {
475475
if ($item['type'] != Swatch::SWATCH_TYPE_TEXTUAL) {
476-
$swatches[$item['option_id']] = $item->getData();
476+
$swatches[$item['option_id']] = $item;
477477
} elseif ($item['store_id'] == $currentStoreId && $item['value'] != '') {
478-
$fallbackValues[$item['option_id']][$currentStoreId] = $item->getData();
478+
$fallbackValues[$item['option_id']][$currentStoreId] = $item;
479479
} elseif ($item['store_id'] == self::DEFAULT_STORE_ID) {
480-
$fallbackValues[$item['option_id']][self::DEFAULT_STORE_ID] = $item->getData();
480+
$fallbackValues[$item['option_id']][self::DEFAULT_STORE_ID] = $item;
481481
}
482482
}
483483

app/code/Magento/Swatches/Test/Unit/Helper/DataTest.php

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,8 @@ public function dataForGettingSwatchAsArray()
669669

670670
public function testGetSwatchesByOptionsIdIf1()
671671
{
672-
$swatchMock = $this->createMock(\Magento\Swatches\Model\Swatch::class);
672+
//Simulate behaviour of \Magento\Swatches\Model\Swatch as array item
673+
$swatchMock = $this->createMock(\ArrayAccess::class);
673674

674675
$optionsData = [
675676
[
@@ -692,22 +693,18 @@ public function testGetSwatchesByOptionsIdIf1()
692693
->willReturn($optionsData[0]['type']);
693694
$swatchMock->expects($this->at(1))->method('offsetGet')->with('option_id')
694695
->willReturn($optionsData[0]['option_id']);
695-
$swatchMock->expects($this->at(2))->method('getData')->with('')
696-
->willReturn($optionsData[0]);
697-
$swatchMock->expects($this->at(3))->method('offsetGet')->with('type')
696+
$swatchMock->expects($this->at(2))->method('offsetGet')->with('type')
698697
->willReturn($optionsData[1]['type']);
699-
$swatchMock->expects($this->at(4))->method('offsetGet')->with('store_id')
698+
$swatchMock->expects($this->at(3))->method('offsetGet')->with('store_id')
700699
->willReturn($optionsData[1]['store_id']);
701-
$swatchMock->expects($this->at(5))->method('offsetGet')->with('store_id')
700+
$swatchMock->expects($this->at(4))->method('offsetGet')->with('store_id')
702701
->willReturn($optionsData[1]['store_id']);
703-
$swatchMock->expects($this->at(6))->method('offsetGet')->with('option_id')
702+
$swatchMock->expects($this->at(5))->method('offsetGet')->with('option_id')
704703
->willReturn($optionsData[1]['option_id']);
705-
$swatchMock->expects($this->at(7))->method('getData')->with('')
706-
->willReturn($optionsData[1]);
707704

708-
$swatchCollectionMock = $this->objectManager
709-
->getCollectionMock(Collection::class, [$swatchMock, $swatchMock]);
705+
$swatchCollectionMock = $this->createMock(Collection::class);
710706
$swatchCollectionMock->method('addFilterByOptionsIds')->with([35])->will($this->returnSelf());
707+
$swatchCollectionMock->expects($this->once())->method('getData')->willReturn([$swatchMock, $swatchMock]);
711708
$this->swatchCollectionFactoryMock->method('create')->willReturn($swatchCollectionMock);
712709

713710
$storeMock = $this->createMock(\Magento\Store\Model\Store::class);
@@ -719,7 +716,8 @@ public function testGetSwatchesByOptionsIdIf1()
719716

720717
public function testGetSwatchesByOptionsIdIf2()
721718
{
722-
$swatchMock = $this->createMock(\Magento\Swatches\Model\Swatch::class);
719+
//Simulate behaviour of \Magento\Swatches\Model\Swatch as array item
720+
$swatchMock = $this->createMock(\ArrayAccess::class);
723721

724722
$optionsData = [
725723
[
@@ -737,28 +735,21 @@ public function testGetSwatchesByOptionsIdIf2()
737735
'id' => 488,
738736
]
739737
];
740-
741-
$swatchMock->expects($this->at(0))->method('offsetGet')->with('type')->willReturn(0);
742-
$swatchMock->expects($this->at(1))->method('offsetGet')->with('store_id')->willReturn(1);
743-
$swatchMock->expects($this->at(2))->method('offsetGet')->with('value')->willReturn('test');
744-
$swatchMock->expects($this->at(3))->method('offsetGet')->with('option_id')->willReturn(35);
745-
$swatchMock->expects($this->at(4))->method('getData')->with('')->willReturn($optionsData[0]);
746-
$swatchMock->expects($this->at(5))->method('offsetGet')->with('type')->willReturn(0);
747-
$swatchMock->expects($this->at(6))->method('offsetGet')->with('store_id')->willReturn(1);
748-
$swatchMock->expects($this->at(7))->method('offsetGet')->with('value')->willReturn('test2');
749-
$swatchMock->expects($this->at(8))->method('offsetGet')->with('option_id')->willReturn(36);
750-
$swatchMock->expects($this->at(9))->method('getData')->with('')->willReturn($optionsData[1]);
751-
752-
$swatchCollectionMock = $this->objectManager->getCollectionMock(
753-
Collection::class,
754-
[
755-
$swatchMock,
756-
$swatchMock,
757-
]
758-
);
738+
// @codingStandardsIgnoreStart
739+
$swatchMock->expects($this->at(0))->method('offsetGet')->with('type')->willReturn($optionsData[0]['type']);
740+
$swatchMock->expects($this->at(1))->method('offsetGet')->with('store_id')->willReturn($optionsData[0]['store_id']);
741+
$swatchMock->expects($this->at(2))->method('offsetGet')->with('value')->willReturn($optionsData[0]['value']);
742+
$swatchMock->expects($this->at(3))->method('offsetGet')->with('option_id')->willReturn($optionsData[0]['option_id']);
743+
$swatchMock->expects($this->at(4))->method('offsetGet')->with('type')->willReturn($optionsData[1]['type']);
744+
$swatchMock->expects($this->at(5))->method('offsetGet')->with('store_id')->willReturn($optionsData[1]['store_id']);
745+
$swatchMock->expects($this->at(6))->method('offsetGet')->with('value')->willReturn($optionsData[1]['value']);
746+
$swatchMock->expects($this->at(7))->method('offsetGet')->with('option_id')->willReturn($optionsData[1]['option_id']);
747+
// @codingStandardsIgnoreEnd
748+
$swatchCollectionMock = $this->createMock(Collection::class);
759749
$this->swatchCollectionFactoryMock->method('create')->willReturn($swatchCollectionMock);
760750

761751
$swatchCollectionMock->method('addFilterByOptionsIds')->with([35])->will($this->returnSelf());
752+
$swatchCollectionMock->expects($this->once())->method('getData')->willReturn([$swatchMock, $swatchMock]);
762753

763754
$storeMock = $this->createMock(\Magento\Store\Model\Store::class);
764755
$this->storeManagerMock->method('getStore')->willReturn($storeMock);
@@ -769,7 +760,8 @@ public function testGetSwatchesByOptionsIdIf2()
769760

770761
public function testGetSwatchesByOptionsIdIf3()
771762
{
772-
$swatchMock = $this->createMock(\Magento\Swatches\Model\Swatch::class);
763+
//Simulate behaviour of \Magento\Swatches\Model\Swatch as array item
764+
$swatchMock = $this->createMock(\ArrayAccess::class);
773765

774766
$optionsData = [
775767
'type' => 0,
@@ -778,22 +770,17 @@ public function testGetSwatchesByOptionsIdIf3()
778770
'option_id' => 35,
779771
'id' => 423,
780772
];
781-
782-
$swatchMock->expects($this->at(0))->method('offsetGet')->with('type')->willReturn(0);
783-
$swatchMock->expects($this->at(1))->method('offsetGet')->with('store_id')->willReturn(0);
784-
$swatchMock->expects($this->at(2))->method('offsetGet')->with('store_id')->willReturn(0);
785-
$swatchMock->expects($this->at(3))->method('offsetGet')->with('option_id')->willReturn(35);
786-
$swatchMock->expects($this->at(4))->method('getData')->with('')->willReturn($optionsData);
787-
788-
$swatchCollectionMock = $this->objectManager->getCollectionMock(
789-
Collection::class,
790-
[
791-
$swatchMock,
792-
]
793-
);
773+
// @codingStandardsIgnoreStart
774+
$swatchMock->expects($this->at(0))->method('offsetGet')->with('type')->willReturn($optionsData['type']);
775+
$swatchMock->expects($this->at(1))->method('offsetGet')->with('store_id')->willReturn($optionsData['store_id']);
776+
$swatchMock->expects($this->at(2))->method('offsetGet')->with('store_id')->willReturn($optionsData['store_id']);
777+
$swatchMock->expects($this->at(3))->method('offsetGet')->with('option_id')->willReturn($optionsData['option_id']);
778+
// @codingStandardsIgnoreEnd
779+
$swatchCollectionMock = $this->createMock(Collection::class);
794780
$this->swatchCollectionFactoryMock->method('create')->willReturn($swatchCollectionMock);
795781

796782
$swatchCollectionMock->method('addFilterByOptionsIds')->with([35])->will($this->returnSelf());
783+
$swatchCollectionMock->expects($this->once())->method('getData')->willReturn([$swatchMock]);
797784

798785
$storeMock = $this->createMock(\Magento\Store\Model\Store::class);
799786
$this->storeManagerMock->method('getStore')->willReturn($storeMock);

0 commit comments

Comments
 (0)