‪TYPO3CMS  11.5
CsvUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
26 class ‪CsvUtilityTest extends UnitTestCase
27 {
31  public function ‪csvToArrayDataProvider(): array
32  {
33  return [
34  'Valid data' => [
35  'input' => 'Column A, Column B, Column C' . LF . 'Value, Value2, Value 3',
36  'fieldDelimiter' => ',',
37  'fieldEnclosure' => '"',
38  'maximumColumns' => 0,
39  'expectedResult' => [
40  ['Column A', ' Column B', ' Column C'],
41  ['Value', ' Value2', ' Value 3'],
42  ],
43  ],
44 
45  'Valid data with enclosed "' => [
46  'input' => '"Column A", "Column B", "Column C"' . LF . '"Value", "Value2", "Value 3"',
47  'fieldDelimiter' => ',',
48  'fieldEnclosure' => '"',
49  'maximumColumns' => 0,
50  'expectedResult' => [
51  ['Column A', 'Column B', 'Column C'],
52  ['Value', 'Value2', 'Value 3'],
53  ],
54  ],
55 
56  'Valid data with semicolons and enclosed "' => [
57  'input' => '"Column A"; "Column B"; "Column C"' . LF . '"Value"; "Value2"; "Value 3"',
58  'fieldDelimiter' => ';',
59  'fieldEnclosure' => '"',
60  'maximumColumns' => 0,
61  'expectedResult' => [
62  ['Column A', 'Column B', 'Column C'],
63  ['Value', 'Value2', 'Value 3'],
64  ],
65  ],
66 
67  'Valid data with semicolons and enclosed " and two columns' => [
68  'input' => '"Column A"; "Column B"; "Column C"; "Column D"' . LF . '"Value"; "Value2"; "Value 3"',
69  'fieldDelimiter' => ';',
70  'fieldEnclosure' => '"',
71  'maximumColumns' => 2,
72  'expectedResult' => [
73  ['Column A', 'Column B'],
74  ['Value', 'Value2'],
75  ],
76  ],
77 
78  'Data with comma but configured with semicolons and enclosed "' => [
79  'input' => '"Column A", "Column B", "Column C"' . LF . '"Value", "Value2", "Value 3"',
80  'fieldDelimiter' => ';',
81  'fieldEnclosure' => '"',
82  'maximumColumns' => 0,
83  'expectedResult' => [
84  ['Column A, "Column B", "Column C"'],
85  ['Value, "Value2", "Value 3"'],
86  ],
87  ],
88  ];
89  }
90 
95  public function ‪csvToArraySplitsAsExpected($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns, $expectedResult): void
96  {
97  self::assertEquals($expectedResult, ‪CsvUtility::csvToArray($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns));
98  }
99 
100  public function ‪csvValuesDataProvider(): array
101  {
102  return [
103  'row with semicolon as delimiter (TYPE_PASSTHROUGH)' => [
104  ['val1', 'val2', 'val3'],
105  ';',
106  '"',
107  '"val1";"val2";"val3"',
109  ],
110  'row where value contains line feeds (TYPE_PASSTHROUGH)' => [
111  ['val1 line1' . "\n" . 'val1 line2', 'val2 line1' . "\r\n" . 'val2 line2', 'val3'],
112  ',',
113  '"',
114  '"val1 line1' . "\n" . 'val1 line2","val2 line1' . "\r\n" . 'val2 line2","val3"',
116  ],
117  'row with all possible control chars (TYPE_PASSTHROUGH)' => [
118  ['=val1', '+val2', '*val3', '%val4', '@val5', '-val6'],
119  ',',
120  '"',
121  '"=val1","+val2","*val3","%val4","@val5","-val6"',
123  ],
124  'row with spacing and delimiting chars (TYPE_PASSTHROUGH)' => [
125  [' val1', "\tval2", "\nval3", "\r\nval4", ',val5,', '"val6"'],
126  ',',
127  '"',
128  '" val1","' . "\tval2" . '","' . "\nval3" . '","' . "\r\nval4" . '",",val5,","""val6"""' ,
130  ],
131  'row with all possible control chars (TYPE_PREFIX_CONTROLS)' => [
132  ['=val1', '+val2', '*val3', '%val4', '@val5', '-val6'],
133  ',',
134  '"',
135  '"\'=val1","\'+val2","\'*val3","\'%val4","\'@val5","\'-val6"',
137  ],
138  'row with spacing and delimiting chars (TYPE_PREFIX_CONTROLS)' => [
139  [' val1', "\tval2", "\nval3", "\r\nval4", ',val5,', '"val6"'],
140  ',',
141  '"',
142  '" val1","' . "'\tval2" . '","' . "'\nval3" . '","' . "'\r\nval4" . '",",val5,","""val6"""' ,
144  ],
145  'row with all possible control chars (TYPE_REMOVE_CONTROLS)' => [
146  ['=val1', '+val2', '*val3', '%val4', '@val5', '-val6'],
147  ',',
148  '"',
149  '"val1","val2","val3","val4","val5","val6"',
151  ],
152  'row with spacing and delimiting chars (TYPE_REMOVE_CONTROLS)' => [
153  [' val1', "\tval2", "\nval3", "\r\nval4", ',val5,', '"val6"'],
154  ',',
155  '"',
156  '" val1","val2","val3","val4",",val5,","""val6"""' ,
158  ],
159  ];
160  }
161 
166  public function ‪csvValuesReturnsExpectedResult($row, $delimiter, $quote, $expectedResult, $flag): void
167  {
168  self::assertEquals($expectedResult, ‪CsvUtility::csvValues($row, $delimiter, $quote, $flag));
169  }
170 }
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_PREFIX_CONTROLS
‪const TYPE_PREFIX_CONTROLS
Definition: CsvUtility.php:39
‪TYPO3\CMS\Core\Utility\CsvUtility\csvToArray
‪static array csvToArray($input, $fieldDelimiter=',', $fieldEnclosure='"', $maximumColumns = 0)
Definition: CsvUtility.php:52
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest\csvValuesDataProvider
‪csvValuesDataProvider()
Definition: CsvUtilityTest.php:100
‪TYPO3\CMS\Core\Utility\CsvUtility\csvValues
‪static string csvValues(array $row, string $delim=',', string $quote='"', int $type = self::TYPE_REMOVE_CONTROLS)
Definition: CsvUtility.php:100
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_REMOVE_CONTROLS
‪const TYPE_REMOVE_CONTROLS
Definition: CsvUtility.php:33
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest\csvToArrayDataProvider
‪array csvToArrayDataProvider()
Definition: CsvUtilityTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest\csvToArraySplitsAsExpected
‪csvToArraySplitsAsExpected($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns, $expectedResult)
Definition: CsvUtilityTest.php:95
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest\csvValuesReturnsExpectedResult
‪csvValuesReturnsExpectedResult($row, $delimiter, $quote, $expectedResult, $flag)
Definition: CsvUtilityTest.php:166
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_PASSTHROUGH
‪const TYPE_PASSTHROUGH
Definition: CsvUtility.php:28
‪TYPO3\CMS\Core\Utility\CsvUtility
Definition: CsvUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest
Definition: CsvUtilityTest.php:27