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 __DIR__ . '/../Exceptions.php';
13 : require_once LIBS . '/PhpPath/PhpPath.min.php';
14 : require_once ROOT . '/Printer.php';
15 :
16 : use PhpPath\P;
17 : use XSLTBenchmarking\Printer;
18 :
19 : /**
20 : * Tests generator for XSTL Benchamrking
21 : *
22 : * @author Viktor Mašíček <viktor@masicek.net>
23 : */
24 : class Generator
25 : {
26 :
27 : /**
28 : * Factory class for making new objects
29 : *
30 : * @var \XSLTBenchmarking\Factory
31 : */
32 : private $factory;
33 :
34 : /**
35 : * Object for reading params of tests templates
36 : *
37 : * @var \XSLTBenchmarking\TestsGenerator\Params
38 : */
39 : private $params;
40 :
41 : /**
42 : * Object for generating XSLT from template of XSLT
43 : *
44 : * @var \XSLTBenchmarking\TestsGenerator\Templating
45 : */
46 : private $templating;
47 :
48 : /**
49 : * Object for generating params of test
50 : *
51 : * @var \XSLTBenchmarking\TestsRunner\Params
52 : */
53 : private $paramsTest;
54 :
55 : /**
56 : * Root directory of templates for all tests
57 : *
58 : * @var string
59 : */
60 : private $templatesDirectory;
61 :
62 : /**
63 : * Root directory of generated tests
64 : *
65 : * @var string
66 : */
67 : private $testsDirectory;
68 :
69 : /**
70 : * List of test templates for generating tests
71 : *
72 : * @var array of Test
73 : */
74 : private $templates = array();
75 :
76 :
77 : /**
78 : * Object configuration
79 : *
80 : * @param \XSLTBenchmarking\Factory $factory Factory class for making new objects
81 : * @param \XSLTBenchmarking\TestsGenerator\Params $params Object for reading params of tests templates
82 : * @param \XSLTBenchmarking\TestsGenerator\Templating $templating Object for generating XSLT from template of XSLT
83 : * @param \XSLTBenchmarking\TestsRunner\Params $paramsTest Object for generating params of test
84 : * @param string $templatesDirectory The root directory of all tests templates
85 : * @param string $testsDirectory The root directory of all generated tests
86 : */
87 : public function __construct(
88 : \XSLTBenchmarking\Factory $factory,
89 : \XSLTBenchmarking\TestsGenerator\Params $params,
90 : \XSLTBenchmarking\TestsGenerator\Templating $templating,
91 : \XSLTBenchmarking\TestsRunner\Params $paramsTest,
92 : $templatesDirectory,
93 : $testsDirectory)
94 : {
95 3 : P::cd($templatesDirectory);
96 2 : P::cd($testsDirectory);
97 :
98 1 : $this->factory = $factory;
99 1 : $this->params = $params;
100 1 : $this->templating = $templating;
101 1 : $this->paramsTest = $paramsTest;
102 1 : $this->templatesDirectory = $templatesDirectory;
103 1 : $this->testsDirectory = $testsDirectory;
104 1 : }
105 :
106 :
107 : /**
108 : * Return registered test templates.
109 : *
110 : * @return array
111 : */
112 : public function getTests()
113 : {
114 1 : return $this->templates;
115 : }
116 :
117 :
118 : /**
119 : * Register defined tests for generating
120 : *
121 : * @param string $templateDirectory The subdirectory of the root directory
122 : * of templates containing template of generated tests
123 : * @param string $paramsFiles File defined tests
124 : *
125 : * @throws \XSLTBenchmarking\CollisionException Duplicate name of test
126 : * @return void
127 : */
128 : public function addTests($templateDirectory, $testParamsFile = '__params.xml')
129 : {
130 4 : $testParamsPath = P::mcf($this->templatesDirectory, $templateDirectory, $testParamsFile);
131 2 : $this->params->setFile($testParamsPath);
132 2 : $templateName = $this->params->getTemplateName();
133 2 : $templatePath = $this->params->getTemplatePath();
134 2 : $templatingType = $this->params->getTemplatingType();
135 2 : foreach ($this->params->getTestsNames() as $testName)
136 : {
137 2 : $fullName = $templateName . ' - ' . $testName;
138 2 : if (isset($this->templates[$fullName]))
139 2 : {
140 1 : throw new \XSLTBenchmarking\CollisionException('Duplicate name of test "' . $fullName . '"');
141 : }
142 :
143 2 : $test = $this->factory->getTestsGeneratorTest($fullName);
144 2 : $test->setTemplatePath($templatePath);
145 2 : $test->setTemplatingType($templatingType);
146 2 : $test->setPath($this->testsDirectory);
147 2 : $test->addFilesPaths($this->params->getTestFilesPaths($testName));
148 2 : $test->addSettings($this->params->getTestSettings($testName));
149 2 : $test->setParamsFilePath($this->params->getTestParamsFileName($testName));
150 2 : $this->templates[$fullName] = $test;
151 2 : }
152 1 : }
153 :
154 :
155 : /**
156 : * Generate all registered tests
157 : *
158 : * @param bool $verbose Print information about each run test
159 : *
160 : * @return int Number of generated tests
161 : */
162 : public function generateAll($verbose = FALSE)
163 : {
164 2 : $tests = $this->getTests();
165 2 : $testsCount = count($tests);
166 2 : $testsGenerated = 0;
167 2 : foreach ($tests as $test)
168 : {
169 2 : $regenerate = $this->generateTest($test);
170 2 : $testsGenerated++;
171 : if ($verbose)
172 2 : {
173 1 : $preffix = $testsGenerated . '/' . $testsCount . " - ";
174 : if ($regenerate)
175 1 : {
176 0 : Printer::info($preffix . 'Tests from template "' . $test->getName() . '" were regenerated.');
177 0 : }
178 : else
179 : {
180 1 : Printer::info($preffix . 'Tests from template "' . $test->getName() . '" were generated.');
181 : }
182 1 : }
183 2 : }
184 :
185 2 : return $testsCount;
186 : }
187 :
188 :
189 : /**
190 : * Generate input test and return true, if test existed before generating
191 : *
192 : * @param Test $test Generated test
193 : *
194 : * @return bool
195 : */
196 : private function generateTest(Test $test)
197 : {
198 2 : $destinationDirectory = $test->getPath();
199 2 : $regenrated = TRUE;
200 2 : if (!is_dir($destinationDirectory))
201 2 : {
202 2 : mkdir($destinationDirectory);
203 2 : $regenrated = FALSE;
204 2 : }
205 :
206 : // copy files to tests directory
207 2 : foreach ($test->getFilesPaths() as $inputPath => $outputPath)
208 : {
209 2 : $destinationInputPath = P::m($destinationDirectory, basename($inputPath));
210 2 : $destinationOutputPath = P::m($destinationDirectory, basename($outputPath));
211 2 : copy($inputPath, $destinationInputPath);
212 2 : copy($outputPath, $destinationOutputPath);
213 2 : }
214 :
215 : // generate template
216 2 : $this->templating->setDriver($test->getTemplatingType());
217 2 : $this->templating->generate($test->getTemplatePath(), $test->getXsltPath(), $test->getSettings());
218 :
219 : // generate file with params of generated test
220 2 : $this->paramsTest->setFile($test->getParamsFilePath(), FALSE);
221 2 : $this->paramsTest->generate(
222 2 : $test->getName(),
223 2 : $test->getXsltName(),
224 2 : $test->getFilesPaths()
225 2 : );
226 :
227 2 : return $regenrated;
228 : }
229 :
230 :
231 : }
|