Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractFormTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Data\Form\AbstractForm;
10 
11 class AbstractFormTest extends \PHPUnit\Framework\TestCase
12 {
17 
22 
26  protected $elementMock;
27 
31  protected $allElementsMock;
32 
36  protected $abstractForm;
37 
38  protected function setUp()
39  {
40  $this->factoryElementMock =
41  $this->createPartialMock(\Magento\Framework\Data\Form\Element\Factory::class, ['create']);
42  $this->factoryCollectionMock =
43  $this->createPartialMock(\Magento\Framework\Data\Form\Element\CollectionFactory::class, ['create']);
44  $this->allElementsMock =
45  $this->createMock(\Magento\Framework\Data\Form\Element\Collection::class);
46  $this->elementMock =
47  $this->createMock(\Magento\Framework\Data\Form\Element\AbstractElement::class);
48 
49  $this->abstractForm = new AbstractForm($this->factoryElementMock, $this->factoryCollectionMock, []);
50  }
51 
52  public function testAddElement()
53  {
54  $this->factoryCollectionMock
55  ->expects($this->once())
56  ->method('create')
57  ->will($this->returnValue($this->allElementsMock));
58  $this->elementMock->expects($this->once())->method('setForm');
59  $this->allElementsMock->expects($this->once())->method('add')->with($this->elementMock, false);
60  $this->abstractForm->addElement($this->elementMock, false);
61  }
62 
63  public function testAddField()
64  {
65  $config = ['name' => 'store_type', 'no_span' => true, 'value' => 'value'];
66  $this->factoryElementMock
67  ->expects($this->once())
68  ->method('create')
69  ->with('hidden', ['data' => $config])
70  ->will($this->returnValue($this->elementMock));
71  $this->elementMock->expects($this->once())->method('setId')->with('store_type');
72  $this->factoryCollectionMock
73  ->expects($this->once())
74  ->method('create')
75  ->will($this->returnValue($this->allElementsMock));
76  $this->allElementsMock->expects($this->once())->method('add')->with($this->elementMock, false);
77  $this->assertEquals($this->elementMock, $this->abstractForm->addField('store_type', 'hidden', $config));
78  $this->abstractForm->removeField('hidden');
79  }
80 
81  public function testAddFieldset()
82  {
83  $config = ['name' => 'store_type', 'no_span' => true, 'value' => 'value'];
84  $this->factoryElementMock
85  ->expects($this->once())
86  ->method('create')
87  ->with('fieldset', ['data' => $config])
88  ->will($this->returnValue($this->elementMock));
89  $this->elementMock->expects($this->once())->method('setId')->with('hidden');
90  $this->elementMock->expects($this->once())->method('setAdvanced')->with(false);
91  $this->elementMock->expects($this->once())->method('setForm');
92  $this->factoryCollectionMock
93  ->expects($this->once())
94  ->method('create')
95  ->will($this->returnValue($this->allElementsMock));
96  $this->allElementsMock->expects($this->once())->method('add')->with($this->elementMock, false);
97  $this->abstractForm->addFieldset('hidden', $config);
98  }
99 
100  public function testAddColumn()
101  {
102  $config = ['name' => 'store_type', 'no_span' => true, 'value' => 'value'];
103  $this->factoryElementMock
104  ->expects($this->once())
105  ->method('create')
106  ->with('column', ['data' => $config])
107  ->will($this->returnValue($this->elementMock));
108  $this->elementMock->expects($this->once())->method('setId')->with('hidden');
109  $this->elementMock->expects($this->exactly(2))->method('setForm')->will($this->returnSelf());
110  $this->factoryCollectionMock
111  ->expects($this->once())
112  ->method('create')
113  ->will($this->returnValue($this->allElementsMock));
114  $this->allElementsMock->expects($this->once())->method('add')->with($this->elementMock, false);
115  $this->abstractForm->addColumn('hidden', $config);
116  }
117 
118  public function testAddCustomAttribute()
119  {
120  $this->assertEquals(
121  $this->abstractForm,
122  $this->abstractForm->addCustomAttribute('attribute_key1', 'attribute_value1')
123  );
124 
125  $form = clone $this->abstractForm;
126  $this->assertNotEquals(
127  $form,
128  $this->abstractForm->addCustomAttribute('attribute_key2', 'attribute_value2')
129  );
130  }
131 
139  public function testSerialize(
140  $keys,
141  $data,
143  $result
144  ) {
145  foreach ($data as $key => $value) {
146  $this->abstractForm->setData($key, $value);
147  }
148 
149  foreach ($customAttributes as $key => $value) {
150  $this->abstractForm->addCustomAttribute($key, $value);
151  }
152 
153  $this->assertEquals($result, $this->abstractForm->serialize($keys));
154  }
155 
164  public function dataProviderSerialize()
165  {
166  return [
167  [[], [], [], ''],
168  [['key1'], [], [], ''],
169  [['key1'], ['key1' => 'value'], [], 'key1="value"'],
170  [['key1', 'key2'], ['key1' => 'value'], [], 'key1="value"'],
171  [['key1', 'key2'], ['key1' => 'value', 'key3' => 'value3'], [], 'key1="value"'],
172  [['key1', 'key2'], ['key1' => 'value', 'key3' => 'value3'], ['custom1' => ''], 'key1="value"'],
173  [
174  [
175  'key1',
176  'key2',
177  ],
178  [
179  'key1' => 'value',
180  'key3' => 'value3',
181  ],
182  [
183  'custom1' => 'custom_value1',
184  ],
185  'key1="value" custom1="custom_value1"'
186  ],
187  [
188  [
189  'key1',
190  'key2',
191  ],
192  [
193  'key1' => 'value',
194  'key3' => 'value3',
195  ],
196  [
197  'custom1' => 'custom_value1',
198  'custom2' => '',
199  'custom3' => 0,
200  'custom4' => false,
201  'custom5' => null,
202  ],
203  'key1="value" custom1="custom_value1"'
204  ],
205  ];
206  }
207 }
$config
Definition: fraud_order.php:17
$value
Definition: gender.phtml:16
testSerialize( $keys, $data, $customAttributes, $result)