‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 final class ‪CsvUtilityTest extends UnitTestCase
29 {
30  public static function ‪csvToArrayDataProvider(): array
31  {
32  return [
33  'Valid data' => [
34  'input' => 'Column A, Column B, Column C' . LF . 'Value, Value2, Value 3',
35  'fieldDelimiter' => ',',
36  'fieldEnclosure' => '"',
37  'maximumColumns' => 0,
38  'expectedResult' => [
39  ['Column A', ' Column B', ' Column C'],
40  ['Value', ' Value2', ' Value 3'],
41  ],
42  ],
43 
44  'Valid data with enclosed "' => [
45  'input' => '"Column A", "Column B", "Column C"' . LF . '"Value", "Value2", "Value 3"',
46  'fieldDelimiter' => ',',
47  'fieldEnclosure' => '"',
48  'maximumColumns' => 0,
49  'expectedResult' => [
50  ['Column A', 'Column B', 'Column C'],
51  ['Value', 'Value2', 'Value 3'],
52  ],
53  ],
54 
55  'Valid data with semicolons and enclosed "' => [
56  'input' => '"Column A"; "Column B"; "Column C"' . LF . '"Value"; "Value2"; "Value 3"',
57  'fieldDelimiter' => ';',
58  'fieldEnclosure' => '"',
59  'maximumColumns' => 0,
60  'expectedResult' => [
61  ['Column A', 'Column B', 'Column C'],
62  ['Value', 'Value2', 'Value 3'],
63  ],
64  ],
65 
66  'Valid data with semicolons and enclosed " and two columns' => [
67  'input' => '"Column A"; "Column B"; "Column C"; "Column D"' . LF . '"Value"; "Value2"; "Value 3"',
68  'fieldDelimiter' => ';',
69  'fieldEnclosure' => '"',
70  'maximumColumns' => 2,
71  'expectedResult' => [
72  ['Column A', 'Column B'],
73  ['Value', 'Value2'],
74  ],
75  ],
76 
77  'Data with comma but configured with semicolons and enclosed "' => [
78  'input' => '"Column A", "Column B", "Column C"' . LF . '"Value", "Value2", "Value 3"',
79  'fieldDelimiter' => ';',
80  'fieldEnclosure' => '"',
81  'maximumColumns' => 0,
82  'expectedResult' => [
83  ['Column A, "Column B", "Column C"'],
84  ['Value, "Value2", "Value 3"'],
85  ],
86  ],
87  ];
88  }
89 
90  #[DataProvider('csvToArrayDataProvider')]
91  #[Test]
92  public function ‪csvToArraySplitsAsExpected(string $input, string $fieldDelimiter, string $fieldEnclosure, int $maximumColumns, array $expectedResult): void
93  {
94  self::assertEquals($expectedResult, ‪CsvUtility::csvToArray($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns));
95  }
96 
97  public static 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 
159  #[DataProvider('csvValuesDataProvider')]
160  #[Test]
161  public function ‪csvValuesReturnsExpectedResult(array $row, string $delimiter, string $quote, string $expectedResult, int $flag): void
162  {
163  self::assertEquals($expectedResult, ‪CsvUtility::csvValues($row, $delimiter, $quote, $flag));
164  }
165 }
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_PREFIX_CONTROLS
‪const TYPE_PREFIX_CONTROLS
Definition: CsvUtility.php:41
‪TYPO3\CMS\Core\Utility\CsvUtility\csvToArray
‪static csvToArray(string $input, string $fieldDelimiter=',', string $fieldEnclosure='"', int $maximumColumns = 0)
Definition: CsvUtility.php:53
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest\csvValuesDataProvider
‪static csvValuesDataProvider()
Definition: CsvUtilityTest.php:97
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest\csvValuesReturnsExpectedResult
‪csvValuesReturnsExpectedResult(array $row, string $delimiter, string $quote, string $expectedResult, int $flag)
Definition: CsvUtilityTest.php:161
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_REMOVE_CONTROLS
‪const TYPE_REMOVE_CONTROLS
Definition: CsvUtility.php:35
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest\csvToArrayDataProvider
‪static csvToArrayDataProvider()
Definition: CsvUtilityTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest\csvToArraySplitsAsExpected
‪csvToArraySplitsAsExpected(string $input, string $fieldDelimiter, string $fieldEnclosure, int $maximumColumns, array $expectedResult)
Definition: CsvUtilityTest.php:92
‪TYPO3\CMS\Core\Utility\CsvUtility\TYPE_PASSTHROUGH
‪const TYPE_PASSTHROUGH
Definition: CsvUtility.php:30
‪TYPO3\CMS\Core\Utility\CsvUtility
Definition: CsvUtility.php:26
‪TYPO3\CMS\Core\Tests\Unit\Utility\CsvUtilityTest
Definition: CsvUtilityTest.php:29
‪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