Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ThemeTest.php
Go to the documentation of this file.
1 <?php
11 
15 
19 class ThemeTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $_model;
25 
29  protected $_imageFactory;
30 
34  protected $themeFactory;
35 
40 
44  protected $domainFactory;
45 
49  protected $validator;
50 
55 
59  protected $appState;
60 
64  private $themeModelFactory;
65 
66  protected function setUp()
67  {
68  $customizationConfig = $this->createMock(\Magento\Theme\Model\Config\Customization::class);
69  $this->customizationFactory = $this->createPartialMock(
70  \Magento\Framework\View\Design\Theme\CustomizationFactory::class,
71  ['create']
72  );
73  $this->resourceCollection = $this->createMock(\Magento\Theme\Model\ResourceModel\Theme\Collection::class);
74  $this->_imageFactory = $this->createPartialMock(
75  \Magento\Framework\View\Design\Theme\ImageFactory::class,
76  ['create']
77  );
78  $this->themeFactory = $this->createPartialMock(
79  \Magento\Framework\View\Design\Theme\FlyweightFactory::class,
80  ['create']
81  );
82  $this->domainFactory = $this->createPartialMock(
83  \Magento\Framework\View\Design\Theme\Domain\Factory::class,
84  ['create']
85  );
86  $this->themeModelFactory = $this->createPartialMock(\Magento\Theme\Model\ThemeFactory::class, ['create']);
87  $this->validator = $this->createMock(\Magento\Framework\View\Design\Theme\Validator::class);
88  $this->appState = $this->createMock(\Magento\Framework\App\State::class);
89 
90  $objectManagerHelper = new ObjectManager($this);
91  $arguments = $objectManagerHelper->getConstructArguments(
92  \Magento\Theme\Model\Theme::class,
93  [
94  'customizationFactory' => $this->customizationFactory,
95  'customizationConfig' => $customizationConfig,
96  'imageFactory' => $this->_imageFactory,
97  'resourceCollection' => $this->resourceCollection,
98  'themeFactory' => $this->themeFactory,
99  'domainFactory' => $this->domainFactory,
100  'validator' => $this->validator,
101  'appState' => $this->appState,
102  'themeModelFactory' => $this->themeModelFactory
103  ]
104  );
105 
106  $this->_model = $objectManagerHelper->getObject(\Magento\Theme\Model\Theme::class, $arguments);
107  }
108 
109  protected function tearDown()
110  {
111  $this->_model = null;
112  }
113 
117  public function testThemeImageGetter()
118  {
119  $this->_imageFactory->expects($this->once())->method('create')->with(['theme' => $this->_model]);
120  $this->_model->getThemeImage();
121  }
122 
129  public function testIsVirtual($type, $isVirtual)
130  {
131  $this->_model->setType($type);
132  $this->assertEquals($isVirtual, $this->_model->isVirtual());
133  }
134 
138  public function isVirtualDataProvider()
139  {
140  return [
141  ['type' => ThemeInterface::TYPE_VIRTUAL, 'isVirtual' => true],
142  ['type' => ThemeInterface::TYPE_STAGING, 'isVirtual' => false],
143  ['type' => ThemeInterface::TYPE_PHYSICAL, 'isVirtual' => false]
144  ];
145  }
146 
153  public function testIsPhysical($type, $isPhysical)
154  {
155  $this->_model->setType($type);
156  $this->assertEquals($isPhysical, $this->_model->isPhysical());
157  }
158 
162  public function isPhysicalDataProvider()
163  {
164  return [
165  ['type' => ThemeInterface::TYPE_VIRTUAL, 'isPhysical' => false],
166  ['type' => ThemeInterface::TYPE_STAGING, 'isPhysical' => false],
167  ['type' => ThemeInterface::TYPE_PHYSICAL, 'isPhysical' => true]
168  ];
169  }
170 
177  public function testIsVisible($type, $isVisible)
178  {
179  $this->_model->setType($type);
180  $this->assertEquals($isVisible, $this->_model->isVisible());
181  }
182 
186  public function isVisibleDataProvider()
187  {
188  return [
189  ['type' => ThemeInterface::TYPE_VIRTUAL, 'isVisible' => true],
190  ['type' => ThemeInterface::TYPE_STAGING, 'isVisible' => false],
191  ['type' => ThemeInterface::TYPE_PHYSICAL, 'isVisible' => true]
192  ];
193  }
194 
203  public function testIsDeletable($themeType, $isDeletable)
204  {
205  $themeModel = $this->createPartialMock(\Magento\Theme\Model\Theme::class, ['getType']);
206  $themeModel->expects($this->once())->method('getType')->will($this->returnValue($themeType));
208  $this->assertEquals($isDeletable, $themeModel->isDeletable());
209  }
210 
214  public function isDeletableDataProvider()
215  {
216  return [
220  ];
221  }
222 
228  public function testGetCode($originalCode, $expectedCode)
229  {
230  $this->_model->setCode($originalCode);
231  $this->assertSame($expectedCode, $this->_model->getCode());
232  }
233 
237  public function getCodeDataProvider()
238  {
239  return [
240  'string code' => ['theme/code', 'theme/code'],
241  'null code' => [null, ''],
242  'number code' => [10, '10']
243  ];
244  }
245 
250  public function testGetInheritedThemes()
251  {
252  $inheritedTheme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)->getMock();
253 
254  $this->_model->setParentId(10);
255  $this->themeFactory->expects($this->once())
256  ->method('create')
257  ->with(10)
258  ->willReturn($inheritedTheme);
259 
260  $this->assertContainsOnlyInstancesOf(
261  \Magento\Framework\View\Design\ThemeInterface::class,
262  $this->_model->getInheritedThemes()
263  );
264  $this->assertCount(2, $this->_model->getInheritedThemes());
265  }
266 
271  public function testAfterDelete()
272  {
273  $expectId = 101;
274  $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
275  ->setMethods(['delete', 'getId'])
276  ->getMockForAbstractClass();
277  $theme->expects($this->once())
278  ->method('getId')
279  ->willReturn($expectId);
280  $theme->expects($this->once())
281  ->method('delete')
282  ->willReturnSelf();
283 
284  $this->_model->setId(1);
285  $this->resourceCollection->expects($this->at(0))
286  ->method('addFieldToFilter')
287  ->with('parent_id', 1)
288  ->willReturnSelf();
289  $this->resourceCollection->expects($this->at(1))
290  ->method('addFieldToFilter')
291  ->with('type', Theme::TYPE_STAGING)
292  ->willReturnSelf();
293  $this->resourceCollection->expects($this->once())
294  ->method('getFirstItem')
295  ->willReturn($theme);
296  $this->resourceCollection->expects($this->once())
297  ->method('updateChildRelations')
298  ->with($this->_model);
299 
300  $this->assertInstanceOf(get_class($this->_model), $this->_model->afterDelete());
301  }
302 
307  public function testGetStagingVersion()
308  {
309  $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
310  ->setMethods(['getId'])
311  ->getMockForAbstractClass();
312  $theme->expects($this->once())
313  ->method('getId')
314  ->willReturn(null);
315 
316  $this->_model->setId(1);
317  $this->resourceCollection->expects($this->at(0))
318  ->method('addFieldToFilter')
319  ->with('parent_id', 1)
320  ->willReturnSelf();
321  $this->resourceCollection->expects($this->at(1))
322  ->method('addFieldToFilter')
323  ->with('type', Theme::TYPE_STAGING)
324  ->willReturnSelf();
325  $this->resourceCollection->expects($this->once())
326  ->method('getFirstItem')
327  ->willReturn($theme);
328 
329  $this->assertNull($this->_model->getStagingVersion());
330  }
331 
337  {
338  $this->assertNull($this->_model->getStagingVersion());
339  }
340 
345  public function testGetDomainModel()
346  {
347  $result = 'res';
348  $this->domainFactory->expects($this->once())
349  ->method('create')
350  ->with($this->_model)
351  ->willReturn($result);
352  $this->assertEquals($result, $this->_model->getDomainModel());
353  }
354 
361  {
362  $this->_model->getDomainModel('bla-bla-bla');
363  }
364 
371  public function testValidate()
372  {
373  $this->validator->expects($this->once())
374  ->method('validate')
375  ->with($this->_model)
376  ->willReturn(false);
377  $this->validator->expects($this->once())
378  ->method('getErrorMessages')
379  ->willReturn([[__('testMessage')]]);
380  $this->assertInstanceOf(get_class($this->_model), $this->_model->beforeSave());
381  }
382 
387  public function testValidatePass()
388  {
389  $this->validator->expects($this->once())
390  ->method('validate')
391  ->with($this->_model)
392  ->willReturn(true);
393  $this->assertInstanceOf(get_class($this->_model), $this->_model->beforeSave());
394  }
395 
400  public function testHasChildThemes()
401  {
402  $this->_model->setId(1);
403  $this->resourceCollection->expects($this->once())
404  ->method('addTypeFilter')
405  ->with(Theme::TYPE_VIRTUAL)
406  ->willReturnSelf();
407  $this->resourceCollection->expects($this->once())
408  ->method('addFieldToFilter')
409  ->with('parent_id', ['eq' => 1])
410  ->willReturnSelf();
411  $this->resourceCollection->expects($this->once())
412  ->method('getSize')
413  ->willReturn(10);
414  $this->assertTrue($this->_model->hasChildThemes());
415  }
416 
421  public function testGetCustomization()
422  {
423  $this->customizationFactory->expects($this->once())
424  ->method('create')
425  ->willReturn(
426  $this->getMockBuilder(\Magento\Framework\View\Design\Theme\CustomizationInterface::class)->getMock()
427  );
428  $this->assertInstanceOf(
429  \Magento\Framework\View\Design\Theme\CustomizationInterface::class,
430  $this->_model->getCustomization()
431  );
432  }
433 
438  public function testIsEditable()
439  {
440  $this->_model->setType(Theme::TYPE_VIRTUAL);
441  $this->assertTrue($this->_model->isEditable());
442  $this->_model->setType(Theme::TYPE_PHYSICAL);
443  $this->assertFalse($this->_model->isEditable());
444  }
445 
450  public function getFullThemePath()
451  {
452  $areaCode = 'frontend';
453  $this->appState->expects($this->once())
454  ->method('getAreaCode')
455  ->willReturn($areaCode);
456 
457  $path = 'some/path';
458  $this->_model->setThemePath($path);
459 
460  $this->assertEquals($areaCode . Theme::PATH_SEPARATOR . $path, $this->_model->getFullPath());
461  }
462 
467  public function getParentTheme()
468  {
469  $this->_model->setParentTheme('parent_theme');
470  $this->assertEquals('parent_theme', $this->_model->getParentTheme());
471  }
472 
478  public function testToArray(array $themeData, array $expected)
479  {
480  $this->_model->setData($themeData);
481  $this->assertEquals($expected, $this->_model->toArray());
482  }
483 
487  public function toArrayDataProvider()
488  {
489  $parentTheme = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
490  ->disableOriginalConstructor()
491  ->getMock();
492  $childTheme = clone $parentTheme;
493 
494  $parentTheme->expects($this->once())
495  ->method('toArray')
496  ->willReturn('parent_theme');
497 
498  $childTheme->expects($this->exactly(2))
499  ->method('toArray')
500  ->willReturn('child_theme');
501 
502  return [
503  'null' => [[], []],
504  'valid' => [
505  ['theme_data' => 'theme_data'],
506  ['theme_data' => 'theme_data']
507  ],
508  'valid with parent' => [
509  [
510  'theme_data' => 'theme_data',
511  'parent_theme' => $parentTheme
512  ],
513  [
514  'theme_data' => 'theme_data',
515  'parent_theme' => 'parent_theme'
516  ]
517  ],
518  'valid with children' => [
519  [
520  'theme_data' => 'theme_data',
521  'inherited_themes' => [
522  'key1' => $childTheme,
523  'key2' => $childTheme
524  ]
525  ],
526  [
527  'theme_data' => 'theme_data',
528  'inherited_themes' => [
529  'key1' => 'child_theme',
530  'key2' => 'child_theme'
531  ]
532  ]
533  ]
534  ];
535  }
536 
544  public function testPopulateFromArray(array $value, array $expected, $expectedCallCount = 0)
545  {
546  $themeMock = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
547  ->disableOriginalConstructor()
548  ->getMock();
549  $themeMock->expects($this->exactly($expectedCallCount))
550  ->method('populateFromArray')
551  ->willReturn('theme_instance');
552 
553  $this->themeModelFactory->expects($this->exactly($expectedCallCount))
554  ->method('create')
555  ->willReturn($themeMock);
556 
557  $this->_model->populateFromArray($value);
558  $this->assertEquals($expected, $this->_model->getData());
559  }
560 
565  {
566  return [
567  'valid data' => [
568  'value' => ['theme_data' => 'theme_data'],
569  'expected' => ['theme_data' => 'theme_data']
570  ],
571  'valid data with parent' => [
572  'value' => [
573  'theme_data' => 'theme_data',
574  'parent_theme' => [
575  'theme_data' => 'theme_data'
576  ]
577  ],
578  'expected' => [
579  'theme_data' => 'theme_data',
580  'parent_theme' => 'theme_instance'
581  ],
582  'expected call count' => 1
583  ],
584  'valid data with children' => [
585  'value' => [
586  'theme_data' => 'theme_data',
587  'inherited_themes' => [
588  'key1' => ['theme_data' => 'theme_data'],
589  'key2' => ['theme_data' => 'theme_data']
590  ]
591  ],
592  'expected' => [
593  'theme_data' => 'theme_data',
594  'inherited_themes' => [
595  'key1' => 'theme_instance',
596  'key2' => 'theme_instance'
597  ]
598  ],
599  'expected call count' => 2
600  ]
601  ];
602  }
603 }
testPopulateFromArray(array $value, array $expected, $expectedCallCount=0)
Definition: ThemeTest.php:544
testGetCode($originalCode, $expectedCode)
Definition: ThemeTest.php:228
__()
Definition: __.php:13
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$theme
$arguments
testToArray(array $themeData, array $expected)
Definition: ThemeTest.php:478