Passed
Push — master ( 4d86c4...586e6b )
by Alex
02:58
created

Mezon/Gui/Tests/FieldsAlgorithmsUnitTest.php (1 issue)

Labels
Severity
1
<?php
2
namespace Mezon\Gui\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Gui\FieldsAlgorithms;
6
use Mezon\Conf\Conf;
7
use Mezon\Fs\InMemory;
8
define('ID_FIELD_NAME', 'id');
9
define('TITLE_FIELD_NAME', 'title');
10
define('USER_ID_FIELD_NAME', 'user_id');
11
define('FIELDS_FIELD_NAME', 'fields');
12
define('DISABLED_FIELD_NAME', 'disabled');
13
define('STRING_TYPE_NAME', 'string');
14
define('INTEGER_TYPE_NAME', 'integer');
15
define('DATE_TYPE_NAME', 'date');
16
define('EXTERNAL_TYPE_NAME', 'external');
17
18
/**
19
 *
20
 * @psalm-suppress PropertyNotSetInConstructor
21
 */
22
class FieldsAlgorithmsUnitTest extends TestCase
23
{
24
25
    /**
26
     *
27
     * {@inheritdoc}
28
     * @see TestCase::setUp()
29
     */
30
    protected function setUp(): void
31
    {
32
        Conf::setConfigValue('fs/layer', 'mock');
33
        InMemory::clearFs();
34
    }
35
36
    /**
37
     * Post test processing
38
     */
39
    public function tearDown(): void
40
    {
41
        unset($_GET[FIELDS_FIELD_NAME]);
42
    }
43
44
    /**
45
     * Method creates testing data
46
     *
47
     * @return array Testing data
48
     */
49
    protected function getFields1(): array
50
    {
51
        return json_decode(file_get_contents(__DIR__ . '/conf/setup.json'), true);
52
    }
53
54
    /**
55
     * Method creates testing data
56
     *
57
     * @return array Testing data
58
     */
59
    protected function getFields2(): array
60
    {
61
        return [
62
            ID_FIELD_NAME => [
63
                'type' => INTEGER_TYPE_NAME,
64
                DISABLED_FIELD_NAME => 1
65
            ],
66
            TITLE_FIELD_NAME => [
67
                'type' => STRING_TYPE_NAME,
68
                'required' => 1
69
            ]
70
        ];
71
    }
72
73
    /**
74
     * Testing invalid construction
75
     */
76
    public function testConstructor(): void
77
    {
78
        // setup and test body
79
        $fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity');
80
81
        // assertions
82
        $this->assertEquals('entity', $fieldsAlgorithms->getEntityName(), 'EntityName was not set');
83
        $this->assertTrue($fieldsAlgorithms->hasCustomFields(), 'Data was not loaded');
84
    }
85
86
    /**
87
     * Testing hasCustomFields
88
     */
89
    public function testHasNotCustomFields(): void
90
    {
91
        // setup and test body
92
        $fieldsAlgorithms = new FieldsAlgorithms($this->getFields2(), 'entity');
93
94
        // assertions
95
        $_GET[FIELDS_FIELD_NAME] = TITLE_FIELD_NAME;
96
        $this->assertFalse($fieldsAlgorithms->hasCustomFields(), 'Custom fields are not in the model');
97
    }
98
99
    /**
100
     * Testing hasCustomFields
101
     */
102
    public function testHasCustomFields(): void
103
    {
104
        // setup and test body
105
        $fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity');
106
107
        // assertions
108
        $this->assertTrue($fieldsAlgorithms->hasCustomFields(), 'Custom fields are in the model');
109
    }
110
111
    /**
112
     * Testing getTypedValue
113
     */
114
    public function testGetTypedValue(): void
115
    {
116
        // setup and test body
117
        $fieldsAlgorithms = new FieldsAlgorithms($this->getFields1(), 'entity');
118
119
        // assertions int
120
        $this->assertEquals(
121
            1,
122
            $fieldsAlgorithms->getTypedValue(INTEGER_TYPE_NAME, '1'),
123
            'Type was not casted properly for integer');
124
        $this->assertTrue(
125
            is_int($fieldsAlgorithms->getTypedValue(INTEGER_TYPE_NAME, '1')),
126
            'Type was not casted properly for integer');
127
128
        // assertions string
129
        $this->assertEquals(
130
            '1',
131
            $fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '1'),
132
            'Type was not casted properly for string');
133
        $this->assertTrue(
134
            is_string($fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '1')),
135
            'Return type is not correct');
136
        $this->assertEquals(
137
            '&amp;',
138
            $fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '&'),
139
            'Type was not casted properly for string');
140
        $this->assertEquals(
141
            '&quot;&quot;',
142
            $fieldsAlgorithms->getTypedValue(STRING_TYPE_NAME, '""'),
143
            'Default brunch for string is not working');
144
145
        // assertions date
146
        $this->assertEquals(
147
            '2019-01-01',
148
            $fieldsAlgorithms->getTypedValue(DATE_TYPE_NAME, '2019-01-01'),
149
            'Type was not casted properly for date');
150
        $this->assertEquals(
151
            '',
152
            $fieldsAlgorithms->getTypedValue(DATE_TYPE_NAME, '""'),
153
            'Default date for string is not working');
154
155
        // assertions file
156
        $this->assertContains('value', $fieldsAlgorithms->getTypedValue('file', [
157
            'value'
158
        ], false));
159
        $path = $fieldsAlgorithms->getTypedValue('file', [
160
            'name' => 'test.txt',
161
            'file' => '1234'
162
        ], true);
163
        $this->assertEquals(base64_decode('1234'), InMemory::fileGetContents($path));
164
165
        // assertions external
166
        $typedValue = $fieldsAlgorithms->getTypedValue(EXTERNAL_TYPE_NAME, [
167
            '1',
168
            '2'
169
        ]);
170
        $this->assertContains(1, $typedValue);
171
        $this->assertContains(2, $typedValue);
172
        $this->assertCount(2, $typedValue);
0 ignored issues
show
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

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