VirtualTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test theme virtual model
  8. */
  9. namespace Magento\Theme\Test\Unit\Model\Theme\Domain;
  10. class VirtualTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Test get existing staging theme
  14. *
  15. * @covers \Magento\Theme\Model\Theme\Domain\Virtual::__construct
  16. * @covers \Magento\Theme\Model\Theme\Domain\Virtual::getStagingTheme
  17. */
  18. public function testGetStagingThemeExisting()
  19. {
  20. $themeStaging = $this->createMock(\Magento\Theme\Model\Theme::class);
  21. $theme = $this->createPartialMock(\Magento\Theme\Model\Theme::class, ['__wakeup', 'getStagingVersion']);
  22. $theme->expects($this->once())->method('getStagingVersion')->will($this->returnValue($themeStaging));
  23. $themeFactory = $this->createPartialMock(\Magento\Theme\Model\ThemeFactory::class, ['create']);
  24. $themeFactory->expects($this->never())->method('create');
  25. $themeCopyService = $this->createPartialMock(\Magento\Theme\Model\CopyService::class, ['copy']);
  26. $themeCopyService->expects($this->never())->method('copy');
  27. $customizationConfig = $this->createMock(\Magento\Theme\Model\Config\Customization::class);
  28. $object = new \Magento\Theme\Model\Theme\Domain\Virtual(
  29. $theme,
  30. $themeFactory,
  31. $themeCopyService,
  32. $customizationConfig
  33. );
  34. $this->assertSame($themeStaging, $object->getStagingTheme());
  35. }
  36. /**
  37. * Test creating staging theme
  38. *
  39. * @covers \Magento\Theme\Model\Theme\Domain\Virtual::_createStagingTheme
  40. * @covers \Magento\Theme\Model\Theme\Domain\Virtual::getStagingTheme
  41. */
  42. public function testGetStagingThemeNew()
  43. {
  44. $theme = $this->createPartialMock(\Magento\Theme\Model\Theme::class, ['__wakeup', 'getStagingVersion']);
  45. $theme->expects($this->once())->method('getStagingVersion')->will($this->returnValue(null));
  46. $appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['getAreaCode']);
  47. $appState->expects($this->any())->method('getAreaCode')->will($this->returnValue('fixture_area'));
  48. $appStateProperty = new \ReflectionProperty(\Magento\Theme\Model\Theme::class, '_appState');
  49. $appStateProperty->setAccessible(true);
  50. /** @var $theme \Magento\Framework\DataObject */
  51. $theme->setData(
  52. [
  53. 'id' => 'fixture_theme_id',
  54. 'theme_title' => 'fixture_theme_title',
  55. 'preview_image' => 'fixture_preview_image',
  56. 'is_featured' => 'fixture_is_featured',
  57. 'type' => \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL,
  58. ]
  59. );
  60. $appStateProperty->setValue($theme, $appState);
  61. $themeStaging = $this->createPartialMock(\Magento\Theme\Model\Theme::class, ['__wakeup', 'setData', 'save']);
  62. $themeStaging->expects(
  63. $this->at(0)
  64. )->method(
  65. 'setData'
  66. )->with(
  67. [
  68. 'parent_id' => 'fixture_theme_id',
  69. 'theme_path' => null,
  70. 'theme_title' => 'fixture_theme_title - Staging',
  71. 'preview_image' => 'fixture_preview_image',
  72. 'is_featured' => 'fixture_is_featured',
  73. 'type' => \Magento\Framework\View\Design\ThemeInterface::TYPE_STAGING,
  74. ]
  75. );
  76. $appStateProperty->setValue($themeStaging, $appState);
  77. $themeStaging->expects($this->at(1))->method('save');
  78. $themeFactory = $this->createPartialMock(\Magento\Theme\Model\ThemeFactory::class, ['create']);
  79. $themeFactory->expects($this->once())->method('create')->will($this->returnValue($themeStaging));
  80. $themeCopyService = $this->createPartialMock(\Magento\Theme\Model\CopyService::class, ['copy']);
  81. $themeCopyService->expects($this->once())->method('copy')->with($theme, $themeStaging);
  82. $customizationConfig = $this->createMock(\Magento\Theme\Model\Config\Customization::class);
  83. $object = new \Magento\Theme\Model\Theme\Domain\Virtual(
  84. $theme,
  85. $themeFactory,
  86. $themeCopyService,
  87. $customizationConfig
  88. );
  89. $this->assertSame($themeStaging, $object->getStagingTheme());
  90. $this->assertSame($themeStaging, $object->getStagingTheme());
  91. }
  92. /**
  93. * Test for is assigned method
  94. *
  95. * @covers \Magento\Theme\Model\Theme\Domain\Virtual::isAssigned
  96. */
  97. public function testIsAssigned()
  98. {
  99. $customizationConfig = $this->createPartialMock(
  100. \Magento\Theme\Model\Config\Customization::class,
  101. ['isThemeAssignedToStore']
  102. );
  103. $themeMock = $this->createPartialMock(
  104. \Magento\Theme\Model\Theme::class,
  105. ['__wakeup', 'getCollection', 'getId']
  106. );
  107. $customizationConfig->expects(
  108. $this->atLeastOnce()
  109. )->method(
  110. 'isThemeAssignedToStore'
  111. )->with(
  112. $themeMock
  113. )->will(
  114. $this->returnValue(true)
  115. );
  116. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  117. $constructArguments = $objectManagerHelper->getConstructArguments(
  118. \Magento\Theme\Model\Theme\Domain\Virtual::class,
  119. ['theme' => $themeMock, 'customizationConfig' => $customizationConfig]
  120. );
  121. /** @var $model \Magento\Theme\Model\Theme\Domain\Virtual */
  122. $model = $objectManagerHelper->getObject(\Magento\Theme\Model\Theme\Domain\Virtual::class, $constructArguments);
  123. $this->assertEquals(true, $model->isAssigned());
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function physicalThemeDataProvider()
  129. {
  130. $physicalTheme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
  131. ->setMethods(['isPhysical', 'getId'])
  132. ->getMockForAbstractClass();
  133. $physicalTheme->expects($this->once())
  134. ->method('isPhysical')
  135. ->willReturn(true);
  136. $physicalTheme->expects($this->once())
  137. ->method('getId')
  138. ->willReturn(1);
  139. return [
  140. 'empty' => [null],
  141. 'theme' => [$physicalTheme],
  142. ];
  143. }
  144. /**
  145. * @test
  146. * @return void
  147. * @dataProvider physicalThemeDataProvider
  148. * @covers \Magento\Theme\Model\Theme\Domain\Virtual::getPhysicalTheme
  149. */
  150. public function testGetPhysicalTheme($data)
  151. {
  152. $themeMock = $this->createPartialMock(\Magento\Theme\Model\Theme::class, ['__wakeup', 'getParentTheme']);
  153. $parentThemeMock = $this->createPartialMock(
  154. \Magento\Theme\Model\Theme::class,
  155. ['__wakeup', 'isPhysical', 'getParentTheme']
  156. );
  157. $themeMock->expects($this->once())
  158. ->method('getParentTheme')
  159. ->willReturn($parentThemeMock);
  160. $parentThemeMock->expects($this->once())
  161. ->method('getParentTheme')
  162. ->willReturn($data);
  163. $parentThemeMock->expects($this->once())
  164. ->method('isPhysical')
  165. ->willReturn(false);
  166. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  167. $object = $objectManagerHelper->getObject(
  168. \Magento\Theme\Model\Theme\Domain\Virtual::class,
  169. ['theme' => $themeMock]
  170. );
  171. /** @var $object \Magento\Theme\Model\Theme\Domain\Virtual */
  172. $this->assertEquals($data, $object->getPhysicalTheme());
  173. }
  174. }