Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddFieldsToAttributeObserverTest.php
Go to the documentation of this file.
1 <?php
7 
11 class AddFieldsToAttributeObserverTest extends \PHPUnit\Framework\TestCase
12 {
14  protected $moduleManagerMock;
15 
17  protected $yesNoMock;
18 
20  protected $formMock;
21 
23  protected $eventObserverMock;
24 
26  protected $observerMock;
27 
28  protected function setUp()
29  {
30  $this->moduleManagerMock = $this->createMock(\Magento\Framework\Module\Manager::class);
31 
32  $this->yesNoMock = $this->createMock(\Magento\Config\Model\Config\Source\Yesno::class);
33  $this->eventObserverMock = $this->createPartialMock(
34  \Magento\Framework\Event\Observer::class,
35  ['getForm', 'getEvent', 'getAttribute']
36  );
37  $this->formMock = $this->createPartialMock(\Magento\Framework\Data\Form::class, ['getElement']);
38 
39  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
40  $this->observerMock = $objectManager->getObject(
41  \Magento\Swatches\Observer\AddFieldsToAttributeObserver::class,
42  [
43  'moduleManager' => $this->moduleManagerMock,
44  'yesNo' => $this->yesNoMock,
45  ]
46  );
47  }
48 
52  public function testAddFields($expected)
53  {
54  $this->moduleManagerMock
55  ->expects($this->once())
56  ->method('isOutputEnabled')
57  ->willReturn($expected['isOutputEnabled']);
58 
59  $this->eventObserverMock
60  ->expects($this->exactly($expected['methods_count']))
61  ->method('getForm')
62  ->willReturn($this->formMock);
63 
64  $element = $this->createMock(\Magento\Framework\Data\Form\Element\AbstractElement::class);
65  $this->formMock
66  ->expects($this->exactly($expected['methods_count']))
67  ->method('getElement')
68  ->with('base_fieldset')
69  ->willReturn($element);
70 
71  $element->expects($this->exactly($expected['addField_count']))->method('addField');
72  $this->yesNoMock->expects($this->exactly($expected['yesno_count']))->method('toOptionArray');
73  $this->observerMock->execute($this->eventObserverMock);
74  }
75 
79  public function dataAddFields()
80  {
81  return [
82  [
83  [
84  'isOutputEnabled' => true,
85  'methods_count' => 1,
86  'addField_count' => 2,
87  'yesno_count' => 1,
88  ],
89  ],
90  [
91  [
92  'isOutputEnabled' => false,
93  'methods_count' => 0,
94  'addField_count' => 0,
95  'yesno_count' => 0,
96  ],
97  ],
98  ];
99  }
100 }
$objectManager
Definition: bootstrap.php:17
$element
Definition: element.phtml:12