Completed
Push — master ( 2b7ec8...e65692 )
by Alex
09:34
created

FieldsAlgorithmsUnitTest::testGetFieldsNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Gui\Tests;
3
4
define('ID_FIELD_NAME', 'id');
5
define('TITLE_FIELD_NAME', 'title');
6
define('USER_ID_FIELD_NAME', 'user_id');
7
define('FIELDS_FIELD_NAME', 'fields');
8
define('DISABLED_FIELD_NAME', 'disabled');
9
define('STRING_TYPE_NAME', 'string');
10
define('INTEGER_TYPE_NAME', 'integer');
11
define('DATE_TYPE_NAME', 'date');
12
define('EXTERNAL_TYPE_NAME', 'external');
13
14
class FieldsAlgorithmsUnitTest extends \PHPUnit\Framework\TestCase
15
{
16
17
    /**
18
     * Post test processing
19
     */
20
    public function tearDown(): void
21
    {
22
        unset($_GET[FIELDS_FIELD_NAME]);
23
    }
24
25
    /**
26
     * Method creates testing data
27
     *
28
     * @return array Testing data
29
     */
30
    protected function getFields1(): array
31
    {
32
        return json_decode(file_get_contents(__DIR__ . '/conf/setup.json'), true);
33
    }
34
35
    /**
36
     * Method creates testing data
37
     *
38
     * @return array Testing data
39
     */
40
    protected function getFields2(): array
41
    {
42
        return [
43
            ID_FIELD_NAME => [
44
                'type' => INTEGER_TYPE_NAME,
45
                DISABLED_FIELD_NAME => 1
46
            ],
47
            TITLE_FIELD_NAME => [
48
                'type' => STRING_TYPE_NAME,
49
                'required' => 1
50
            ]
51
        ];
52
    }
53
54
    /**
55
     * Testing invalid construction
56
     */
57
    public function testConstructor()
58
    {
59
        // setup and test body
60
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
61
62
        // assertions
63
        $this->assertEquals('entity', $fieldsAlgorithms->getEntityName(), 'EntityName was not set');
64
        $this->assertTrue($fieldsAlgorithms->hasCustomFields(), 'Data was not loaded');
65
    }
66
67
    /**
68
     * Testing hasCustomFields
69
     */
70
    public function testHasNotCustomFields()
71
    {
72
        // setup and test body
73
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields2(), 'entity');
74
75
        // assertions
76
        $_GET[FIELDS_FIELD_NAME] = TITLE_FIELD_NAME;
77
        $this->assertFalse($fieldsAlgorithms->hasCustomFields(), 'Custom fields are not in the model');
78
    }
79
80
    /**
81
     * Testing hasCustomFields
82
     */
83
    public function testHasCustomFields()
84
    {
85
        // setup and test body
86
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
87
88
        // assertions
89
        $this->assertTrue($fieldsAlgorithms->hasCustomFields(), 'Custom fields are in the model');
90
    }
91
92
    /**
93
     * Testing getTypedValue
94
     */
95
    public function testGetTypedValue()
96
    {
97
        // setup and test body
98
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
99
100
        // assertions int
101
        $this->assertEquals(
102
            1,
103
            $fieldsAlgorithms->getTypedValue(INTEGER_TYPE_NAME, '1'),
104
            'Type was not casted properly for integer');
105
        $this->assertTrue(
106
            is_int($fieldsAlgorithms->getTypedValue(INTEGER_TYPE_NAME, '1')),
107
            'Type was not casted properly for integer');
108
109
        // assertions string
110
        $this->assertEquals(
111
            '1',
112
            $fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '1'),
113
            'Type was not casted properly for string');
114
        $this->assertTrue(
115
            is_string($fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '1')),
116
            'Return type is not correct');
117
        $this->assertEquals(
118
            '&amp;',
119
            $fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '&'),
120
            'Type was not casted properly for string');
121
        $this->assertEquals(
122
            '&quot;&quot;',
123
            $fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '""'),
124
            'Default brunch for string is not working');
125
126
        // assertions date
127
        $this->assertEquals(
128
            '2019-01-01',
129
            $fieldsAlgorithms->getTypedValue(DATE_TYPE_NAME, '2019-01-01'),
130
            'Type was not casted properly for date');
131
        $this->assertEquals(
132
            '',
133
            $fieldsAlgorithms->getTypedValue(DATE_TYPE_NAME, '""'),
134
            'Default date for string is not working');
135
136
        // assertions file
137
        $this->assertContains('value', $fieldsAlgorithms->getTypedValue('file', [
138
            'value'
139
        ], false));
140
        $this->assertFileExists(
141
            $path = $fieldsAlgorithms->getTypedValue('file', [
142
                'name' => 'test.txt',
143
                'file' => '1234'
144
            ], true),
145
            'File was not saved');
146
        unlink($path);
147
148
        // assertions external
149
        $typedValue = $fieldsAlgorithms->getTypedValue(EXTERNAL_TYPE_NAME, [
150
            '1',
151
            '2'
152
        ]);
153
        $this->assertContains(1, $typedValue);
154
        $this->assertContains(2, $typedValue);
155
        $this->assertCount(2, $typedValue);
0 ignored issues
show
Bug introduced by
It seems like $typedValue can also be of type integer and string; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

155
        $this->assertCount(2, /** @scrutinizer ignore-type */ $typedValue);
Loading history...
156
157
        // assertion unexisting
158
        $this->expectException(\Exception::class);
159
        $fieldsAlgorithms->getTypedValue('unexisting', '1');
160
    }
161
162
    /**
163
     * Test validateFieldExistance method
164
     */
165
    public function testValidateFieldExistance()
166
    {
167
        // setup and test body
168
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
169
170
        // test body and assertions
171
        $this->expectException(\Exception::class);
172
        $fieldsAlgorithms->validateFieldExistance('unexisting-field');
173
174
        $this->expectException(\Exception::class);
175
        $fieldsAlgorithms->validateFieldExistance('id');
176
    }
177
178
    /**
179
     * Test getSecureValue method
180
     */
181
    public function testGetSecureValue()
182
    {
183
        // setup and test body
184
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
185
186
        // test body and assertions
187
        $id = $fieldsAlgorithms->getSecureValue('id', '1');
188
189
        // assertions
190
        $this->assertIsInt($id, 'Invalid secure processing for integer value');
191
        $this->assertEquals(1, $id, 'Data loss for integer value');
192
    }
193
194
    /**
195
     * Test getSecureValues method
196
     */
197
    public function testGetSecureValues()
198
    {
199
        // setup and test body
200
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
201
202
        // test body and assertions
203
        $id = $fieldsAlgorithms->getSecureValues('id', [
204
            '1',
205
            '2&'
206
        ]);
207
208
        // assertions
209
        $this->assertIsInt($id[0], 'Invalid secure processing for integer values');
210
        $this->assertIsInt($id[1], 'Invalid secure processing for integer values');
211
212
        $this->assertEquals(1, $id[0], 'Data loss for integer values');
213
        $this->assertEquals(2, $id[1], 'Data loss for integer values');
214
    }
215
216
    /**
217
     * Test getValuesForPrefix method
218
     */
219
    public function testGetValuesForPrefix()
220
    {
221
        // setup and test body
222
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
223
        $_POST['prefix-id'] = '1';
224
        $_POST['prefix-title'] = 'some string';
225
226
        // test body and assertions
227
        $result = $fieldsAlgorithms->getValuesForPrefix('prefix-');
228
229
        // assertions
230
        $this->assertIsInt($result['id'], 'Invalid secure processing for integer prefix');
231
        $this->assertIsString($result[TITLE_FIELD_NAME], 'Invalid secure processing for string prefix');
232
233
        $this->assertEquals(1, $result['id'], 'Data loss for integer preix');
234
        $this->assertEquals('some string', $result[TITLE_FIELD_NAME], 'Data loss for string preix');
235
    }
236
237
    /**
238
     * Testing 'removeField' method
239
     */
240
    public function testRemoveField()
241
    {
242
        // setup
243
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
244
245
        // test body
246
        $fieldsAlgorithms->removeField('extensions');
247
248
        // assertions
249
        $this->assertFalse($fieldsAlgorithms->hasCustomFields(), 'Field "extensions" was not removed');
250
    }
251
252
    /**
253
     * Testing 'fetchCustomField' method for unexisting field
254
     */
255
    public function testFetchCustomFieldUnexistingField()
256
    {
257
        // setup
258
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
259
        $record = [];
260
261
        // test body
262
        $result = $fieldsAlgorithms->fetchCustomField($record, 'unexisting');
263
264
        // assertions
265
        $this->assertEquals(0, count($result), 'Something was returned, but should not');
266
    }
267
268
    /**
269
     * Testing 'fetchCustomField' method
270
     */
271
    public function testFetchCustomField()
272
    {
273
        // setup
274
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
275
        $record = [];
276
        $_POST['entity' . '-balance'] = '11';
277
278
        // test body
279
        $result = $fieldsAlgorithms->fetchCustomField($record, 'extensions');
280
281
        // assertions
282
        $this->assertEquals(11, $result['balance'], 'Invalid field value');
283
    }
284
285
    /**
286
     * Testing 'fetchField' method
287
     */
288
    public function testFetchField()
289
    {
290
        // setup
291
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
292
        $record = [];
293
        $_POST['entity' . '-id'] = '11';
294
        $_FILES['entity' . '-avatar'] = [
295
            'name' => 'test.dat',
296
            'file' => 'content'
297
        ];
298
        $_POST['entity' . '-balance'] = '33';
299
300
        // test body
301
        $fieldsAlgorithms->fetchField($record, 'id');
302
        $fieldsAlgorithms->fetchField($record, 'avatar');
303
        $fieldsAlgorithms->fetchField($record, 'extensions');
304
305
        // assertions
306
        $this->assertEquals(11, $record['id'], 'id was not fetched');
307
        $avatar = $record['avatar'];
308
        $this->assertFileExists($avatar, 'File does not exists');
309
        unlink($avatar);
310
        $this->assertEquals(33, $record['balance'], 'balance was not fetched');
311
    }
312
313
    /**
314
     * Testing 'getObject' method
315
     */
316
    public function testGetObject()
317
    {
318
        // setup
319
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
320
321
        // test body
322
        $object = $fieldsAlgorithms->getObject('title');
323
324
        // assertions
325
        $this->assertInstanceOf(\Mezon\Gui\Field\InputText::class, $object);
326
    }
327
328
    /**
329
     * Testing 'getFieldsNames' method
330
     */
331
    public function testGetFieldsNames()
332
    {
333
        // setup
334
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
335
336
        // test body
337
        $fields = $fieldsAlgorithms->getFieldsNames();
338
339
        // assertions
340
        $this->assertContains('id', $fields);
341
        $this->assertContains('title', $fields);
342
        $this->assertContains('user_id', $fields);
343
        $this->assertContains('label', $fields);
344
        $this->assertContains('description', $fields);
345
        $this->assertContains('created', $fields);
346
        $this->assertContains('avatar', $fields);
347
        $this->assertContains('parts', $fields);
348
        $this->assertContains('extensions', $fields);
349
    }
350
351
    /**
352
     * Testing field compilation
353
     */
354
    public function testGetCompiledField(): void
355
    {
356
        // setup
357
        $fieldsAlgorithms = new \Mezon\Gui\FieldsAlgorithms($this->getFields1(), 'entity');
358
359
        // test body
360
        $inputField = $fieldsAlgorithms->getCompiledField('title');
361
        $textareaField = $fieldsAlgorithms->getCompiledField('description');
362
363
        // assertions
364
        $this->assertStringContainsString('<input ', $inputField);
365
        $this->assertStringContainsString('<textarea ', $textareaField);
366
    }
367
}
368