Overview

Namespaces

  • PHP
  • XSLTBenchmarking
    • Reports
    • RunnerConsole
    • TestsGenerator
    • TestsRunner

Classes

  • EasyXmlGeneratorDriver
  • Generator
  • Params
  • SimpleTemplatingDriver
  • SmartyTemplatingDriver
  • SmartyXmlGeneratorDriver
  • Templating
  • Test
  • ToxgeneTemplatingDriver
  • ToxgeneXmlGeneratorDriver
  • XmlGenerator
  • XmlParamsDriver

Interfaces

  • IParamsDriver
  • ITemplatingDriver
  • IXmlGeneratorDriver
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  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:         P::cd($templatesDirectory);
 96:         P::cd($testsDirectory);
 97: 
 98:         $this->factory = $factory;
 99:         $this->params = $params;
100:         $this->templating = $templating;
101:         $this->paramsTest = $paramsTest;
102:         $this->templatesDirectory = $templatesDirectory;
103:         $this->testsDirectory = $testsDirectory;
104:     }
105: 
106: 
107:     /**
108:      * Return registered test templates.
109:      *
110:      * @return array
111:      */
112:     public function getTests()
113:     {
114:         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:         $testParamsPath = P::mcf($this->templatesDirectory, $templateDirectory, $testParamsFile);
131:         $this->params->setFile($testParamsPath);
132:         $templateName = $this->params->getTemplateName();
133:         $templatePath = $this->params->getTemplatePath();
134:         $templatingType = $this->params->getTemplatingType();
135:         foreach ($this->params->getTestsNames() as $testName)
136:         {
137:             $fullName = $templateName . ' - ' . $testName;
138:             if (isset($this->templates[$fullName]))
139:             {
140:                 throw new \XSLTBenchmarking\CollisionException('Duplicate name of test "' . $fullName . '"');
141:             }
142: 
143:             $test = $this->factory->getTestsGeneratorTest($fullName);
144:             $test->setTemplatePath($templatePath);
145:             $test->setTemplatingType($templatingType);
146:             $test->setPath($this->testsDirectory);
147:             $test->addFilesPaths($this->params->getTestFilesPaths($testName));
148:             $test->addSettings($this->params->getTestSettings($testName));
149:             $test->setParamsFilePath($this->params->getTestParamsFileName($testName));
150:             $this->templates[$fullName] = $test;
151:         }
152:     }
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:         $tests = $this->getTests();
165:         $testsCount = count($tests);
166:         $testsGenerated = 0;
167:         foreach ($tests as $test)
168:         {
169:             $regenerate = $this->generateTest($test);
170:             $testsGenerated++;
171:             if ($verbose)
172:             {
173:                 $preffix = $testsGenerated . '/' . $testsCount . " - ";
174:                 if ($regenerate)
175:                 {
176:                     Printer::info($preffix . 'Tests from template "' . $test->getName() . '" were regenerated.');
177:                 }
178:                 else
179:                 {
180:                     Printer::info($preffix . 'Tests from template "' . $test->getName() . '" were generated.');
181:                 }
182:             }
183:         }
184: 
185:         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:         $destinationDirectory = $test->getPath();
199:         $regenrated = TRUE;
200:         if (!is_dir($destinationDirectory))
201:         {
202:             mkdir($destinationDirectory);
203:             $regenrated = FALSE;
204:         }
205: 
206:         // copy files to tests directory
207:         foreach ($test->getFilesPaths() as $inputPath => $outputPath)
208:         {
209:             $destinationInputPath = P::m($destinationDirectory, basename($inputPath));
210:             $destinationOutputPath = P::m($destinationDirectory, basename($outputPath));
211:             copy($inputPath, $destinationInputPath);
212:             copy($outputPath, $destinationOutputPath);
213:         }
214: 
215:         // generate template
216:         $this->templating->setDriver($test->getTemplatingType());
217:         $this->templating->generate($test->getTemplatePath(), $test->getXsltPath(), $test->getSettings());
218: 
219:         // generate file with params of generated test
220:         $this->paramsTest->setFile($test->getParamsFilePath(), FALSE);
221:         $this->paramsTest->generate(
222:             $test->getName(),
223:             $test->getXsltName(),
224:             $test->getFilesPaths()
225:         );
226: 
227:         return $regenrated;
228:     }
229: 
230: 
231: }
232: 
XSTL Benchmarking API documentation generated by ApiGen.
Generated using the TokenReflection library.