TYPO3 CMS  TYPO3_8-7
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 
21 class CsvUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
22 {
26  public function csvToArrayDataProvider()
27  {
28  return [
29  'Valid data' => [
30  'input' => 'Column A, Column B, Column C' . LF . 'Value, Value2, Value 3',
31  'fieldDelimiter' => ',',
32  'fieldEnclosure' => '"',
33  'maximumColumns' => 0,
34  'expectedResult' => [
35  ['Column A', ' Column B', ' Column C'],
36  ['Value', ' Value2', ' Value 3']
37  ]
38  ],
39 
40  'Valid data with enclosed "' => [
41  'input' => '"Column A", "Column B", "Column C"' . LF . '"Value", "Value2", "Value 3"',
42  'fieldDelimiter' => ',',
43  'fieldEnclosure' => '"',
44  'maximumColumns' => 0,
45  'expectedResult' => [
46  ['Column A', 'Column B', 'Column C'],
47  ['Value', 'Value2', 'Value 3']
48  ]
49  ],
50 
51  'Valid data with semicolons and enclosed "' => [
52  'input' => '"Column A"; "Column B"; "Column C"' . LF . '"Value"; "Value2"; "Value 3"',
53  'fieldDelimiter' => ';',
54  'fieldEnclosure' => '"',
55  'maximumColumns' => 0,
56  'expectedResult' => [
57  ['Column A', 'Column B', 'Column C'],
58  ['Value', 'Value2', 'Value 3']
59  ]
60  ],
61 
62  'Valid data with semicolons and enclosed " and two columns' => [
63  'input' => '"Column A"; "Column B"; "Column C"; "Column D"' . LF . '"Value"; "Value2"; "Value 3"',
64  'fieldDelimiter' => ';',
65  'fieldEnclosure' => '"',
66  'maximumColumns' => 2,
67  'expectedResult' => [
68  ['Column A', 'Column B'],
69  ['Value', 'Value2']
70  ]
71  ],
72 
73  'Data with comma but configured with semicolons and enclosed "' => [
74  'input' => '"Column A", "Column B", "Column C"' . LF . '"Value", "Value2", "Value 3"',
75  'fieldDelimiter' => ';',
76  'fieldEnclosure' => '"',
77  'maximumColumns' => 0,
78  'expectedResult' => [
79  ['Column A, "Column B", "Column C"'],
80  ['Value, "Value2", "Value 3"']
81  ]
82  ]
83  ];
84  }
85 
90  public function csvToArraySplitsAsExpected($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns, $expectedResult)
91  {
92  $this->assertEquals($expectedResult, CsvUtility::csvToArray($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns));
93  }
94 }
csvToArraySplitsAsExpected($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns, $expectedResult)