RegistrationTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Plugin;
  7. use Magento\Theme\Model\Theme\Plugin\Registration;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Phrase;
  10. class RegistrationTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\Theme\Model\Theme\Registration|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $themeRegistration;
  14. /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $logger;
  16. /** @var \Magento\Backend\App\AbstractAction|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $abstractAction;
  18. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $request;
  20. /** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $appState;
  22. /** @var \Magento\Theme\Model\Theme\Collection|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $themeCollection;
  24. /** @var \Magento\Theme\Model\ResourceModel\Theme\Collection|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $themeLoader;
  26. /** @var Registration */
  27. protected $plugin;
  28. protected function setUp()
  29. {
  30. $this->themeRegistration = $this->createMock(\Magento\Theme\Model\Theme\Registration::class);
  31. $this->logger = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class, [], '', false);
  32. $this->abstractAction = $this->getMockForAbstractClass(
  33. \Magento\Backend\App\AbstractAction::class,
  34. [],
  35. '',
  36. false
  37. );
  38. $this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class, [], '', false);
  39. $this->appState = $this->createMock(\Magento\Framework\App\State::class);
  40. $this->themeCollection = $this->createMock(\Magento\Theme\Model\Theme\Collection::class);
  41. $this->themeLoader = $this->createMock(\Magento\Theme\Model\ResourceModel\Theme\Collection::class);
  42. $this->plugin = new Registration(
  43. $this->themeRegistration,
  44. $this->themeCollection,
  45. $this->themeLoader,
  46. $this->logger,
  47. $this->appState
  48. );
  49. }
  50. /**
  51. * @param bool $hasParentTheme
  52. * @dataProvider dataProviderBeforeDispatch
  53. * @SuppressWarnings(PHPMD.NPathComplexity)
  54. */
  55. public function testBeforeDispatch(
  56. $hasParentTheme
  57. ) {
  58. $themeId = 1;
  59. $themeTitle = 'Theme title';
  60. $themeFromConfigMock = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
  61. ->disableOriginalConstructor()
  62. ->setMethods([
  63. 'getArea',
  64. 'getThemePath',
  65. 'getParentTheme',
  66. 'getThemeTitle',
  67. ])
  68. ->getMock();
  69. $themeFromDbMock = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
  70. ->disableOriginalConstructor()
  71. ->setMethods([
  72. 'setParentId',
  73. 'setThemeTitle',
  74. 'save',
  75. ])
  76. ->getMock();
  77. $parentThemeFromDbMock = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $parentThemeFromConfigMock = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->appState->expects($this->once())
  84. ->method('getMode')
  85. ->willReturn('default');
  86. $this->themeRegistration->expects($this->once())
  87. ->method('register');
  88. $this->themeCollection->expects($this->once())
  89. ->method('loadData')
  90. ->willReturn([$themeFromConfigMock]);
  91. $this->themeLoader->expects($hasParentTheme ? $this->exactly(2) : $this->once())
  92. ->method('getThemeByFullPath')
  93. ->willReturnMap([
  94. ['frontend/Magento/blank', $parentThemeFromDbMock],
  95. ['frontend/Magento/luma', $themeFromDbMock],
  96. ]);
  97. $themeFromConfigMock->expects($this->once())
  98. ->method('getArea')
  99. ->willReturn('frontend');
  100. $themeFromConfigMock->expects($this->once())
  101. ->method('getThemePath')
  102. ->willReturn('Magento/luma');
  103. $themeFromConfigMock->expects($hasParentTheme ? $this->exactly(2) : $this->once())
  104. ->method('getParentTheme')
  105. ->willReturn($hasParentTheme ? $parentThemeFromConfigMock : null);
  106. $themeFromConfigMock->expects($this->once())
  107. ->method('getThemeTitle')
  108. ->willReturn($themeTitle);
  109. $parentThemeFromDbMock->expects($hasParentTheme ? $this->once() : $this->never())
  110. ->method('getId')
  111. ->willReturn($themeId);
  112. $parentThemeFromConfigMock->expects($hasParentTheme ? $this->once() : $this->never())
  113. ->method('getFullPath')
  114. ->willReturn('frontend/Magento/blank');
  115. $themeFromDbMock->expects($hasParentTheme ? $this->once() : $this->never())
  116. ->method('setParentId')
  117. ->with($themeId)
  118. ->willReturnSelf();
  119. $themeFromDbMock->expects($this->once())
  120. ->method('setThemeTitle')
  121. ->with($themeTitle)
  122. ->willReturnSelf();
  123. $themeFromDbMock->expects($this->once())
  124. ->method('save')
  125. ->willReturnSelf();
  126. $this->plugin->beforeDispatch($this->abstractAction, $this->request);
  127. }
  128. /**
  129. * @return array
  130. */
  131. public function dataProviderBeforeDispatch()
  132. {
  133. return [
  134. [true],
  135. [false],
  136. ];
  137. }
  138. public function testBeforeDispatchWithProductionMode()
  139. {
  140. $this->appState->expects($this->once())->method('getMode')->willReturn('production');
  141. $this->plugin->beforeDispatch($this->abstractAction, $this->request);
  142. }
  143. public function testBeforeDispatchWithException()
  144. {
  145. $exception = new LocalizedException(new Phrase('Phrase'));
  146. $this->themeRegistration->expects($this->once())->method('register')->willThrowException($exception);
  147. $this->logger->expects($this->once())->method('critical');
  148. $this->plugin->beforeDispatch($this->abstractAction, $this->request);
  149. }
  150. }