1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Gui\FormBuilder\Tests; |
3
|
|
|
|
4
|
|
|
use Mezon\Gui\FormBuilder\FormBuilder; |
5
|
|
|
use Mezon\Gui\FieldsAlgorithms; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
define('SESSION_ID', 'session-id'); |
8
|
|
|
|
9
|
|
|
class FormBuilderUnitTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Method returns testing data |
14
|
|
|
* |
15
|
|
|
* @param string $name |
16
|
|
|
* File name |
17
|
|
|
* @return array Testing data |
18
|
|
|
*/ |
19
|
|
|
protected function getJson(string $name): array |
20
|
|
|
{ |
21
|
|
|
return json_decode(file_get_contents(__DIR__ . '/conf/' . $name . '.json'), true); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Method constructs FieldsAlgorithms object |
26
|
|
|
* |
27
|
|
|
* @return FieldsAlgorithms Fields algorithms object |
28
|
|
|
*/ |
29
|
|
|
protected function getFieldsAlgorithms() |
30
|
|
|
{ |
31
|
|
|
return new FieldsAlgorithms($this->getJson('form-builder-setup'), 'entity'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Setting on and off the form title flag. |
36
|
|
|
* |
37
|
|
|
* @param bool $flag |
38
|
|
|
*/ |
39
|
|
|
protected function formHeader(bool $flag) |
40
|
|
|
{ |
41
|
|
|
if (! $flag) { |
42
|
|
|
$_GET['no-header'] = 1; |
43
|
|
|
} else { |
44
|
|
|
unset($_GET['no-header']); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Testing creation form |
50
|
|
|
*/ |
51
|
|
|
public function testCreationForm(): void |
52
|
|
|
{ |
53
|
|
|
// setup |
54
|
|
|
$formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', $this->getJson('layout')); |
55
|
|
|
|
56
|
|
|
$this->formHeader(true); |
57
|
|
|
|
58
|
|
|
// test body |
59
|
|
|
$content = $formBuilder->creationForm(); |
60
|
|
|
|
61
|
|
|
// assertions |
62
|
|
|
$this->assertStringContainsString('<div class="page-title">', $content, 'No form title was found'); |
63
|
|
|
$this->assertStringContainsString('<form', $content, 'No form tag was found'); |
64
|
|
|
$this->assertStringContainsString('<textarea', $content, 'No textarea tag was found'); |
65
|
|
|
$this->assertStringContainsString('<input', $content, 'No input tag was found'); |
66
|
|
|
$this->assertStringContainsString('<select', $content, 'No select tag was found'); |
67
|
|
|
$this->assertStringContainsString('<option', $content, 'No option tag was found'); |
68
|
|
|
$this->assertStringContainsString('type="file"', $content, 'No file field was found'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Testing creation form |
73
|
|
|
*/ |
74
|
|
|
public function testUpdatingForm(): void |
75
|
|
|
{ |
76
|
|
|
// setup |
77
|
|
|
$formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', $this->getJson('layout')); |
78
|
|
|
|
79
|
|
|
$this->formHeader(true); |
80
|
|
|
|
81
|
|
|
// test body |
82
|
|
|
$content = $formBuilder->updatingForm('session-id', [ |
83
|
|
|
'id' => '23' |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
// assertions |
87
|
|
|
$this->assertStringContainsString('<div class="page-title">', $content, 'No form title was found'); |
88
|
|
|
$this->assertStringContainsString('<form', $content, 'No form tag was found'); |
89
|
|
|
$this->assertStringContainsString('<textarea', $content, 'No textarea tag was found'); |
90
|
|
|
$this->assertStringContainsString('<input', $content, 'No input tag was found'); |
91
|
|
|
$this->assertStringContainsString('<select', $content, 'No select tag was found'); |
92
|
|
|
$this->assertStringContainsString('<option', $content, 'No option tag was found'); |
93
|
|
|
$this->assertStringContainsString('type="file"', $content, 'No file field was found'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Testing constructor with no form title |
98
|
|
|
*/ |
99
|
|
|
public function testConstructorNoFormTitle(): void |
100
|
|
|
{ |
101
|
|
|
// setup |
102
|
|
|
$_GET['form-width'] = 7; |
103
|
|
|
$formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', []); |
104
|
|
|
|
105
|
|
|
$this->formHeader(false); |
106
|
|
|
|
107
|
|
|
// test body |
108
|
|
|
$content = $formBuilder->creationForm(); |
109
|
|
|
|
110
|
|
|
// assertions |
111
|
|
|
$this->assertStringNotContainsStringIgnoringCase('<div class="page-title"', $content, 'Form title was found'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|