1 : <?php
2 :
3 : /**
4 : * XSLT Benchmarking
5 : * @link https://github.com/masicek/XSLT-Benchmarking
6 : * @author Viktor Mašíček <viktor@masicek.net>
7 : * @license "New" BSD License
8 : */
9 :
10 : namespace XSLTBenchmarking\TestsGenerator;
11 :
12 : require_once LIBS . '/PhpPath/PhpPath.min.php';
13 : require_once ROOT . '/Exceptions.php';
14 :
15 : use PhpPath\P;
16 :
17 : /**
18 : * Container for information about one test.
19 : *
20 : * @author Viktor Mašíček <viktor@masicek.net>
21 : */
22 : class Test
23 : {
24 :
25 : /**
26 : * The human-redable name of the test
27 : *
28 : * @var string
29 : */
30 : private $name;
31 :
32 : /**
33 : * The path of the test template for generating the test
34 : *
35 : * @var string
36 : */
37 : private $templatePath;
38 :
39 : /**
40 : * Type of templating for generating test from template
41 : *
42 : * @var string
43 : */
44 : private $templatingType;
45 :
46 : /**
47 : * The path of the test directory for generating
48 : *
49 : * @var string
50 : */
51 : private $path;
52 :
53 : /**
54 : * The array of settings for the test teamplte
55 : *
56 : * @var array
57 : */
58 : private $settings = array();
59 :
60 : /**
61 : * The list of paths of input files for testing
62 : * with the paths of their expected output files
63 : *
64 : * @var array ([input] => [expected output], ...)
65 : */
66 : private $filesPaths = array();
67 :
68 :
69 : /**
70 : * Path to file with pramas of test
71 : *
72 : * @var string
73 : */
74 : private $paramsFilePath;
75 :
76 :
77 : /**
78 : * Set the human-redable name of the test
79 : *
80 : * @param string $name The human-redable name of the test
81 : */
82 : public function __construct($name)
83 : {
84 1 : $this->name = $name;
85 1 : }
86 :
87 :
88 : /**
89 : * Return the human-redable name of the test
90 : *
91 : * @return string
92 : */
93 : public function getName()
94 : {
95 1 : return $this->name;
96 : }
97 :
98 :
99 : /**
100 : * Set the path of the test template
101 : *
102 : * @param string $templatePath The path of the test template
103 : *
104 : * @return void
105 : */
106 : public function setTemplatePath($templatePath)
107 : {
108 9 : $templatePath = P::mcf($templatePath);
109 :
110 8 : $basename = basename($templatePath);
111 8 : $parts = explode('.', $basename);
112 8 : $partsCount = count($parts);
113 8 : if ($partsCount < 3 ||
114 1 : ($parts[$partsCount - 2] != 'tpl') ||
115 1 : ($parts[$partsCount - 1] != 'xsl' && $parts[$partsCount - 1] != 'xslt'))
116 8 : {
117 7 : throw new \XSLTBenchmarking\InvalidArgumentException('Template path does not have extension ".tpl.xsl" or ".tpl.xslt". It has value "' . $templatePath . '"');
118 : }
119 :
120 1 : $this->templatePath = $templatePath;
121 1 : }
122 :
123 :
124 : /**
125 : * Return the path of the test template
126 : *
127 : * @return string
128 : */
129 : public function getTemplatePath()
130 : {
131 1 : return $this->templatePath;
132 : }
133 :
134 :
135 : /**
136 : * Set the type of templating
137 : *
138 : * @param string $templatingType The type of templating
139 : *
140 : * @return void
141 : */
142 : public function setTemplatingType($templatingType)
143 : {
144 1 : $this->templatingType = $templatingType;
145 1 : }
146 :
147 :
148 : /**
149 : * Return the type of templating
150 : *
151 : * @return string
152 : */
153 : public function getTemplatingType()
154 : {
155 1 : return $this->templatingType;
156 : }
157 :
158 :
159 : /**
160 : * Set the path to the test
161 : *
162 : * @param string $rootPath The root directory of all tests
163 : *
164 : * @return void
165 : */
166 : public function setPath($rootPath)
167 : {
168 1 : $name = $this->name;
169 1 : $name = strtolower(trim($name));
170 1 : $name = preg_replace('/[^a-z0-9-_]/', '-', $name);
171 1 : $name = preg_replace('/-+/', '-', $name);
172 :
173 1 : $this->path = P::m($rootPath, $name);
174 1 : }
175 :
176 :
177 : /**
178 : * Return the path of directory for generating the test
179 : *
180 : * @return string
181 : */
182 : public function getPath()
183 : {
184 1 : return $this->path;
185 : }
186 :
187 :
188 : /**
189 : * Return the name of the test xslt file
190 : *
191 : * @return string
192 : */
193 : public function getXsltName()
194 : {
195 : // remove "tpl" part
196 1 : $basename = basename($this->getTemplatePath());
197 1 : $parts = explode('.', $basename);
198 1 : unset($parts[count($parts) - 2]);
199 1 : $xsltName = implode('.', $parts);
200 :
201 1 : return $xsltName;
202 : }
203 :
204 :
205 : /**
206 : * Return the path of the test xslt file
207 : *
208 : * @return string
209 : */
210 : public function getXsltPath()
211 : {
212 1 : return P::m($this->getPath(), $this->getXsltName());
213 : }
214 :
215 :
216 : /**
217 : * Add settins of the test for generating
218 : *
219 : * @param array $settings ([name] => [value], ...)
220 : *
221 : * @return void
222 : */
223 : public function addSettings(array $settings)
224 : {
225 1 : $this->settings = array_merge($this->settings, $settings);
226 1 : }
227 :
228 :
229 : /**
230 : * Return all settings of test for generating
231 : *
232 : * @return array
233 : */
234 : public function getSettings()
235 : {
236 1 : return $this->settings;
237 : }
238 :
239 :
240 : /**
241 : * Add paths of input files for testing
242 : * with the paths of their expected output files
243 : *
244 : * @param array $filesPaths ([input] => [expected output], ...)
245 : *
246 : * @return void
247 : */
248 : public function addFilesPaths(array $filesPaths)
249 : {
250 2 : foreach ($filesPaths as $input => $output)
251 : {
252 2 : $inputExtension = pathinfo($input, PATHINFO_EXTENSION);
253 2 : if ($inputExtension != 'xml')
254 2 : {
255 1 : throw new \XSLTBenchmarking\InvalidArgumentException('XSLT template path does not have extension ".xml". It has value "' . $input . '"');
256 : }
257 1 : $this->filesPaths[P::mcf($input)] = P::mcf($output);
258 1 : }
259 1 : }
260 :
261 :
262 : /**
263 : * Return all paths of xml files for testing
264 : * with their expected output files paths
265 : *
266 : * @return array ([input] => [expectedOutput], ...)
267 : */
268 : public function getFilesPaths()
269 : {
270 1 : return $this->filesPaths;
271 : }
272 :
273 :
274 : /**
275 : * Set the path of the file with params of test
276 : *
277 : * @param string $fileName Name of file with params
278 : *
279 : * @return void
280 : */
281 : public function setParamsFilePath($fileName)
282 : {
283 1 : $this->paramsFilePath = P::m($this->getPath(), $fileName);
284 1 : }
285 :
286 :
287 : /**
288 : * Return the path of the file with params of test
289 : *
290 : * @return string
291 : */
292 : public function getParamsFilePath()
293 : {
294 1 : return $this->paramsFilePath;
295 : }
296 :
297 :
298 : }
|