ApplyThemeCustomizationObserverTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Test\Unit\Observer;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class ApplyThemeCustomizationObserverTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $themeCustomization;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $assetRepo;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $assetsMock;
  24. /**
  25. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $logger;
  28. /**
  29. * @var \Magento\Theme\Observer\ApplyThemeCustomizationObserver
  30. */
  31. protected $themeObserver;
  32. protected function setUp()
  33. {
  34. $this->themeCustomization = $this->createMock(\Magento\Framework\View\Design\Theme\Customization::class);
  35. $themeMock = $this->createPartialMock(\Magento\Theme\Model\Theme::class, ['__wakeup', 'getCustomization']);
  36. $themeMock->expects(
  37. $this->any()
  38. )->method(
  39. 'getCustomization'
  40. )->will(
  41. $this->returnValue($this->themeCustomization)
  42. );
  43. $designMock = $this->createMock(\Magento\Framework\View\DesignInterface::class);
  44. $designMock->expects($this->any())->method('getDesignTheme')->will($this->returnValue($themeMock));
  45. $this->assetsMock = $this->createMock(\Magento\Framework\View\Asset\GroupedCollection::class);
  46. $this->assetRepo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
  47. $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
  48. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  49. $this->themeObserver = $objectManagerHelper->getObject(
  50. \Magento\Theme\Observer\ApplyThemeCustomizationObserver::class,
  51. [
  52. 'design' => $designMock,
  53. 'assets' => $this->assetsMock,
  54. 'assetRepo' => $this->assetRepo,
  55. 'logger' => $this->logger,
  56. ]
  57. );
  58. }
  59. public function testApplyThemeCustomization()
  60. {
  61. $asset = $this->createMock(\Magento\Framework\View\Asset\File::class);
  62. $file = $this->createMock(\Magento\Theme\Model\Theme\File::class);
  63. $fileService = $this->getMockForAbstractClass(
  64. \Magento\Framework\View\Design\Theme\Customization\FileAssetInterface::class
  65. );
  66. $file->expects($this->any())->method('getCustomizationService')->will($this->returnValue($fileService));
  67. $this->assetRepo->expects($this->once())
  68. ->method('createArbitrary')
  69. ->will($this->returnValue($asset));
  70. $this->themeCustomization->expects($this->once())->method('getFiles')->will($this->returnValue([$file]));
  71. $this->assetsMock->expects($this->once())->method('add')->with($this->anything(), $asset);
  72. $observer = new \Magento\Framework\Event\Observer();
  73. $this->themeObserver->execute($observer);
  74. }
  75. public function testApplyThemeCustomizationException()
  76. {
  77. $file = $this->createMock(\Magento\Theme\Model\Theme\File::class);
  78. $file->expects($this->any())
  79. ->method('getCustomizationService')
  80. ->willThrowException(new \InvalidArgumentException());
  81. $this->themeCustomization->expects($this->once())->method('getFiles')->will($this->returnValue([$file]));
  82. $this->logger->expects($this->once())->method('critical');
  83. $observer = new \Magento\Framework\Event\Observer();
  84. $this->themeObserver->execute($observer);
  85. }
  86. }