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