Passed
Push — master ( 7aa904...662637 )
by Alex
02:49
created

testUpdatingFormWithNoHeader()   A

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
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
    private 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 data for creation form tests
50
     *
51
     * @return array testing data
52
     */
53
    public function creationFormWidthDataProvider(): array
54
    {
55
        return [
56
            [
57
                $this->getJson('layout')
58
            ],
59
            [
60
                []
61
            ]
62
        ];
63
    }
64
    
65
    /**
66
     * Testing creation form
67
     *
68
     * @param array $layout
69
     *            layout config
70
     * @dataProvider creationFormWidthDataProvider
71
     */
72
    public function testCreationFormWith(array $layout): void
73
    {
74
        // setup
75
        if (isset($_GET)) {
76
            unset($_GET['form-width']);
77
        }
78
        $formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', $layout);
79
        
80
        $this->formHeader(true);
81
        
82
        // test body
83
        $content = $formBuilder->creationForm();
84
        
85
        // assertions
86
        $this->assertStringContainsString('<div class="page-title">', $content, 'No form title was found');
87
        $this->assertStringContainsString('<form', $content, 'No form tag was found');
88
        $this->assertStringContainsString('<textarea', $content, 'No textarea tag was found');
89
        $this->assertStringContainsString('<input', $content, 'No input tag was found');
90
        $this->assertStringContainsString('<select', $content, 'No select tag was found');
91
        $this->assertStringContainsString('<option', $content, 'No option tag was found');
92
        $this->assertStringContainsString('type="file"', $content, 'No file field was found');
93
    }
94
    
95
    /**
96
     * Common part of the tests testUpdatingFormWithNoHeader and testUpdatingFormWithHeader
97
     *
98
     * @return string form content
99
     */
100
    private function updatingFormTestCommonPart(): string
101
    {
102
        // setup
103
        $formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', $this->getJson('layout'));
104
        
105
        // test body
106
        $content = $formBuilder->updatingForm('session-id', [
107
            'id' => '23'
108
        ]);
109
        
110
        // assertions
111
        $this->assertStringContainsString('<form', $content, 'No form tag was found');
112
        $this->assertStringContainsString('<textarea', $content, 'No textarea tag was found');
113
        $this->assertStringContainsString('<input', $content, 'No input tag was found');
114
        $this->assertStringContainsString('<select', $content, 'No select tag was found');
115
        $this->assertStringContainsString('<option', $content, 'No option tag was found');
116
        $this->assertStringContainsString('type="file"', $content, 'No file field was found');
117
        
118
        return $content;
119
    }
120
    
121
    /**
122
     * Testing updating form with no header
123
     */
124
    public function testUpdatingFormWithNoHeader(): void
125
    {
126
        // setup
127
        $_GET['no-header'] = 1;
128
        
129
        $content = $this->updatingFormTestCommonPart();
130
        
131
        $this->assertStringNotContainsString('<div class="page-title">', $content, 'No form title was found');
132
    }
133
    
134
    /**
135
     * Testing updating form with header
136
     */
137
    public function testUpdatingFormWithHeader(): void
138
    {
139
        // setup
140
        if (isset($_GET['no-header'])) {
141
            unset($_GET['no-header']);
142
        }
143
        
144
        $content = $this->updatingFormTestCommonPart();
145
        
146
        $this->assertStringContainsString('<div class="page-title">', $content, 'No form title was found');
147
    }
148
    
149
    /**
150
     * Testing constructor with no form title
151
     */
152
    public function testConstructorNoFormTitle(): void
153
    {
154
        // setup
155
        $_GET['form-width'] = 7;
156
        $formBuilder = new FormBuilder($this->getFieldsAlgorithms(), SESSION_ID, 'test-record', []);
157
        
158
        $this->formHeader(false);
159
        
160
        // test body
161
        $content = $formBuilder->creationForm();
162
        
163
        // assertions
164
        $this->assertStringNotContainsStringIgnoringCase('<div class="page-title"', $content, 'Form title was found');
165
    }
166
}
167