33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
7+ declare (strict_types=1 );
8+
69namespace Magento \Email \Test \Unit \Model \Template ;
710
811use Magento \Email \Model \Template \Css \Processor ;
1417
1518/**
1619 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+ * @SuppressWarnings(PHPMD.TooManyFields)
1721 */
1822class FilterTest extends \PHPUnit \Framework \TestCase
1923{
@@ -92,6 +96,31 @@ 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+
114+ /**
115+ * @var \PHPUnit\Framework\MockObject\MockObject|\Magento\Framework\Filter\VariableResolver\StrategyResolver
116+ */
117+ private $ variableResolver ;
118+
119+ /**
120+ * @var array
121+ */
122+ private $ directiveProcessors ;
123+
95124 protected function setUp ()
96125 {
97126 $ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -147,6 +176,41 @@ protected function setUp()
147176 $ this ->cssInliner = $ this ->objectManager ->getObject (
148177 \Magento \Framework \Css \PreProcessor \Adapter \CssInliner::class
149178 );
179+
180+ $ this ->cssProcessor = $ this ->getMockBuilder (\Magento \Email \Model \Template \Css \Processor::class)
181+ ->disableOriginalConstructor ()
182+ ->getMock ();
183+
184+ $ this ->pubDirectory = $ this ->getMockBuilder (\Magento \Framework \Filesystem::class)
185+ ->disableOriginalConstructor ()
186+ ->getMock ();
187+
188+ $ this ->pubDirectoryRead = $ this ->getMockBuilder (\Magento \Framework \Filesystem \Directory \Read::class)
189+ ->disableOriginalConstructor ()
190+ ->getMock ();
191+ $ this ->variableResolver =
192+ $ this ->getMockBuilder (\Magento \Framework \Filter \VariableResolver \StrategyResolver::class)
193+ ->disableOriginalConstructor ()
194+ ->getMock ();
195+
196+ $ this ->directiveProcessors = [
197+ 'depend ' =>
198+ $ this ->getMockBuilder (\Magento \Framework \Filter \DirectiveProcessor \DependDirective::class)
199+ ->disableOriginalConstructor ()
200+ ->getMock (),
201+ 'if ' =>
202+ $ this ->getMockBuilder (\Magento \Framework \Filter \DirectiveProcessor \IfDirective::class)
203+ ->disableOriginalConstructor ()
204+ ->getMock (),
205+ 'template ' =>
206+ $ this ->getMockBuilder (\Magento \Framework \Filter \DirectiveProcessor \TemplateDirective::class)
207+ ->disableOriginalConstructor ()
208+ ->getMock (),
209+ 'legacy ' =>
210+ $ this ->getMockBuilder (\Magento \Framework \Filter \DirectiveProcessor \LegacyDirective::class)
211+ ->disableOriginalConstructor ()
212+ ->getMock (),
213+ ];
150214 }
151215
152216 /**
@@ -173,6 +237,10 @@ protected function getModel($mockedMethods = null)
173237 $ this ->configVariables ,
174238 [],
175239 $ this ->cssInliner ,
240+ $ this ->directiveProcessors ,
241+ $ this ->variableResolver ,
242+ $ this ->cssProcessor ,
243+ $ this ->pubDirectory
176244 ]
177245 )
178246 ->setMethods ($ mockedMethods )
@@ -252,17 +320,16 @@ public function testGetCssFilesContent()
252320 ->with ($ file , $ designParams )
253321 ->willReturn ($ asset );
254322
255- $ pubDirectory = $ this ->getMockBuilder (ReadInterface::class)
256- ->getMockForAbstractClass ();
257- $ reflectionClass = new \ReflectionClass (Filter::class);
258- $ reflectionProperty = $ reflectionClass ->getProperty ('pubDirectory ' );
259- $ reflectionProperty ->setAccessible (true );
260- $ reflectionProperty ->setValue ($ filter , $ pubDirectory );
261- $ pubDirectory ->expects ($ this ->once ())
323+ $ this ->pubDirectory
324+ ->expects ($ this ->once ())
325+ ->method ('getDirectoryRead ' )
326+ ->willReturn ($ this ->pubDirectoryRead );
327+
328+ $ this ->pubDirectoryRead ->expects ($ this ->once ())
262329 ->method ('isExist ' )
263330 ->with ($ path . DIRECTORY_SEPARATOR . $ file )
264331 ->willReturn (true );
265- $ pubDirectory ->expects ($ this ->once ())
332+ $ this -> pubDirectoryRead ->expects ($ this ->once ())
266333 ->method ('readFile ' )
267334 ->with ($ path . DIRECTORY_SEPARATOR . $ file )
268335 ->willReturn ($ css );
@@ -319,43 +386,6 @@ public function testApplyInlineCssThrowsExceptionWhenDesignParamsNotSet()
319386 $ filter ->applyInlineCss ('test ' );
320387 }
321388
322- /**
323- * Ensure that after filter callbacks are reset after exception is thrown during filtering
324- */
325- public function testAfterFilterCallbackGetsResetWhenExceptionTriggered ()
326- {
327- $ value = '{{var random_var}} ' ;
328- $ exception = new \Exception ('Test exception ' );
329- $ exceptionResult = sprintf (__ ('Error filtering template: %s ' ), $ exception ->getMessage ());
330-
331- $ this ->appState ->expects ($ this ->once ())
332- ->method ('getMode ' )
333- ->will ($ this ->returnValue (\Magento \Framework \App \State::MODE_DEVELOPER ));
334- $ this ->logger ->expects ($ this ->once ())
335- ->method ('critical ' )
336- ->with ($ exception );
337-
338- $ filter = $ this ->getModel (['varDirective ' , 'resetAfterFilterCallbacks ' ]);
339- $ filter ->expects ($ this ->once ())
340- ->method ('varDirective ' )
341- ->will ($ this ->throwException ($ exception ));
342-
343- // Callbacks must be reset after exception is thrown
344- $ filter ->expects ($ this ->once ())
345- ->method ('resetAfterFilterCallbacks ' );
346-
347- // Build arbitrary object to pass into the addAfterFilterCallback method
348- $ callbackObject = $ this ->getMockBuilder ('stdObject ' )
349- ->setMethods (['afterFilterCallbackMethod ' ])
350- ->getMock ();
351- // Callback should never run due to exception happening during filtering
352- $ callbackObject ->expects ($ this ->never ())
353- ->method ('afterFilterCallbackMethod ' );
354- $ filter ->addAfterFilterCallback ([$ callbackObject , 'afterFilterCallbackMethod ' ]);
355-
356- $ this ->assertEquals ($ exceptionResult , $ filter ->filter ($ value ));
357- }
358-
359389 public function testConfigDirectiveAvailable ()
360390 {
361391 $ path = "web/unsecure/base_url " ;
0 commit comments