|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\LayeredNavigation\Test\Unit\Observer\Edit\Tab\Front; |
| 10 | + |
| 11 | +use Magento\Config\Model\Config\Source\Yesno; |
| 12 | +use Magento\Framework\Data\Form; |
| 13 | +use Magento\Framework\Data\Form\Element\Fieldset; |
| 14 | +use Magento\Framework\Event\Observer; |
| 15 | +use Magento\Framework\Module\Manager; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 17 | +use Magento\LayeredNavigation\Observer\Edit\Tab\Front\ProductAttributeFormBuildFrontTabObserver; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Unit Test for \Magento\LayeredNavigation\Observer\Edit\Tab\Front\ProductAttributeFormBuildFrontTabObserver |
| 23 | + */ |
| 24 | +class ProductAttributeFormBuildFrontTabObserverTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var MockObject|Observer |
| 28 | + */ |
| 29 | + private $eventObserverMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var MockObject|Yesno |
| 33 | + */ |
| 34 | + private $optionListLock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var MockObject|Manager |
| 38 | + */ |
| 39 | + private $moduleManagerMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ProductAttributeFormBuildFrontTabObserver |
| 43 | + */ |
| 44 | + private $observer; |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritDoc |
| 48 | + */ |
| 49 | + protected function setUp(): void |
| 50 | + { |
| 51 | + $this->optionListLock = $this->createMock(Yesno::class); |
| 52 | + $this->moduleManagerMock = $this->createMock(Manager::class); |
| 53 | + $this->eventObserverMock = $this->getMockBuilder(Observer::class) |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->setMethods(['getForm']) |
| 56 | + ->getMock(); |
| 57 | + |
| 58 | + $objectManager = new ObjectManager($this); |
| 59 | + $this->observer = $objectManager->getObject( |
| 60 | + ProductAttributeFormBuildFrontTabObserver::class, |
| 61 | + [ |
| 62 | + 'optionList' => $this->optionListLock, |
| 63 | + 'moduleManager' => $this->moduleManagerMock, |
| 64 | + ] |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test case when module output is disabled |
| 70 | + */ |
| 71 | + public function testExecuteWhenOutputDisabled(): void |
| 72 | + { |
| 73 | + $this->moduleManagerMock->expects($this->once()) |
| 74 | + ->method('isOutputEnabled') |
| 75 | + ->with('Magento_LayeredNavigation') |
| 76 | + ->willReturn(false); |
| 77 | + |
| 78 | + $this->eventObserverMock->expects($this->never())->method('getForm'); |
| 79 | + |
| 80 | + $this->observer->execute($this->eventObserverMock); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Test case when module output is enabled |
| 85 | + */ |
| 86 | + public function testExecuteWhenOutputEnabled(): void |
| 87 | + { |
| 88 | + $this->moduleManagerMock->expects($this->once()) |
| 89 | + ->method('isOutputEnabled') |
| 90 | + ->with('Magento_LayeredNavigation') |
| 91 | + ->willReturn(true); |
| 92 | + |
| 93 | + $fieldsetMock = $this->createMock(Fieldset::class); |
| 94 | + $fieldsetMock->expects($this->exactly(3))->method('addField'); |
| 95 | + $formMock = $this->createMock(Form::class); |
| 96 | + $formMock->expects($this->once()) |
| 97 | + ->method('getElement') |
| 98 | + ->with('front_fieldset') |
| 99 | + ->willReturn($fieldsetMock); |
| 100 | + |
| 101 | + $this->eventObserverMock->expects($this->once()) |
| 102 | + ->method('getForm') |
| 103 | + ->willReturn($formMock); |
| 104 | + |
| 105 | + $this->observer->execute($this->eventObserverMock); |
| 106 | + } |
| 107 | +} |
0 commit comments