Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApplyThemeCustomizationObserverTest.php
Go to the documentation of this file.
1 <?php
8 
12 class ApplyThemeCustomizationObserverTest extends \PHPUnit\Framework\TestCase
13 {
18 
22  protected $assetRepo;
23 
27  protected $assetsMock;
28 
32  protected $logger;
33 
37  protected $themeObserver;
38 
39  protected function setUp()
40  {
41  $this->themeCustomization = $this->createMock(\Magento\Framework\View\Design\Theme\Customization::class);
42  $themeMock = $this->createPartialMock(\Magento\Theme\Model\Theme::class, ['__wakeup', 'getCustomization']);
43  $themeMock->expects(
44  $this->any()
45  )->method(
46  'getCustomization'
47  )->will(
48  $this->returnValue($this->themeCustomization)
49  );
50 
51  $designMock = $this->createMock(\Magento\Framework\View\DesignInterface::class);
52  $designMock->expects($this->any())->method('getDesignTheme')->will($this->returnValue($themeMock));
53 
54  $this->assetsMock = $this->createMock(\Magento\Framework\View\Asset\GroupedCollection::class);
55 
56  $this->assetRepo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
57 
58  $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
59 
60  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
61  $this->themeObserver = $objectManagerHelper->getObject(
62  \Magento\Theme\Observer\ApplyThemeCustomizationObserver::class,
63  [
64  'design' => $designMock,
65  'assets' => $this->assetsMock,
66  'assetRepo' => $this->assetRepo,
67  'logger' => $this->logger,
68  ]
69  );
70  }
71 
72  public function testApplyThemeCustomization()
73  {
74  $asset = $this->createMock(\Magento\Framework\View\Asset\File::class);
75  $file = $this->createMock(\Magento\Theme\Model\Theme\File::class);
76  $fileService = $this->getMockForAbstractClass(
77  \Magento\Framework\View\Design\Theme\Customization\FileAssetInterface::class
78  );
79  $file->expects($this->any())->method('getCustomizationService')->will($this->returnValue($fileService));
80 
81  $this->assetRepo->expects($this->once())
82  ->method('createArbitrary')
83  ->will($this->returnValue($asset));
84 
85  $this->themeCustomization->expects($this->once())->method('getFiles')->will($this->returnValue([$file]));
86  $this->assetsMock->expects($this->once())->method('add')->with($this->anything(), $asset);
87 
88  $observer = new \Magento\Framework\Event\Observer();
89  $this->themeObserver->execute($observer);
90  }
91 
93  {
94  $file = $this->createMock(\Magento\Theme\Model\Theme\File::class);
95  $file->expects($this->any())
96  ->method('getCustomizationService')
97  ->willThrowException(new \InvalidArgumentException());
98 
99  $this->themeCustomization->expects($this->once())->method('getFiles')->will($this->returnValue([$file]));
100  $this->logger->expects($this->once())->method('critical');
101 
102  $observer = new \Magento\Framework\Event\Observer();
103  $this->themeObserver->execute($observer);
104  }
105 }