[TASK] Add functional test cases to DataHandler (live)
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / Framework / DataSet.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2014 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use \TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * DataHandler DataSet
31 */
32 class DataSet {
33
34 /**
35 * @var array
36 */
37 protected $data;
38
39 /**
40 * @param string $fileName
41 * @return DataSet
42 */
43 public static function read($fileName) {
44 $data = self::parseData(self::readData($fileName));
45
46 return GeneralUtility::makeInstance(
47 'TYPO3\\CMS\\Core\\Tests\\Functional\\DataHandling\\Framework\\DataSet',
48 $data
49 );
50 }
51
52 /**
53 * @param string $fileName
54 * @return array
55 * @throws \RuntimeException
56 */
57 protected static function readData($fileName) {
58 if (!file_exists($fileName)) {
59 throw new \RuntimeException('File "' . $fileName . '" does not exist');
60 }
61
62 $rawData = array();
63 $fileHandle = fopen($fileName, 'r');
64 while (($values = fgetcsv($fileHandle, 0)) !== FALSE) {
65 $rawData[] = $values;
66 }
67 fclose($fileHandle);
68 return $rawData;
69 }
70
71 /**
72 * Parses CSV data.
73 *
74 * Special values are:
75 * + "\NULL" to treat as NULL value
76 * + "\*" to ignore value during comparison
77 *
78 * @param array $rawData
79 * @return array
80 */
81 protected static function parseData(array $rawData) {
82 $data = array();
83 $tableName = NULL;
84 $fieldCount = NULL;
85 $idIndex = NULL;
86 foreach ($rawData as $values) {
87 if (!empty($values[0])) {
88 $tableName = $values[0];
89 $fieldCount = NULL;
90 $idIndex = NULL;
91 if (!isset($data[$tableName])) {
92 $data[$tableName] = array();
93 }
94 } elseif (implode('', $values) === '') {
95 $tableName = NULL;
96 $fieldCount = NULL;
97 $idIndex = NULL;
98 } elseif ($tableName !== NULL && !empty($values[1])) {
99 array_shift($values);
100 if (!isset($data[$tableName]['fields'])) {
101 $data[$tableName]['fields'] = array();
102 foreach ($values as $value) {
103 if (empty($value)) {
104 continue;
105 }
106 $data[$tableName]['fields'][] = $value;
107 $fieldCount = count($data[$tableName]['fields']);
108 }
109 if (in_array('uid', $values)) {
110 $idIndex = array_search('uid', $values);
111 $data[$tableName]['idIndex'] = $idIndex;
112 }
113 } else {
114 if (!isset($data[$tableName]['elements'])) {
115 $data[$tableName]['elements'] = array();
116 }
117 $values = array_slice($values, 0, $fieldCount);
118 foreach ($values as &$value) {
119 if ($value === '\\NULL') {
120 $value = NULL;
121 }
122 }
123 unset($value);
124 $element = array_combine($data[$tableName]['fields'], $values);
125 if ($idIndex !== NULL) {
126 $data[$tableName]['elements'][$values[$idIndex]] = $element;
127 } else {
128 $data[$tableName]['elements'][] = $element;
129 }
130 }
131 }
132 }
133 return $data;
134 }
135
136 /**
137 * @param array $data
138 */
139 public function __construct(array $data) {
140 $this->data = $data;
141 }
142
143 /**
144 * @return array
145 */
146 public function getTableNames() {
147 return array_keys($this->data);
148 }
149
150 /**
151 * @param string $tableName
152 * @return NULL|array
153 */
154 public function getFields($tableName) {
155 $fields = NULL;
156 if (isset($this->data[$tableName]['fields'])) {
157 $fields = $this->data[$tableName]['fields'];
158 }
159 return $fields;
160 }
161
162 /**
163 * @param string $tableName
164 * @return NULL|integer
165 */
166 public function getIdIndex($tableName) {
167 $idIndex = NULL;
168 if (isset($this->data[$tableName]['idIndex'])) {
169 $idIndex = $this->data[$tableName]['idIndex'];
170 }
171 return $idIndex;
172 }
173
174 /**
175 * @param string $tableName
176 * @return NULL|array
177 */
178 public function getElements($tableName) {
179 $elements = NULL;
180 if (isset($this->data[$tableName]['elements'])) {
181 $elements = $this->data[$tableName]['elements'];
182 }
183 return $elements;
184 }
185
186 /**
187 * @param string $fileName
188 */
189 public function persist($fileName) {
190 $fileHandle = fopen($fileName, 'w');
191
192 foreach ($this->data as $tableName => $tableData) {
193 if (empty($tableData['fields']) || empty($tableData['elements'])) {
194 continue;
195 }
196
197 $fields = $tableData['fields'];
198 array_unshift($fields, '');
199
200 fputcsv($fileHandle, array($tableName));
201 fputcsv($fileHandle, $fields);
202
203 foreach ($tableData['elements'] as $element) {
204 array_unshift($element, '');
205 fputcsv($fileHandle, $element);
206 }
207 }
208
209 fclose($fileHandle);
210 }
211
212 }