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__ . '/IParamsDriver.php';
 13: require_once __DIR__ . '/../XmlGenerator/XmlGenerator.php';
 14: require_once LIBS . '/PhpPath/PhpPath.min.php';
 15: 
 16: use PhpPath\P;
 17: 
 18: /**
 19:  * Xml prarams for the collection of tests
 20:  *
 21:  * @author Viktor Mašíček <viktor@masicek.net>
 22:  */
 23: class XmlParamsDriver implements IParamsDriver
 24: {
 25: 
 26:     /**
 27:      * Params loaded from xml file
 28:      *
 29:      * @var \SimpleXMLElement
 30:      */
 31:     private $tests;
 32: 
 33:     /**
 34:      * The path of the directory with definition of generated tests
 35:      *
 36:      * @var string
 37:      */
 38:     private $rootDirectory;
 39: 
 40:     /**
 41:      * The path of the temporary directory
 42:      *
 43:      * @var string
 44:      */
 45:     private $tmpDirectoryPath;
 46: 
 47:     /**
 48:      * The cache of list all files path and their id defined in params file
 49:      *
 50:      * @var string
 51:      */
 52:     private $allFilesPaths = NULL;
 53: 
 54: 
 55:     /**
 56:      * Object for generating XML files
 57:      *
 58:      * @var XmlGenerator
 59:      */
 60:     private $xmlGenerator = NULL;
 61: 
 62: 
 63:     /**
 64:      * Choose the params driver by extension
 65:      *
 66:      * @param \XSLTBenchmarking\TestsGenerator\XmlGenerator $xmlGenerator Object for generating XML files
 67:      * @param string $tmpDirectoryPath The path of the temporary directory
 68:      * @param string $paramsFilePath The path of the file with deffinition of generated tests
 69:      *
 70:      * @throws \XSLTBenchmarking\InvalidArgumentException Wrong format of file with params
 71:      */
 72:     public function __construct(
 73:         \XSLTBenchmarking\TestsGenerator\XmlGenerator $xmlGenerator,
 74:         $tmpDirectoryPath,
 75:         $paramsFilePath)
 76:     {
 77:         P::cd($tmpDirectoryPath);
 78:         P::cf($paramsFilePath);
 79: 
 80:         // validate
 81:         $dom = new \DOMDocument();
 82:         $dom->load($paramsFilePath);
 83:         try {
 84:             $dom->schemaValidate(P::m(__DIR__, 'XmlParamsDriver.xsd'));
 85:         } catch (\Exception $e) {
 86:             $error = libxml_get_last_error();
 87:             throw new \XSLTBenchmarking\InvalidArgumentException(
 88:                 'File "' . $paramsFilePath . '" has wrong format: ' . $error->message
 89:             );
 90:         }
 91: 
 92:         $this->rootDirectory = dirname($paramsFilePath);
 93:         $this->xmlGenerator = $xmlGenerator;
 94:         $this->tmpDirectoryPath = $tmpDirectoryPath;
 95:         $this->tests = new \SimpleXMLElement($paramsFilePath, 0, TRUE);
 96:     }
 97: 
 98: 
 99:     /**
100:      * Return the name of tests collection
101:      *
102:      * @return string
103:      */
104:     public function getTemplateName()
105:     {
106:         return (string)$this->tests['name'];
107:     }
108: 
109: 
110:     /**
111:      * Return the path to the template file
112:      *
113:      * @return string
114:      */
115:     public function getTemplatePath()
116:     {
117:         return P::m($this->rootDirectory, (string)$this->tests['template']);
118:     }
119: 
120: 
121:     /**
122:      * Return the type of templating
123:      *
124:      * @return string
125:      */
126:     public function getTemplatingType()
127:     {
128:         return (string)$this->tests['templatingType'];
129:     }
130: 
131: 
132:     /**
133:      * Return the list of tests names
134:      *
135:      * @return array
136:      */
137:     public function getTestsNames()
138:     {
139:         $names = array();
140: 
141:         foreach ($this->tests->test as $test)
142:         {
143:             $names[] = (string)$test['name'];
144:         }
145: 
146:         return $names;
147:     }
148: 
149: 
150:     /**
151:      * Return the list of input files paths
152:      * and paths of their expected output files for selected test
153:      *
154:      * @param string $testName The name of the selected test
155:      *
156:      * @return array
157:      */
158:     public function getTestFilesPaths($testName)
159:     {
160:         $files = array();
161:         $test = $this->tests->xpath('//test[@name="' . $testName . '"]');
162:         $allFiles = $this->getAllFilesPaths();
163: 
164:         foreach ($test[0]->file as $file)
165:         {
166:             $input = $allFiles[(string)$file['input']];
167:             $output = $allFiles[(string)$file['output']];
168:             $files[$input] = $output;
169:         }
170: 
171:         return $files;
172:     }
173: 
174: 
175:     /**
176:      * Return the list of settings for the selected test
177:      *
178:      * @param string $testName The name of the selected test
179:      *
180:      * @return array
181:      */
182:     public function getTestSettings($testName)
183:     {
184:         $settings = array();
185: 
186:         $test = $this->tests->xpath('//test[@name="' . $testName . '"]');
187:         foreach ($test[0]->setting as $setting)
188:         {
189:             $settings[(string)$setting['name']] = (string)$setting;
190:         }
191: 
192:         return $settings;
193:     }
194: 
195: 
196:     /**
197:      * Return the name of file with params of the test
198:      *
199:      * @param string $testName The name of the selected test
200:      *
201:      * @return string|NULL
202:      */
203:     public function getTestParamsFileName($testName)
204:     {
205:         $test = $this->tests->xpath('//test[@name="' . $testName . '"]');
206:         $name = NULL;
207:         if (isset($test[0]['paramsFile']))
208:         {
209:             $name = (string)$test[0]['paramsFile'];
210:         }
211: 
212:         return $name;
213:     }
214: 
215: 
216:     /**
217:      * Return list of possible paths of files for testing with their ids
218:      *
219:      * @return array ([id] => [path], ...)
220:      */
221:     private function getAllFilesPaths()
222:     {
223:         if ($this->allFilesPaths)
224:         {
225:             return $this->allFilesPaths;
226:         }
227: 
228:         $this->allFilesPaths = $this->createAllFilesPaths();
229:         return $this->allFilesPaths;
230:     }
231: 
232: 
233:     /**
234:      * Create list of possible paths of files for testing with their ids
235:      *
236:      * @return array ([id] => [path], ...)
237:      */
238:     private function createAllFilesPaths()
239:     {
240:         $files = array();
241: 
242:         // existed xml files
243:         foreach ($this->tests->files->file as $file)
244:         {
245:             $files[(string)$file['id']] = P::m($this->rootDirectory, (string)$file);
246:         }
247: 
248:         // make tmp subdirectory for generating files
249:         if (count($this->tests->files->generated) > 0)
250:         {
251:             $tmpSubdirectory = strtolower(trim($this->getTemplateName()));
252:             $tmpSubdirectory = preg_replace('/[^a-z0-9-_]/', '-', $tmpSubdirectory);
253:             $tmpSubdirectory = preg_replace('/-+/', '-', $tmpSubdirectory);
254:             $tmpSubdirectoryPath = P::m($this->tmpDirectoryPath, $tmpSubdirectory);
255:             if (!is_dir($tmpSubdirectoryPath))
256:             {
257:                 mkdir($tmpSubdirectoryPath);
258:             }
259:         }
260:         // generated xml files
261:         foreach ($this->tests->files->generated as $generated)
262:         {
263:             // read settings
264:             $settings = array();
265:             foreach ($generated->setting as $setting)
266:             {
267:                 $settings[(string)$setting['name']] = (string)$setting;
268:             }
269: 
270:             // generate file into TMP
271:             $type = (string)$generated['generator'];
272:             $outputPath = P::m($this->tmpDirectoryPath, $tmpSubdirectory, (string)$generated['output']);
273:             $this->xmlGenerator->setDriver($type);
274:             $this->xmlGenerator->generate($outputPath, $this->rootDirectory, $settings);
275: 
276:             // add generated file into list of files
277:             $files[(string)$generated['id']] = $outputPath;
278:         }
279: 
280:         return $files;
281:     }
282: 
283: 
284: }
285: 
XSTL Benchmarking API documentation generated by ApiGen.
Generated using the TokenReflection library.