Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AreaTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\App\Area;
10 
14 class AreaTest extends \PHPUnit\Framework\TestCase
15 {
16  const SCOPE_ID = '1';
17 
21  protected $objectManager;
22 
26  protected $eventManagerMock;
27 
31  protected $objectManagerMock;
32 
37 
41  protected $translatorMock;
42 
46  protected $loggerMock;
47 
51  protected $designMock;
52 
56  protected $scopeResolverMock;
57 
62 
66  protected $areaCode;
67 
71  protected $object;
72 
74  private $defaultRenderer;
75 
76  protected function setUp()
77  {
78  $this->defaultRenderer = \Magento\Framework\Phrase::getRenderer();
79  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
80  $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->translatorMock = $this->getMockBuilder(\Magento\Framework\TranslateInterface::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->diConfigLoaderMock = $this->getMockBuilder(\Magento\Framework\App\ObjectManager\ConfigLoader::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $this->designMock = $this->getMockBuilder(\Magento\Framework\App\DesignInterface::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98  $this->scopeResolverMock = $this->getMockBuilder(\Magento\Framework\App\ScopeResolverInterface::class)
99  ->disableOriginalConstructor()
100  ->getMock();
101  $scopeMock = $this->getMockBuilder(\Magento\Framework\App\ScopeInterface::class)
102  ->disableOriginalConstructor()
103  ->getMock();
104  $scopeMock->expects($this->any())
105  ->method('getId')
106  ->will($this->returnValue(self::SCOPE_ID));
107  $this->scopeResolverMock->expects($this->any())
108  ->method('getScope')
109  ->will($this->returnValue($scopeMock));
110  $this->designExceptionsMock = $this->getMockBuilder(\Magento\Framework\View\DesignExceptions::class)
111  ->disableOriginalConstructor()
112  ->getMock();
113  $this->areaCode = Area::AREA_FRONTEND;
114 
115  $this->object = $this->objectManager->getObject(
116  \Magento\Framework\App\Area::class,
117  [
118  'logger' => $this->loggerMock,
119  'objectManager' => $this->objectManagerMock,
120  'eventManager' => $this->eventManagerMock,
121  'translator' => $this->translatorMock,
122  'diConfigLoader' => $this->diConfigLoaderMock,
123  'design' => $this->designMock,
124  'scopeResolver' => $this->scopeResolverMock,
125  'designExceptions' => $this->designExceptionsMock,
126  'areaCode' => $this->areaCode,
127  ]
128  );
129  }
130 
131  public function tearDown()
132  {
133  \Magento\Framework\Phrase::setRenderer($this->defaultRenderer);
134  }
135 
136  public function testLoadConfig()
137  {
138  $this->verifyLoadConfig();
139  $this->object->load(Area::PART_CONFIG);
140  }
141 
142  public function testLoadTranslate()
143  {
144  $this->translatorMock->expects($this->once())
145  ->method('loadData');
146  $renderMock = $this->getMockBuilder(\Magento\Framework\Phrase\RendererInterface::class)
147  ->disableOriginalConstructor()
148  ->getMock();
149  $this->objectManagerMock->expects($this->once())
150  ->method('get')
151  ->with(\Magento\Framework\Phrase\RendererInterface::class)
152  ->will($this->returnValue($renderMock));
153  $this->object->load(Area::PART_TRANSLATE);
154  }
155 
156  public function testLoadDesign()
157  {
158  $designMock = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
159  ->disableOriginalConstructor()
160  ->getMock();
161  $this->objectManagerMock->expects($this->once())
162  ->method('get')
163  ->with(\Magento\Framework\View\DesignInterface::class)
164  ->will($this->returnValue($designMock));
165  $designMock->expects($this->once())
166  ->method('setArea')
167  ->with($this->areaCode)
168  ->willReturnSelf();
169  $designMock->expects($this->once())
170  ->method('setDefaultDesignTheme');
171  $this->object->load(Area::PART_DESIGN);
172  }
173 
174  public function testLoadUnknownPart()
175  {
176  $this->objectManagerMock->expects($this->never())
177  ->method('configure');
178  $this->objectManagerMock->expects($this->never())
179  ->method('get');
180  $this->object->load('unknown part');
181  }
182 
183  public function testLoad()
184  {
185  $this->verifyLoadConfig();
186  $this->translatorMock->expects($this->once())
187  ->method('loadData');
188  $renderMock = $this->getMockBuilder(\Magento\Framework\Phrase\RendererInterface::class)
189  ->disableOriginalConstructor()
190  ->getMock();
191  $designMock = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
192  ->disableOriginalConstructor()
193  ->getMock();
194  $designMock->expects($this->once())
195  ->method('setArea')
196  ->with($this->areaCode)
197  ->willReturnSelf();
198  $designMock->expects($this->once())
199  ->method('setDefaultDesignTheme');
200  $this->objectManagerMock->expects($this->exactly(2))
201  ->method('get')
202  ->will($this->returnValueMap(
203  [
204  [\Magento\Framework\Phrase\RendererInterface::class, $renderMock],
205  [\Magento\Framework\View\DesignInterface::class, $designMock],
206  ]
207  ));
208  $this->object->load();
209  }
210 
211  private function verifyLoadConfig()
212  {
213  $configs = ['dummy configs'];
214  $this->diConfigLoaderMock->expects($this->once())
215  ->method('load')
216  ->with($this->areaCode)
217  ->will($this->returnValue($configs));
218  $this->objectManagerMock->expects($this->once())
219  ->method('configure')
220  ->with($configs);
221  }
222 
223  public function testDetectDesign()
224  {
225  $this->designExceptionsMock->expects($this->never())
226  ->method('getThemeByRequest');
227  $this->designMock->expects($this->once())
228  ->method('loadChange')
229  ->with(self::SCOPE_ID)
230  ->willReturnSelf();
231  $designMock = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
232  ->disableOriginalConstructor()
233  ->getMock();
234  $this->objectManagerMock->expects($this->once())
235  ->method('get')
236  ->with(\Magento\Framework\View\DesignInterface::class)
237  ->will($this->returnValue($designMock));
238  $this->designMock->expects($this->once())
239  ->method('changeDesign')
240  ->with($designMock)
241  ->willReturnSelf();
242  $this->object->detectDesign();
243  }
244 
251  public function testDetectDesignByRequest($value, $callNum, $callNum2)
252  {
253  $this->designExceptionsMock->expects($this->once())
254  ->method('getThemeByRequest')
255  ->will($this->returnValue($value));
256  $designMock = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
257  ->disableOriginalConstructor()
258  ->getMock();
259  $designMock->expects($this->exactly($callNum))
260  ->method('setDesignTheme');
261  $this->objectManagerMock->expects($this->once())
262  ->method('get')
263  ->with(\Magento\Framework\View\DesignInterface::class)
264  ->will($this->returnValue($designMock));
265  $this->designMock->expects($this->exactly($callNum2))
266  ->method('loadChange')
267  ->with(self::SCOPE_ID)
268  ->willReturnSelf();
269  $this->designMock->expects($this->exactly($callNum2))
270  ->method('changeDesign')
271  ->with($designMock)
272  ->willReturnSelf();
273  $requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
274  ->disableOriginalConstructor()
275  ->getMock();
276  $this->object->detectDesign($requestMock);
277  }
278 
283  {
284  return [
285  [false, 0, 1],
286  ['theme', 1, 0],
287  ];
288  }
289 
291  {
292  $exception = new \Exception('exception');
293  $this->designExceptionsMock->expects($this->once())
294  ->method('getThemeByRequest')
295  ->will($this->throwException($exception));
296  $designMock = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
297  ->disableOriginalConstructor()
298  ->getMock();
299  $designMock->expects($this->never())
300  ->method('setDesignTheme');
301  $this->objectManagerMock->expects($this->once())
302  ->method('get')
303  ->with(\Magento\Framework\View\DesignInterface::class)
304  ->will($this->returnValue($designMock));
305  $this->designMock->expects($this->once())
306  ->method('loadChange')
307  ->with(self::SCOPE_ID)
308  ->willReturnSelf();
309  $this->designMock->expects($this->once())
310  ->method('changeDesign')
311  ->with($designMock)
312  ->willReturnSelf();
313  $requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
314  ->disableOriginalConstructor()
315  ->getMock();
316  $this->loggerMock->expects($this->once())
317  ->method('critical')
318  ->with($exception);
319  $this->object->detectDesign($requestMock);
320  }
321 }
$value
Definition: gender.phtml:16
static setRenderer(RendererInterface $renderer)
Definition: Phrase.php:46
testDetectDesignByRequest($value, $callNum, $callNum2)
Definition: AreaTest.php:251