Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Test for Magento\LayeredNavigation\Observer\Edit\Tab\Front\ProductAttributeFormBuildFrontTabObserver #26617

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\LayeredNavigation\Test\Unit\Observer\Edit\Tab\Front\ProductAttributeFormBuildFrontTabObserverTest;

use Magento\Config\Model\Config\Source\Yesno;
use Magento\Framework\Data\Form;
use Magento\Framework\Data\Form\Element\Fieldset;
use Magento\Framework\Event\Observer;
use Magento\Framework\Module\Manager;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\LayeredNavigation\Observer\Edit\Tab\Front\ProductAttributeFormBuildFrontTabObserver;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Unit Test for \Magento\LayeredNavigation\Observer\Edit\Tab\Front\ProductAttributeFormBuildFrontTabObserver
*/
class ProductAttributeFormBuildFrontTabObserverTest extends TestCase
{
/**
* @var MockObject|Observer
*/
private $eventObserverMock;

/**
* @var MockObject|Yesno
*/
private $optionListLock;

/**
* @var MockObject|Manager
*/
private $moduleManagerMock;

/**
* @var ProductAttributeFormBuildFrontTabObserver
*/
private $observer;

/**
* @inheritDoc
*/
protected function setUp()
{
$this->optionListLock = $this->createMock(Yesno::class);
$this->moduleManagerMock = $this->createMock(Manager::class);
$this->eventObserverMock = $this->getMockBuilder(Observer::class)
->disableOriginalConstructor()
->setMethods(['getForm'])
->getMock();

$objectManager = new ObjectManager($this);
$this->observer = $objectManager->getObject(
ProductAttributeFormBuildFrontTabObserver::class,
[
'optionList' => $this->optionListLock,
'moduleManager' => $this->moduleManagerMock,
]
);
}

/**
* Test case when module output is disabled
*/
public function testExecuteWhenOutputDisabled()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not critical but would be preferred to follow the strict type of definition for methods signature.

Suggested change
public function testExecuteWhenOutputDisabled()
public function testExecuteWhenOutputDisabled(): void

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

{
$this->moduleManagerMock->expects($this->once())
->method('isOutputEnabled')
->with('Magento_LayeredNavigation')
->willReturn(false);

$this->eventObserverMock->expects($this->never())->method('getForm');

$this->observer->execute($this->eventObserverMock);
}

/**
* Test case when module output is enabled
*/
public function testExecuteWhenOutputEnabled()
{
$this->moduleManagerMock->expects($this->once())
->method('isOutputEnabled')
->with('Magento_LayeredNavigation')
->willReturn(true);

$fieldsetMock = $this->createMock(Fieldset::class);
$fieldsetMock->expects(self::exactly(3))->method('addField');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please replace the static method's call for consistency with other stubs definition.

Suggested change
$fieldsetMock->expects(self::exactly(3))->method('addField');
$fieldsetMock->expects($this->exactly(3))->method('addField');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmytro-ch thanks for the note, fixed!

$formMock = $this->createMock(Form::class);
$formMock->expects($this->once())
->method('getElement')
->with('front_fieldset')
->willReturn($fieldsetMock);

$this->eventObserverMock->expects($this->once())
->method('getForm')
->willReturn($formMock);

$this->observer->execute($this->eventObserverMock);
}
}