Backport two test methods for phpunit 8.5
[phpmyadmin.git] / test / classes / Config / FormTest.php
blobc7647d2c8df8f2d1fe31f309ab474d66b7d0643f
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests\Config;
7 use PhpMyAdmin\Config\ConfigFile;
8 use PhpMyAdmin\Config\Form;
9 use PhpMyAdmin\Tests\AbstractTestCase;
10 use ReflectionClass;
11 use ReflectionProperty;
13 use function array_keys;
14 use function preg_match;
16 /**
17 * @covers \PhpMyAdmin\Config\Form
19 class FormTest extends AbstractTestCase
21 /** @var Form */
22 protected $object;
24 /**
25 * Configures global environment.
27 protected function setUp(): void
29 parent::setUp();
30 parent::setTheme();
31 parent::setGlobalConfig();
32 $GLOBALS['server'] = 0;
33 $this->object = new Form(
34 'pma_form_name',
36 'pma_form1',
37 'pma_form2',
39 new ConfigFile(),
44 /**
45 * tearDown for test cases
47 protected function tearDown(): void
49 parent::tearDown();
50 unset($this->object);
53 /**
54 * Test for Form::__constructor
56 * @group medium
58 public function testContructor(): void
60 $this->assertEquals(1, $this->object->index);
61 $this->assertEquals('pma_form_name', $this->object->name);
62 $this->assertArrayHasKey('pma_form1', $this->object->fields);
65 /**
66 * Test for Form::getOptionType
68 public function testGetOptionType(): void
70 $attrFieldsTypes = new ReflectionProperty(Form::class, 'fieldsTypes');
71 $attrFieldsTypes->setAccessible(true);
72 $attrFieldsTypes->setValue(
73 $this->object,
74 ['7' => 'Seven']
77 $this->assertNull(
78 $this->object->getOptionType('123/4/5/6')
81 $this->assertEquals(
82 'Seven',
83 $this->object->getOptionType('123/4/5/7')
87 /**
88 * Test for Form::getOptionValueList
90 public function testGetOptionValueList(): void
92 $this->assertEquals(
94 'NHibernate C# DO',
95 'NHibernate XML',
97 $this->object->getOptionValueList('Export/codegen_format')
100 $this->assertEquals(
102 'auto' => 'auto',
103 '1' => 1,
104 '0' => 0,
106 $this->object->getOptionValueList('OBGzip')
109 $this->assertEquals(
111 'none' => 'Nowhere',
112 'left' => 'Left',
113 'right' => 'Right',
114 'both' => 'Both',
116 $this->object->getOptionValueList('RowActionLinks')
121 * Test for Form::readFormPathsCallback
123 public function testReadFormPathsCallBack(): void
125 $reflection = new ReflectionClass(Form::class);
126 $method = $reflection->getMethod('readFormPathsCallback');
127 $method->setAccessible(true);
129 $array = [
130 'foo' => [
131 'bar' => [
132 'test' => 1,
133 1 => ':group:end',
138 $method->invoke($this->object, $array, 'foo', 'pref');
140 $result = $this->object->fields;
142 $this->assertCount(4, $result);
144 $this->assertEquals('pma_form1', $result['pma_form1']);
146 $this->assertEquals('pma_form2', $result['pma_form2']);
148 $this->assertEquals('preffoo/foo/bar/test', $result[0]);
150 $this->assertIsString($result[1]);
152 // needs regexp because the counter is static
153 $this->assertMatchesRegularExpression('/^preffoo\/foo\/bar\/\:group\:end\:\d+$/', $result[1]);
157 * Test for Form::readFormPaths
159 public function testReadFormPaths(): void
161 $reflection = new ReflectionClass(Form::class);
162 $method = $reflection->getMethod('readFormPaths');
163 $method->setAccessible(true);
165 $array = [
166 'foo' => [
167 'bar' => [
168 'test' => 1,
169 1 => ':group:end',
174 $method->invoke($this->object, $array);
176 $result = $this->object->fields;
178 $this->assertCount(2, $result);
180 $this->assertEquals('foo/bar/test', $result['test']);
182 unset($result['test']);
184 // needs regexp because the counter is static
186 $keys = array_keys($result);
187 $key = $keys[0];
188 $this->assertIsString($key);
189 $this->assertMatchesRegularExpression('/^\:group\:end\:(\d+)$/', $key);
191 preg_match('/^\:group\:end\:(\d+)$/', $key, $matches);
192 $digit = $matches[1];
194 $this->assertEquals('foo/bar/:group:end:' . $digit, $result[':group:end:' . $digit]);
198 * Test for Form::readTypes
200 public function testReadTypes(): void
202 $reflection = new ReflectionClass(Form::class);
203 $method = $reflection->getMethod('readTypes');
204 $method->setAccessible(true);
206 $this->object->fields = [
207 'pma_form1' => 'Servers/1/port',
208 'pma_form2' => 'Servers/1/auth_type',
209 ':group:end:0' => 'preffoo/foo/bar/test',
210 '1' => 'preffoo/foo/bar/:group:end:0',
213 $attrFieldsTypes = $reflection->getProperty('fieldsTypes');
214 $attrFieldsTypes->setAccessible(true);
216 $method->invoke($this->object, null);
218 $this->assertEquals(
220 'pma_form1' => 'integer',
221 'pma_form2' => 'select',
222 ':group:end:0' => 'group',
223 '1' => 'NULL',
225 $attrFieldsTypes->getValue($this->object)
230 * Test for Form::loadForm
232 public function testLoadForm(): void
234 $this->object = $this->getMockBuilder(Form::class)
235 ->disableOriginalConstructor()
236 ->onlyMethods(['readFormPaths', 'readTypes'])
237 ->getMock();
239 $this->object->expects($this->exactly(1))
240 ->method('readFormPaths')
241 ->with(['testForm']);
243 $this->object->expects($this->exactly(1))
244 ->method('readTypes');
246 $this->object->loadForm('pmaform', ['testForm']);
248 $this->assertEquals('pmaform', $this->object->name);
252 * Test for Form::cleanGroupPaths
254 public function testCleanGroupPaths(): void
256 $this->object = $this->getMockBuilder(Form::class)
257 ->disableOriginalConstructor()
258 ->onlyMethods(['readFormPaths', 'readTypes'])
259 ->getMock();
261 $this->object->expects($this->exactly(1))->method('readFormPaths')->with([
262 ':group:OpenDocument-OpenOffice 試算表',
263 'group:test/data',
264 'Export/ods_columns',
265 'Export/ods_null',
266 ':group:end',
269 $this->object->loadForm('pmaform', [
270 ':group:OpenDocument/OpenOffice 試算表',
271 'group:test/data',
272 'Export/ods_columns',
273 'Export/ods_null',
274 ':group:end',