1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Plugins\Export;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Export;
use PhpMyAdmin\Plugins\Export\ExportCodegen;
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
use PhpMyAdmin\Properties\Options\Items\SelectPropertyItem;
use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Transformations;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use function ob_get_clean;
use function ob_start;
/**
* @covers \PhpMyAdmin\Plugins\Export\ExportCodegen
* @group medium
*/
class ExportCodegenTest extends AbstractTestCase
{
/** @var ExportCodegen */
protected $object;
/**
* Configures global environment.
*/
protected function setUp(): void
{
parent::setUp();
$GLOBALS['server'] = 0;
$this->object = new ExportCodegen(
new Relation($GLOBALS['dbi']),
new Export($GLOBALS['dbi']),
new Transformations()
);
}
/**
* tearDown for test cases
*/
protected function tearDown(): void
{
parent::tearDown();
unset($this->object);
}
public function testInitSpecificVariables(): void
{
$method = new ReflectionMethod(ExportCodegen::class, 'init');
$method->setAccessible(true);
$method->invoke($this->object, null);
$attrCgFormats = new ReflectionProperty(ExportCodegen::class, 'cgFormats');
$attrCgFormats->setAccessible(true);
$this->assertEquals(
[
'NHibernate C# DO',
'NHibernate XML',
],
$attrCgFormats->getValue($this->object)
);
}
public function testSetProperties(): void
{
$method = new ReflectionMethod(ExportCodegen::class, 'setProperties');
$method->setAccessible(true);
$method->invoke($this->object, null);
$attrProperties = new ReflectionProperty(ExportCodegen::class, 'properties');
$attrProperties->setAccessible(true);
$properties = $attrProperties->getValue($this->object);
$this->assertInstanceOf(ExportPluginProperties::class, $properties);
$this->assertEquals(
'CodeGen',
$properties->getText()
);
$this->assertEquals(
'cs',
$properties->getExtension()
);
$this->assertEquals(
'text/cs',
$properties->getMimeType()
);
$this->assertEquals(
'Options',
$properties->getOptionsText()
);
$options = $properties->getOptions();
$this->assertInstanceOf(OptionsPropertyRootGroup::class, $options);
$this->assertEquals(
'Format Specific Options',
$options->getName()
);
$generalOptionsArray = $options->getProperties();
$generalOptions = $generalOptionsArray[0];
$this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
$this->assertEquals(
'general_opts',
$generalOptions->getName()
);
$generalProperties = $generalOptions->getProperties();
$hidden = $generalProperties[0];
$this->assertInstanceOf(HiddenPropertyItem::class, $hidden);
$this->assertEquals(
'structure_or_data',
$hidden->getName()
);
$select = $generalProperties[1];
$this->assertInstanceOf(SelectPropertyItem::class, $select);
$this->assertEquals(
'format',
$select->getName()
);
$this->assertEquals(
'Format:',
$select->getText()
);
$this->assertEquals(
[
'NHibernate C# DO',
'NHibernate XML',
],
$select->getValues()
);
}
public function testExportHeader(): void
{
$this->assertTrue(
$this->object->exportHeader()
);
}
public function testExportFooter(): void
{
$this->assertTrue(
$this->object->exportFooter()
);
}
public function testExportDBHeader(): void
{
$this->assertTrue(
$this->object->exportDBHeader('testDB')
);
}
public function testExportDBFooter(): void
{
$this->assertTrue(
$this->object->exportDBFooter('testDB')
);
}
public function testExportData(): void
{
$GLOBALS['codegen_format'] = 1;
$GLOBALS['output_kanji_conversion'] = false;
$GLOBALS['output_charset_conversion'] = false;
$GLOBALS['buffer_needed'] = false;
$GLOBALS['asfile'] = true;
$GLOBALS['save_on_server'] = false;
ob_start();
$this->object->exportData('test_db', 'test_table', "\n", 'localhost', 'SELECT * FROM `test_db`.`test_table`;');
$result = ob_get_clean();
$this->assertIsString($result);
$this->assertEquals(
'<?xml version="1.0" encoding="utf-8" ?>' . "\n"
. '<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Test_db" assembly="Test_db">' . "\n"
. ' <class name="Test_table" table="Test_table">' . "\n"
. ' <id name="Id" type="Int32" unsaved-value="0">' . "\n"
. ' <column name="id" sql-type="int" not-null="true" unique="true" index="PRIMARY"/>' . "\n"
. ' <generator class="native" />' . "\n"
. ' </id>' . "\n"
. ' <property name="Name" type="String">' . "\n"
. ' <column name="name" sql-type="varchar" not-null="true" />' . "\n"
. ' </property>' . "\n"
. ' <property name="Datetimefield" type="DateTime">' . "\n"
. ' <column name="datetimefield" sql-type="datetime" not-null="true" />' . "\n"
. ' </property>' . "\n"
. ' </class>' . "\n"
. '</hibernate-mapping>',
$result
);
$GLOBALS['codegen_format'] = 4;
$this->object->exportData('test_db', 'test_table', "\n", 'localhost', 'SELECT * FROM `test_db`.`test_table`;');
$this->expectOutputString('4 is not supported.');
}
public function testCgMakeIdentifier(): void
{
$this->assertEquals(
'_Ⅲfoo',
ExportCodegen::cgMakeIdentifier('Ⅲ{}96`{}foo', true)
);
$this->assertEquals(
'TestⅢ',
ExportCodegen::cgMakeIdentifier('`98testⅢ{}96`{}', true)
);
$this->assertEquals(
'testⅢ',
ExportCodegen::cgMakeIdentifier('`98testⅢ{}96`{}', false)
);
}
public function testHandleNHibernateCSBody(): void
{
$method = new ReflectionMethod(ExportCodegen::class, 'handleNHibernateCSBody');
$method->setAccessible(true);
$result = $method->invoke($this->object, 'test_db', 'test_table', "\n");
$this->assertEquals(
'using System;' . "\n" .
'using System.Collections;' . "\n" .
'using System.Collections.Generic;' . "\n" .
'using System.Text;' . "\n" .
'namespace Test_db' . "\n" .
'{' . "\n" .
' #region Test_table' . "\n" .
' public class Test_table' . "\n" .
' {' . "\n" .
' #region Member Variables' . "\n" .
' protected int _id;' . "\n" .
' protected string _name;' . "\n" .
' protected DateTime _datetimefield;' . "\n" .
' #endregion' . "\n" .
' #region Constructors' . "\n" .
' public Test_table() { }' . "\n" .
' public Test_table(string name, DateTime datetimefield)' . "\n" .
' {' . "\n" .
' this._name=name;' . "\n" .
' this._datetimefield=datetimefield;' . "\n" .
' }' . "\n" .
' #endregion' . "\n" .
' #region Public Properties' . "\n" .
' public virtual int Id' . "\n" .
' {' . "\n" .
' get {return _id;}' . "\n" .
' set {_id=value;}' . "\n" .
' }' . "\n" .
' public virtual string Name' . "\n" .
' {' . "\n" .
' get {return _name;}' . "\n" .
' set {_name=value;}' . "\n" .
' }' . "\n" .
' public virtual DateTime Datetimefield' . "\n" .
' {' . "\n" .
' get {return _datetimefield;}' . "\n" .
' set {_datetimefield=value;}' . "\n" .
' }' . "\n" .
' #endregion' . "\n" .
' }' . "\n" .
' #endregion' . "\n" .
'}',
$result
);
}
public function testHandleNHibernateXMLBody(): void
{
$method = new ReflectionMethod(ExportCodegen::class, 'handleNHibernateXMLBody');
$method->setAccessible(true);
$result = $method->invoke($this->object, 'test_db', 'test_table', "\n");
$this->assertEquals(
'<?xml version="1.0" encoding="utf-8" ?>' . "\n" .
'<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Test_db" assembly="Test_db">' . "\n" .
' <class name="Test_table" table="Test_table">' . "\n" .
' <id name="Id" type="Int32" unsaved-value="0">' . "\n" .
' <column name="id" sql-type="int" not-null="true" unique="true" index="PRIMARY"/>' . "\n" .
' <generator class="native" />' . "\n" .
' </id>' . "\n" .
' <property name="Name" type="String">' . "\n" .
' <column name="name" sql-type="varchar" not-null="true" />' . "\n" .
' </property>' . "\n" .
' <property name="Datetimefield" type="DateTime">' . "\n" .
' <column name="datetimefield" sql-type="datetime" not-null="true" />' . "\n" .
' </property>' . "\n" .
' </class>' . "\n" .
'</hibernate-mapping>',
$result
);
}
/**
* Test for
* - PhpMyAdmin\Plugins\Export\ExportCodegen::getCgFormats
* - PhpMyAdmin\Plugins\Export\ExportCodegen::setCgFormats
*/
public function testSetGetCgFormats(): void
{
$reflection = new ReflectionClass(ExportCodegen::class);
$getter = $reflection->getMethod('getCgFormats');
$setter = $reflection->getMethod('setCgFormats');
$getter->setAccessible(true);
$setter->setAccessible(true);
$setter->invoke($this->object, [1, 2]);
$this->assertEquals(
[
1,
2,
],
$getter->invoke($this->object)
);
}
}
|