Skip to content

Commit a30f40a

Browse files
committed
Fixed ObjectManager usage and respective unit tests
1 parent cc599e5 commit a30f40a

File tree

2 files changed

+65
-39
lines changed

2 files changed

+65
-39
lines changed

app/code/Magento/Email/Model/Template/Filter.php

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,20 @@ class Filter extends \Magento\Framework\Filter\Template
165165
protected $configVariables;
166166

167167
/**
168-
* @var \Magento\Email\Model\Template\Css\Processor
168+
* @var Css\Processor
169169
*/
170170
private $cssProcessor;
171171

172172
/**
173-
* @var ReadInterface
173+
* @var Filesystem
174174
*/
175175
private $pubDirectory;
176176

177+
/**
178+
* @var \Magento\Framework\Filesystem\Directory\Read
179+
*/
180+
private $pubDirectoryRead;
181+
177182
/**
178183
* @param \Magento\Framework\Stdlib\StringUtils $string
179184
* @param \Psr\Log\LoggerInterface $logger
@@ -190,7 +195,8 @@ class Filter extends \Magento\Framework\Filter\Template
190195
* @param \Magento\Variable\Model\Source\Variables $configVariables
191196
* @param array $variables
192197
* @param \Magento\Framework\Css\PreProcessor\Adapter\CssInliner|null $cssInliner
193-
*
198+
* @param Css\Processor|null $cssProcessor
199+
* @param Filesystem|null $pubDirectory
194200
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
195201
*/
196202
public function __construct(
@@ -208,7 +214,9 @@ public function __construct(
208214
\Pelago\Emogrifier $emogrifier,
209215
\Magento\Variable\Model\Source\Variables $configVariables,
210216
$variables = [],
211-
\Magento\Framework\Css\PreProcessor\Adapter\CssInliner $cssInliner = null
217+
\Magento\Framework\Css\PreProcessor\Adapter\CssInliner $cssInliner = null,
218+
Css\Processor $cssProcessor = null,
219+
Filesystem $pubDirectory = null
212220
) {
213221
$this->_escaper = $escaper;
214222
$this->_assetRepo = $assetRepo;
@@ -224,6 +232,10 @@ public function __construct(
224232
$this->emogrifier = $emogrifier;
225233
$this->cssInliner = $cssInliner ?: \Magento\Framework\App\ObjectManager::getInstance()
226234
->get(\Magento\Framework\Css\PreProcessor\Adapter\CssInliner::class);
235+
$this->cssProcessor = $cssProcessor ?: ObjectManager::getInstance()
236+
->get(Css\Processor::class);
237+
$this->pubDirectory = $pubDirectory ?: ObjectManager::getInstance()
238+
->get(Filesystem::class);
227239
$this->configVariables = $configVariables;
228240
parent::__construct($string, $variables);
229241
}
@@ -321,32 +333,14 @@ public function setDesignParams(array $designParams)
321333
}
322334

323335
/**
324-
* Get CSS processor
325-
*
326-
* @deprecated 100.1.2
327-
* @return Css\Processor
328-
*/
329-
private function getCssProcessor()
330-
{
331-
if (!$this->cssProcessor) {
332-
$this->cssProcessor = ObjectManager::getInstance()->get(Css\Processor::class);
333-
}
334-
return $this->cssProcessor;
335-
}
336-
337-
/**
338-
* Get pub directory
336+
* Sets pub directory
339337
*
340-
* @deprecated 100.1.2
341338
* @param string $dirType
342-
* @return ReadInterface
339+
* @return void
343340
*/
344-
private function getPubDirectory($dirType)
341+
private function setPubDirectory($dirType)
345342
{
346-
if (!$this->pubDirectory) {
347-
$this->pubDirectory = ObjectManager::getInstance()->get(Filesystem::class)->getDirectoryRead($dirType);
348-
}
349-
return $this->pubDirectory;
343+
$this->pubDirectoryRead = $this->pubDirectory->getDirectoryRead($dirType);
350344
}
351345

352346
/**
@@ -844,7 +838,7 @@ public function cssDirective($construction)
844838
return '/* ' . __('"file" parameter must be specified') . ' */';
845839
}
846840

847-
$css = $this->getCssProcessor()->process(
841+
$css = $this->cssProcessor->process(
848842
$this->getCssFilesContent([$params['file']])
849843
);
850844

@@ -947,9 +941,9 @@ public function getCssFilesContent(array $files)
947941
try {
948942
foreach ($files as $file) {
949943
$asset = $this->_assetRepo->createAsset($file, $designParams);
950-
$pubDirectory = $this->getPubDirectory($asset->getContext()->getBaseDirType());
951-
if ($pubDirectory->isExist($asset->getPath())) {
952-
$css .= $pubDirectory->readFile($asset->getPath());
944+
$this->setPubDirectory($asset->getContext()->getBaseDirType());
945+
if ($this->pubDirectoryRead->isExist($asset->getPath())) {
946+
$css .= $this->pubDirectoryRead->readFile($asset->getPath());
953947
} else {
954948
$css .= $asset->getContent();
955949
}
@@ -979,7 +973,7 @@ public function applyInlineCss($html)
979973
$cssToInline = $this->getCssFilesContent(
980974
$this->getInlineCssFiles()
981975
);
982-
$cssToInline = $this->getCssProcessor()->process($cssToInline);
976+
$cssToInline = $this->cssProcessor->process($cssToInline);
983977

984978
// Only run Emogrify if HTML and CSS contain content
985979
if ($html && $cssToInline) {

app/code/Magento/Email/Test/Unit/Model/Template/FilterTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Email\Test\Unit\Model\Template;
710

811
use Magento\Email\Model\Template\Css\Processor;
@@ -14,6 +17,7 @@
1417

1518
/**
1619
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+
* @SuppressWarnings(PHPMD.TooManyFields)
1721
*/
1822
class FilterTest extends \PHPUnit\Framework\TestCase
1923
{
@@ -92,6 +96,21 @@ class FilterTest extends \PHPUnit\Framework\TestCase
9296
*/
9397
private $cssInliner;
9498

99+
/**
100+
* @var \PHPUnit\Framework\MockObject\MockObject|\Magento\Email\Model\Template\Css\Processor
101+
*/
102+
private $cssProcessor;
103+
104+
/**
105+
* @var \PHPUnit\Framework\MockObject\MockObject|\Magento\Framework\Filesystem
106+
*/
107+
private $pubDirectory;
108+
109+
/**
110+
* @var \PHPUnit\Framework\MockObject\MockObject|\Magento\Framework\Filesystem\Directory\Read
111+
*/
112+
private $pubDirectoryRead;
113+
95114
protected function setUp()
96115
{
97116
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -147,6 +166,18 @@ protected function setUp()
147166
$this->cssInliner = $this->objectManager->getObject(
148167
\Magento\Framework\Css\PreProcessor\Adapter\CssInliner::class
149168
);
169+
170+
$this->cssProcessor = $this->getMockBuilder(\Magento\Email\Model\Template\Css\Processor::class)
171+
->disableOriginalConstructor()
172+
->getMock();
173+
174+
$this->pubDirectory = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
175+
->disableOriginalConstructor()
176+
->getMock();
177+
178+
$this->pubDirectoryRead = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\Read::class)
179+
->disableOriginalConstructor()
180+
->getMock();
150181
}
151182

152183
/**
@@ -173,6 +204,8 @@ protected function getModel($mockedMethods = null)
173204
$this->configVariables,
174205
[],
175206
$this->cssInliner,
207+
$this->cssProcessor,
208+
$this->pubDirectory
176209
]
177210
)
178211
->setMethods($mockedMethods)
@@ -329,17 +362,16 @@ public function testGetCssFilesContent()
329362
->with($file, $designParams)
330363
->willReturn($asset);
331364

332-
$pubDirectory = $this->getMockBuilder(ReadInterface::class)
333-
->getMockForAbstractClass();
334-
$reflectionClass = new \ReflectionClass(Filter::class);
335-
$reflectionProperty = $reflectionClass->getProperty('pubDirectory');
336-
$reflectionProperty->setAccessible(true);
337-
$reflectionProperty->setValue($filter, $pubDirectory);
338-
$pubDirectory->expects($this->once())
365+
$this->pubDirectory
366+
->expects($this->once())
367+
->method('getDirectoryRead')
368+
->willReturn($this->pubDirectoryRead);
369+
370+
$this->pubDirectoryRead->expects($this->once())
339371
->method('isExist')
340372
->with($path . DIRECTORY_SEPARATOR . $file)
341373
->willReturn(true);
342-
$pubDirectory->expects($this->once())
374+
$this->pubDirectoryRead->expects($this->once())
343375
->method('readFile')
344376
->with($path . DIRECTORY_SEPARATOR . $file)
345377
->willReturn($css);

0 commit comments

Comments
 (0)