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