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\TestsRunner;
11 :
12 : require_once __DIR__ . '/IParamsDriver.php';
13 : require_once __DIR__ . '/../../Exceptions.php';
14 : require_once LIBS . '/PhpPath/PhpPath.min.php';
15 :
16 : use PhpPath\P;
17 :
18 :
19 : /**
20 : * Class for read params of test form XML file.
21 : *
22 : * @author Viktor Mašíček <viktor@masicek.net>
23 : */
24 : class XmlParamsDriver implements IParamsDriver
25 : {
26 :
27 :
28 : /**
29 : * Params loaded from xml file
30 : *
31 : * @var \SimpleXMLElement
32 : */
33 : private $test;
34 :
35 : /**
36 : * The path of the directory with definition of test
37 : *
38 : * @var string
39 : */
40 : private $rootDirectory;
41 :
42 : /**
43 : * The path of the generated file defined test
44 : *
45 : * @var string
46 : */
47 : private $path;
48 :
49 :
50 : /**
51 : * Set the params file.
52 : *
53 : * @param string $paramsFilePath The path of the file with deffinition of generated tests
54 : * @param bool $read Flag, for reading the params
55 : *
56 : * @throws \XSLTBenchmarking\InvalidArgumentException Wrong format of file with params
57 : */
58 : public function __construct($paramsFilePath, $read = TRUE)
59 : {
60 : // reading of params
61 : if ($read)
62 15 : {
63 : // validate
64 14 : $dom = new \DOMDocument();
65 14 : $dom->load($paramsFilePath);
66 : try {
67 14 : $dom->schemaValidate(P::m(__DIR__, 'XmlParamsDriver.xsd'));
68 14 : } catch (\Exception $e) {
69 12 : $error = libxml_get_last_error();
70 12 : throw new \XSLTBenchmarking\InvalidArgumentException(
71 12 : 'File "' . $paramsFilePath . '" has wrong format: ' . $error->message
72 12 : );
73 : }
74 :
75 2 : $this->rootDirectory = dirname($paramsFilePath);
76 2 : $this->test = new \SimpleXMLElement($paramsFilePath, 0, TRUE);
77 2 : }
78 : else
79 : {
80 1 : $this->path = $paramsFilePath;
81 : }
82 3 : }
83 :
84 :
85 : /**
86 : * Function for generating new paramas file
87 : *
88 : * @param string $name Name of the test
89 : * @param string $templatePath Path of tested XSLT template
90 : * @param array $couplesPaths ([input] => [output], ...)
91 : *
92 : * @return void
93 : */
94 : public function generate($name, $templatePath, array $couplesPaths)
95 : {
96 : // get base name of couples
97 1 : $couplesKeys = array_map('basename', array_keys($couplesPaths));
98 1 : $couplesValues = array_map('basename', $couplesPaths);
99 1 : $couples = array_combine($couplesKeys, $couplesValues);
100 :
101 : // make xml file
102 1 : $testDef = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><test></test>');
103 1 : $testDef->addAttribute('name', $name);
104 1 : $testDef->addAttribute('template', $templatePath);
105 :
106 : // couples
107 1 : foreach ($couples as $input => $output)
108 : {
109 1 : $couple = $testDef->addChild('couple');
110 1 : $couple->addAttribute('input', $input);
111 1 : $couple->addAttribute('output', $output);
112 1 : }
113 :
114 : // save
115 1 : $dom = dom_import_simplexml($testDef)->ownerDocument;
116 1 : $dom->formatOutput = TRUE;
117 1 : $dom->save($this->path);
118 1 : }
119 :
120 :
121 : /**
122 : * Return the name of test
123 : *
124 : * @return string
125 : */
126 : public function getName()
127 : {
128 1 : return (string)$this->test['name'];
129 : }
130 :
131 :
132 : /**
133 : * Return the path to the XSLT template
134 : *
135 : * @return string
136 : */
137 : public function getTemplatePath()
138 : {
139 1 : return P::m($this->rootDirectory, (string)$this->test['template']);
140 : }
141 :
142 :
143 : /**
144 : * Return the path to the XML files for testing
145 : * - input
146 : * - expected output
147 : *
148 : * @return array ([input] => [expected output])
149 : */
150 : public function getCouplesPaths()
151 : {
152 1 : $couples = array();
153 1 : foreach ($this->test->couple as $couple)
154 : {
155 1 : $input = P::m($this->rootDirectory, (string)$couple['input']);
156 1 : $output = P::m($this->rootDirectory, (string)$couple['output']);
157 :
158 1 : $couples[$input] = $output;
159 1 : }
160 :
161 1 : return $couples;
162 : }
163 :
164 :
165 : }
|