testUpdatingFormWithNoHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 8
rs 10
c 1
b 0
f 0
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
/**
10
 *
11
 * @psalm-suppress PropertyNotSetInConstructor
12
 */
13
class FormBuilderUnitTest extends TestCase
14
{
15
16
    /**
17
     * Method returns testing data
18
     *
19
     * @param string $name
20
     *            File name
21
     * @return array Testing data
22
     */
23
    protected function getJson(string $name): array
24
    {
25
        return json_decode(file_get_contents(__DIR__ . '/conf/' . $name . '.json'), true);
26
    }
27
28
    /**
29
     * Method constructs FieldsAlgorithms object
30
     *
31
     * @return FieldsAlgorithms Fields algorithms object
32
     */
33
    protected function getFieldsAlgorithms()
34
    {
35
        return new FieldsAlgorithms($this->getJson('form-builder-setup'), 'entity');
36
    }
37
38
    /**
39
     * Setting on and off the form title flag.
40
     *
41
     * @param bool $flag
42
     */
43
    private function formHeader(bool $flag): void
44
    {
45
        if (! $flag) {
46
            $_GET['no-header'] = 1;
47
        } else {
48
            unset($_GET['no-header']);
49
        }
50
    }
51
52
    /**
53
     * Testing data for creation form tests
54
     *
55
     * @return array testing data
56
     */
57
    public function creationFormWidthDataProvider(): array
58
    {
59
        return [
60
            [
61
                $this->getJson('layout')
62
            ],
63
            [
64
                []
65
            ]
66
        ];
67
    }
68
69
    /**
70
     * Testing creation form
71
     *
72
     * @param array $layout
73
     *            layout config
74
     * @dataProvider creationFormWidthDataProvider
75
     * @psalm-suppress RedundantCondition
76
     */
77
    public function testCreationFormWith(array $layout): void
78
    {
79
        // setup
80
        if (isset($_GET)) {
81
            unset($_GET['form-width']);
82
        }
83
        $formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', $layout);
84
85
        $this->formHeader(true);
86
87
        // test body
88
        $content = $formBuilder->creationForm();
89
90
        // assertions
91
        $this->assertStringContainsString('<div class="page-title">', $content, 'No form title was found');
92
        $this->assertStringContainsString('<form', $content, 'No form tag was found');
93
        $this->assertStringContainsString('<textarea', $content, 'No textarea tag was found');
94
        $this->assertStringContainsString('<input', $content, 'No input tag was found');
95
        $this->assertStringContainsString('<select', $content, 'No select tag was found');
96
        $this->assertStringContainsString('<option', $content, 'No option tag was found');
97
        $this->assertStringContainsString('type="file"', $content, 'No file field was found');
98
    }
99
100
    /**
101
     * Common part of the tests testUpdatingFormWithNoHeader and testUpdatingFormWithHeader
102
     *
103
     * @return string form content
104
     */
105
    private function updatingFormTestCommonPart(): string
106
    {
107
        // setup
108
        $formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', $this->getJson('layout'));
109
110
        // test body
111
        $content = $formBuilder->updatingForm('session-id', [
112
            'id' => '23'
113
        ]);
114
115
        // assertions
116
        $this->assertStringContainsString('<form', $content, 'No form tag was found');
117
        $this->assertStringContainsString('<textarea', $content, 'No textarea tag was found');
118
        $this->assertStringContainsString('<input', $content, 'No input tag was found');
119
        $this->assertStringContainsString('<select', $content, 'No select tag was found');
120
        $this->assertStringContainsString('<option', $content, 'No option tag was found');
121
        $this->assertStringContainsString('type="file"', $content, 'No file field was found');
122
123
        return $content;
124
    }
125
126
    /**
127
     * Testing updating form with no header
128
     */
129
    public function testUpdatingFormWithNoHeader(): void
130
    {
131
        // setup
132
        $_GET['no-header'] = 1;
133
134
        $content = $this->updatingFormTestCommonPart();
135
136
        $this->assertStringNotContainsString('<div class="page-title">', $content, 'No form title was found');
137
    }
138
139
    /**
140
     * Testing updating form with header
141
     */
142
    public function testUpdatingFormWithHeader(): void
143
    {
144
        // setup
145
        if (isset($_GET['no-header'])) {
146
            unset($_GET['no-header']);
147
        }
148
149
        $content = $this->updatingFormTestCommonPart();
150
151
        $this->assertStringContainsString('<div class="page-title">', $content, 'No form title was found');
152
    }
153
154
    /**
155
     * Testing constructor with no form title
156
     */
157
    public function testConstructorNoFormTitle(): void
158
    {
159
        // setup
160
        $_GET['form-width'] = 7;
161
        $formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', []);
162
163
        $this->formHeader(false);
164
165
        // test body
166
        $content = $formBuilder->creationForm();
167
168
        // assertions
169
        $this->assertStringNotContainsStringIgnoringCase('<div class="page-title"', $content, 'Form title was found');
170
    }
171
}
172