Skip to content

Commit 2b56594

Browse files
committed
MAGETWO-69036: Lazy-loaders cause fatal errors in production mode on cloud
1 parent bb60ab8 commit 2b56594

File tree

27 files changed

+513
-404
lines changed

27 files changed

+513
-404
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Helper
100100
* @param ProductRepository $productRepository
101101
* @param CustomOptionFactory $customOptionFactory
102102
* @param DateTime $dateTimeFilter
103+
* @param LinkResolver $linkResolver
103104
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
104105
*/
105106
public function __construct(
@@ -113,7 +114,8 @@ public function __construct(
113114
CustomOptionFactory $customOptionFactory = null,
114115
ProductLinkFactory $productLinkFactory = null,
115116
ProductRepository $productRepository = null,
116-
DateTime $dateTimeFilter = null
117+
DateTime $dateTimeFilter = null,
118+
LinkResolver $linkResolver = null
117119
) {
118120
if (null === $linkTypeProvider) {
119121
$linkTypeProvider = ObjectManager::getInstance()->get(LinkTypeProvider::class);
@@ -130,6 +132,9 @@ public function __construct(
130132
if (null === $dateTimeFilter) {
131133
$dateTimeFilter = ObjectManager::getInstance()->get(DateTime::class);
132134
}
135+
if (null === $linkResolver) {
136+
$linkResolver = ObjectManager::getInstance()->get(LinkResolver::class);
137+
}
133138
$this->request = $request;
134139
$this->storeManager = $storeManager;
135140
$this->stockFilter = $stockFilter;
@@ -141,6 +146,7 @@ public function __construct(
141146
$this->productLinkFactory = $productLinkFactory;
142147
$this->productRepository = $productRepository;
143148
$this->dateTimeFilter = $dateTimeFilter;
149+
$this->linkResolver = $linkResolver;
144150
}
145151

146152
/**
@@ -251,8 +257,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
251257
return empty($valueData['is_delete']);
252258
});
253259
}
254-
255-
$customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
260+
$customOption = $this->customOptionFactory->create(['data' => $customOptionData]);
256261
$customOption->setProductSku($product->getSku());
257262
$customOptions[] = $customOption;
258263
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter;
1212
use Magento\Catalog\Model\Product;
1313
use Magento\Catalog\Model\Product\Option;
14-
use Magento\Catalog\Model\ProductRepository;
14+
use Magento\Catalog\Api\ProductRepositoryInterface\Proxy;
1515
use Magento\Framework\App\RequestInterface;
1616
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1717
use Magento\Store\Api\Data\StoreInterface;
@@ -124,8 +124,8 @@ protected function setUp()
124124
->disableOriginalConstructor()
125125
->setMethods(['create'])
126126
->getMock();
127-
$this->productRepositoryMock = $this->getMockBuilder(ProductRepository::class)
128-
->disableOriginalConstructor()
127+
$this->productRepositoryMock = $this->getMockBuilder(Proxy::class)
128+
->setMethods(['getById'])
129129
->getMock();
130130
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
131131
->setMethods(['getPost'])
@@ -704,7 +704,7 @@ private function assembleProductRepositoryMock($links)
704704
->willReturn($link['sku']);
705705

706706
// Even optional arguments need to be provided for returnMapValue
707-
$repositoryReturnMap[] = [$link['id'], false, null, false, $mockLinkedProduct];
707+
$repositoryReturnMap[] = [$link['id'], $mockLinkedProduct];
708708
}
709709
}
710710

app/code/Magento/Catalog/Test/Unit/Model/Product/Option/RepositoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ protected function setUp()
9595
$this->optionRepository = new Repository(
9696
$this->productRepositoryMock,
9797
$this->optionResourceMock,
98-
$this->converterMock
98+
$this->converterMock,
99+
$optionFactory,
100+
$this->optionCollectionFactory
99101
);
100102

101103
$this->setProperties(
102104
$this->optionRepository,
103105
[
104-
'optionFactory' => $optionFactory,
105-
'collectionFactory' => $this->optionCollectionFactory,
106106
'metadataPool' => $metadataPool
107107
]
108108
);

app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected function setUp()
146146
$this->shippingAssignmentFactoryMock =
147147
$this->getMock(\Magento\Quote\Model\ShippingAssignmentFactory::class, ['create'], [], '', false);
148148
$this->cartExtensionFactoryMock =
149-
$this->getMock(\Magento\Quote\Api\Data\CartExtensionInterfaceFactory::class, ['create'], [], '', false);
149+
$this->getMock(\Magento\Quote\Api\Data\CartExtensionFactory::class, ['create'], [], '', false);
150150
$this->shippingFactoryMock =
151151
$this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false);
152152

app/code/Magento/ConfigurableProduct/Model/LinkManagement.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
namespace Magento\ConfigurableProduct\Model;
88

9-
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
109
use Magento\Framework\App\ObjectManager;
1110
use Magento\Framework\Exception\InputException;
1211
use Magento\Framework\Exception\StateException;
@@ -40,7 +39,7 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
4039
private $optionsFactory;
4140

4241
/**
43-
* @var AttributeFactory
42+
* @var \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory
4443
*/
4544
private $attributeFactory;
4645

@@ -49,24 +48,34 @@ class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementI
4948
* @param \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory
5049
* @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableType
5150
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
52-
* @param AttributeFactory|null $attributeFactory
51+
* @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory|null $attributeFactory
52+
* @param \Magento\ConfigurableProduct\Helper\Product\Options\Factory|null $optionsFactory
5353
* @throws \RuntimeException
5454
*/
5555
public function __construct(
5656
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
5757
\Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
5858
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableType,
5959
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
60-
AttributeFactory $attributeFactory = null
60+
\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory = null,
61+
\Magento\ConfigurableProduct\Helper\Product\Options\Factory $optionsFactory = null
6162
) {
6263
$this->productRepository = $productRepository;
6364
$this->productFactory = $productFactory;
6465
$this->configurableType = $configurableType;
6566
$this->dataObjectHelper = $dataObjectHelper;
66-
if (null !== $attributeFactory) {
67-
$attributeFactory = ObjectManager::getInstance()->get(AttributeFactory::class);
67+
if (null === $attributeFactory) {
68+
$attributeFactory = ObjectManager::getInstance()->get(
69+
\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory::class
70+
);
6871
}
6972
$this->attributeFactory = $attributeFactory;
73+
if (null === $optionsFactory) {
74+
$optionsFactory = ObjectManager::getInstance()->get(
75+
\Magento\ConfigurableProduct\Helper\Product\Options\Factory::class
76+
);
77+
}
78+
$this->optionsFactory = $optionsFactory;
7079
}
7180

7281
/**
@@ -136,9 +145,7 @@ public function addChild($sku, $childSku)
136145
}
137146
$configurableOptionData = $this->getConfigurableAttributesData($attributeIds);
138147

139-
/** @var \Magento\ConfigurableProduct\Helper\Product\Options\Factory $optionFactory */
140-
$optionFactory = $this->getOptionsFactory();
141-
$options = $optionFactory->create($configurableOptionData);
148+
$options = $this->optionsFactory->create($configurableOptionData);
142149
$childrenIds[] = $child->getId();
143150
$product->getExtensionAttributes()->setConfigurableProductOptions($options);
144151
$product->getExtensionAttributes()->setConfigurableProductLinks($childrenIds);
@@ -175,22 +182,6 @@ public function removeChild($sku, $childSku)
175182
return true;
176183
}
177184

178-
/**
179-
* Get Options Factory
180-
*
181-
* @return \Magento\ConfigurableProduct\Helper\Product\Options\Factory
182-
*
183-
* @deprecated
184-
*/
185-
private function getOptionsFactory()
186-
{
187-
if (!$this->optionsFactory) {
188-
$this->optionsFactory = ObjectManager::getInstance()
189-
->get(\Magento\ConfigurableProduct\Helper\Product\Options\Factory::class);
190-
}
191-
return $this->optionsFactory;
192-
}
193-
194185
/**
195186
* Get Configurable Attribute Data
196187
*

app/code/Magento/ConfigurableProduct/Test/Unit/Model/LinkManagementTest.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class LinkManagementTest extends \PHPUnit_Framework_TestCase
3636
*/
3737
protected $object;
3838

39+
/** \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory|\PHPUnit_Framework_MockObject_MockObject */
40+
private $attributeFactory;
41+
42+
/** \Magento\ConfigurableProduct\Helper\Product\Options\Factory|\PHPUnit_Framework_MockObject_MockObject */
43+
private $productOptionsFactory;
44+
3945
/**
4046
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\DataObjectHelper
4147
*/
@@ -56,9 +62,16 @@ protected function setUp()
5662
->disableOriginalConstructor()
5763
->getMock();
5864

59-
$this->configurableType =
60-
$this->getMockBuilder('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')
61-
->disableOriginalConstructor()->getMock();
65+
$this->configurableType = $this->getMockBuilder(
66+
'\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable'
67+
)->disableOriginalConstructor()->getMock();
68+
69+
$this->attributeFactory = $this->getMockBuilder(
70+
'\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory'
71+
)->disableOriginalConstructor()->setMethods(['create'])->getMock();
72+
$this->productOptionsFactory = $this->getMockBuilder(
73+
'\Magento\ConfigurableProduct\Helper\Product\Options\Factory'
74+
)->disableOriginalConstructor()->setMethods(['create'])->getMock();
6275

6376
$this->object = $this->objectManagerHelper->getObject(
6477
'\Magento\ConfigurableProduct\Model\LinkManagement',
@@ -67,6 +80,8 @@ protected function setUp()
6780
'productFactory' => $this->productFactory,
6881
'configurableType' => $this->configurableType,
6982
'dataObjectHelper' => $this->dataObjectHelperMock,
83+
'attributeFactory' => $this->attributeFactory,
84+
'optionsFactory' => $this->productOptionsFactory
7085
]
7186
);
7287
}
@@ -164,22 +179,13 @@ public function testAddChild()
164179
->disableOriginalConstructor()
165180
->setMethods(['getAttributeCode'])
166181
->getMock();
167-
$optionsFactoryMock = $this->getMockBuilder('Magento\ConfigurableProduct\Helper\Product\Options\Factory')
168-
->disableOriginalConstructor()
169-
->setMethods(['create'])
170-
->getMock();
171182
$reflectionClass = new \ReflectionClass('Magento\ConfigurableProduct\Model\LinkManagement');
172183
$optionsFactoryReflectionProperty = $reflectionClass->getProperty('optionsFactory');
173184
$optionsFactoryReflectionProperty->setAccessible(true);
174-
$optionsFactoryReflectionProperty->setValue($this->object, $optionsFactoryMock);
175-
176-
$attributeFactoryMock = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory')
177-
->disableOriginalConstructor()
178-
->setMethods(['create'])
179-
->getMock();
185+
$optionsFactoryReflectionProperty->setValue($this->object, $this->productOptionsFactory);
180186
$attributeFactoryReflectionProperty = $reflectionClass->getProperty('attributeFactory');
181187
$attributeFactoryReflectionProperty->setAccessible(true);
182-
$attributeFactoryReflectionProperty->setValue($this->object, $attributeFactoryMock);
188+
$attributeFactoryReflectionProperty->setValue($this->object, $this->attributeFactory);
183189

184190
$attributeMock = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Eav\Attribute')
185191
->disableOriginalConstructor()
@@ -217,8 +223,8 @@ public function testAddChild()
217223
$simple->expects($this->any())->method('getData')->willReturn('color');
218224
$optionMock->expects($this->any())->method('getAttributeId')->willReturn('1');
219225

220-
$optionsFactoryMock->expects($this->any())->method('create')->willReturn([$optionMock]);
221-
$attributeFactoryMock->expects($this->any())->method('create')->willReturn($attributeMock);
226+
$this->productOptionsFactory->expects($this->any())->method('create')->willReturn([$optionMock]);
227+
$this->attributeFactory->expects($this->any())->method('create')->willReturn($attributeMock);
222228
$attributeMock->expects($this->any())->method('getCollection')->willReturn($attributeCollectionMock);
223229
$attributeCollectionMock->expects($this->any())->method('addFieldToFilter')->willReturnSelf();
224230
$attributeCollectionMock->expects($this->any())->method('getItems')->willReturn([$attributeMock]);

app/code/Magento/Customer/Model/Metadata/Form/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public function outputValue($format = \Magento\Customer\Model\Metadata\ElementFa
392392
protected function getFileProcessor()
393393
{
394394
if ($this->fileProcessor === null) {
395-
$this->fileProcessor = $this->getFileProcessorFactory()->create([
395+
$this->fileProcessor = $this->fileProcessorFactory->create([
396396
'entityTypeCode' => $this->_entityTypeCode,
397397
]);
398398
}

app/code/Magento/Customer/Model/Metadata/Form/Image.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ protected function processCustomerValue(array $value)
193193
{
194194
$temporaryFile = FileProcessor::TMP_DIR . '/' . ltrim($value['file'], '/');
195195

196-
if ($this->getFileProcessor()->isExist($temporaryFile)) {
197-
$base64EncodedData = $this->getFileProcessor()->getBase64EncodedData($temporaryFile);
196+
$fileProcessor = $this->fileProcessorFactory->create();
197+
if ($fileProcessor->isExist($temporaryFile)) {
198+
$base64EncodedData = $fileProcessor->getBase64EncodedData($temporaryFile);
198199

199200
/** @var ImageContentInterface $imageContentDataObject */
200201
$imageContentDataObject = $this->imageContentFactory->create()
@@ -203,7 +204,7 @@ protected function processCustomerValue(array $value)
203204
->setType($value['type']);
204205

205206
// Remove temporary file
206-
$this->getFileProcessor()->removeUploadedFile($temporaryFile);
207+
$fileProcessor->removeUploadedFile($temporaryFile);
207208

208209
return $imageContentDataObject;
209210
}

app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/FileTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class FileTest extends AbstractFormTestCase
3232
*/
3333
protected $uploaderFactoryMock;
3434

35+
/**
36+
* @var \Magento\Customer\Model\FileProcessorFactory|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $fileProcessorFactory;
39+
3540
/**
3641
* @var \Magento\Customer\Model\FileProcessor|\PHPUnit_Framework_MockObject_MockObject
3742
*/
@@ -50,7 +55,6 @@ protected function setUp()
5055
$this->requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http')
5156
->disableOriginalConstructor()->getMock();
5257
$this->uploaderFactoryMock = $this->getMock('Magento\Framework\File\UploaderFactory', [], [], '', false);
53-
5458
$this->fileProcessorMock = $this->getMockBuilder('Magento\Customer\Model\FileProcessor')
5559
->disableOriginalConstructor()
5660
->getMock();
@@ -59,7 +63,6 @@ protected function setUp()
5963
/**
6064
* @param array|bool $expected
6165
* @param string $attributeCode
62-
* @param bool $isAjax
6366
* @param string $delete
6467
* @dataProvider extractValueNoRequestScopeDataProvider
6568
*/
@@ -448,6 +451,9 @@ public function testOutputValueJson()
448451
*/
449452
private function initialize(array $data)
450453
{
454+
$this->fileProcessorFactory = $this->getMockBuilder(\Magento\Customer\Model\FileProcessorFactory::class)
455+
->disableOriginalConstructor()->setMethods(['create'])->getMock();
456+
$this->fileProcessorFactory->expects($this->any())->method('create')->willReturn($this->fileProcessorMock);
451457
$model = new \Magento\Customer\Model\Metadata\Form\File(
452458
$this->localeMock,
453459
$this->loggerMock,
@@ -459,14 +465,8 @@ private function initialize(array $data)
459465
$this->urlEncode,
460466
$this->fileValidatorMock,
461467
$this->fileSystemMock,
462-
$this->uploaderFactoryMock
463-
);
464-
465-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
466-
$objectManager->setBackwardCompatibleProperty(
467-
$model,
468-
'fileProcessor',
469-
$this->fileProcessorMock
468+
$this->uploaderFactoryMock,
469+
$this->fileProcessorFactory
470470
);
471471

472472
return $model;
@@ -514,7 +514,6 @@ public function testCompactValueRemoveUiComponentValue()
514514
'isAjax' => false,
515515
'entityTypeCode' => self::ENTITY_TYPE,
516516
]);
517-
518517
$this->fileProcessorMock->expects($this->once())
519518
->method('removeUploadedFile')
520519
->with($value)

0 commit comments

Comments
 (0)