ThemeProviderTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Test\Unit\Model\Theme;
  7. use Magento\Framework\App\Area;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\Framework\View\Design\ThemeInterface;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class ThemeProviderTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** Theme path used by tests */
  16. const THEME_PATH = 'frontend/Magento/luma';
  17. /** Theme ID used by tests */
  18. const THEME_ID = 755;
  19. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  20. private $objectManager;
  21. /** @var \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject */
  22. private $collectionFactory;
  23. /** @var \Magento\Theme\Model\ThemeFactory|\PHPUnit_Framework_MockObject_MockObject */
  24. private $themeFactory;
  25. /** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
  26. private $cache;
  27. /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
  28. private $serializer;
  29. /** @var \Magento\Theme\Model\Theme\ThemeProvider|\PHPUnit_Framework_MockObject_MockObject */
  30. private $themeProvider;
  31. /** @var \Magento\Theme\Model\Theme|\PHPUnit_Framework_MockObject_MockObject */
  32. private $theme;
  33. protected function setUp()
  34. {
  35. $this->objectManager = new ObjectManagerHelper($this);
  36. $this->collectionFactory = $this->createPartialMock(
  37. \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory::class,
  38. ['create']
  39. );
  40. $this->themeFactory = $this->createPartialMock(\Magento\Theme\Model\ThemeFactory::class, ['create']);
  41. $this->cache = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
  45. $this->themeProvider = $this->objectManager->getObject(
  46. \Magento\Theme\Model\Theme\ThemeProvider::class,
  47. [
  48. 'collectionFactory' => $this->collectionFactory,
  49. 'themeFactory' => $this->themeFactory,
  50. 'cache' => $this->cache,
  51. 'serializer' => $this->serializer
  52. ]
  53. );
  54. $this->theme = $this->createMock(\Magento\Theme\Model\Theme::class);
  55. }
  56. public function testGetByFullPath()
  57. {
  58. $themeArray = ['theme_data' => 'theme_data'];
  59. $this->theme->expects($this->exactly(2))
  60. ->method('getId')
  61. ->willReturn(self::THEME_ID);
  62. $this->theme->expects($this->exactly(2))
  63. ->method('toArray')
  64. ->willReturn($themeArray);
  65. $collectionMock = $this->createMock(\Magento\Theme\Model\ResourceModel\Theme\Collection::class);
  66. $collectionMock->expects($this->once())
  67. ->method('getThemeByFullPath')
  68. ->with(self::THEME_PATH)
  69. ->willReturn($this->theme);
  70. $this->collectionFactory->expects($this->once())
  71. ->method('create')
  72. ->willReturn($collectionMock);
  73. $this->serializer->expects($this->exactly(2))
  74. ->method('serialize')
  75. ->with($themeArray)
  76. ->willReturn('serialized theme');
  77. $deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $deploymentConfig->expects($this->once())
  81. ->method('isDbAvailable')
  82. ->willReturn(true);
  83. $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  84. $objectManagerMock->expects($this->any())
  85. ->method('get')
  86. ->willReturnMap([
  87. [\Magento\Framework\App\DeploymentConfig::class, $deploymentConfig],
  88. ]);
  89. \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
  90. $this->assertSame(
  91. $this->theme,
  92. $this->themeProvider->getThemeByFullPath(self::THEME_PATH),
  93. 'Unable to load Theme'
  94. );
  95. $this->assertSame(
  96. $this->theme,
  97. $this->themeProvider->getThemeByFullPath(self::THEME_PATH),
  98. 'Unable to load Theme from object cache'
  99. );
  100. }
  101. public function testGetByFullPathWithCache()
  102. {
  103. $deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $deploymentConfig->expects($this->once())
  107. ->method('isDbAvailable')
  108. ->willReturn(true);
  109. $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  110. $objectManagerMock->expects($this->any())
  111. ->method('get')
  112. ->willReturnMap([
  113. [\Magento\Framework\App\DeploymentConfig::class, $deploymentConfig],
  114. ]);
  115. \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
  116. $serializedTheme = '{"theme_data":"theme_data"}';
  117. $themeArray = ['theme_data' => 'theme_data'];
  118. $this->theme->expects($this->once())
  119. ->method('populateFromArray')
  120. ->with($themeArray)
  121. ->willReturnSelf();
  122. $this->themeFactory->expects($this->once())
  123. ->method('create')
  124. ->willReturn($this->theme);
  125. $this->serializer->expects($this->once())
  126. ->method('unserialize')
  127. ->with($serializedTheme)
  128. ->willReturn($themeArray);
  129. $this->cache->expects($this->once())
  130. ->method('load')
  131. ->with('theme' . self::THEME_PATH)
  132. ->willReturn($serializedTheme);
  133. $this->assertSame(
  134. $this->theme,
  135. $this->themeProvider->getThemeByFullPath(self::THEME_PATH),
  136. 'Unable to load Theme from application cache'
  137. );
  138. $this->assertSame(
  139. $this->theme,
  140. $this->themeProvider->getThemeByFullPath(self::THEME_PATH),
  141. 'Unable to load Theme from object cache'
  142. );
  143. }
  144. public function testGetById()
  145. {
  146. $themeArray = ['theme_data' => 'theme_data'];
  147. $this->theme->expects($this->once())
  148. ->method('load')
  149. ->with(self::THEME_ID)
  150. ->willReturnSelf();
  151. $this->theme->expects($this->once())
  152. ->method('getId')
  153. ->willReturn(self::THEME_ID);
  154. $this->theme->expects($this->once())
  155. ->method('toArray')
  156. ->willReturn($themeArray);
  157. $this->themeFactory->expects($this->once())->method('create')->will($this->returnValue($this->theme));
  158. $this->cache->expects($this->once())
  159. ->method('load')
  160. ->with('theme-by-id-' . self::THEME_ID)
  161. ->willReturn(false);
  162. $this->serializer->expects($this->once())
  163. ->method('serialize')
  164. ->with($themeArray)
  165. ->willReturn('{"theme_data":"theme_data"}');
  166. $this->assertSame(
  167. $this->theme,
  168. $this->themeProvider->getThemeById(self::THEME_ID),
  169. 'Unable to load Theme'
  170. );
  171. $this->assertSame(
  172. $this->theme,
  173. $this->themeProvider->getThemeById(self::THEME_ID),
  174. 'Unable to load Theme from object cache'
  175. );
  176. }
  177. public function testGetByIdWithCache()
  178. {
  179. $serializedTheme = '{"theme_data":"theme_data"}';
  180. $themeArray = ['theme_data' => 'theme_data'];
  181. $this->theme->expects($this->once())
  182. ->method('populateFromArray')
  183. ->with($themeArray)
  184. ->willReturnSelf();
  185. $this->cache->expects($this->once())
  186. ->method('load')
  187. ->with('theme-by-id-' . self::THEME_ID)
  188. ->willReturn($serializedTheme);
  189. $this->serializer->expects($this->once())
  190. ->method('unserialize')
  191. ->with($serializedTheme)
  192. ->willReturn($themeArray);
  193. $this->themeFactory->expects($this->once())
  194. ->method('create')
  195. ->willReturn($this->theme);
  196. $this->assertSame(
  197. $this->theme,
  198. $this->themeProvider->getThemeById(self::THEME_ID),
  199. 'Unable to load Theme from application cache'
  200. );
  201. $this->assertSame(
  202. $this->theme,
  203. $this->themeProvider->getThemeById(self::THEME_ID),
  204. 'Unable to load Theme from object cache'
  205. );
  206. }
  207. public function testGetThemeCustomizations()
  208. {
  209. $collection = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\Collection::class)
  210. ->disableOriginalConstructor()
  211. ->getMock();
  212. $collection->expects($this->once())
  213. ->method('addAreaFilter')
  214. ->with(Area::AREA_FRONTEND)
  215. ->willReturnSelf();
  216. $collection->expects($this->once())
  217. ->method('addTypeFilter')
  218. ->with(ThemeInterface::TYPE_VIRTUAL)
  219. ->willReturnSelf();
  220. $this->collectionFactory->expects($this->once())
  221. ->method('create')
  222. ->willReturn($collection);
  223. $this->assertInstanceOf(get_class($collection), $this->themeProvider->getThemeCustomizations());
  224. }
  225. }