ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
TestRailXMLWriter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(dirname(__FILE__) . '/templates/testrail.case_ids.php');
22
25
27{
29 protected const SHOW = 'show';
30 protected const VALIDATE = 'validate';
31 protected const BASE = 'UIBASE';
32 protected const OPEN = 'open';
33 protected const PREPARE = 'prepare';
34
35 protected SimpleXMLElement $xml;
36
37 protected int $no_found_ids = 0;
38 protected int $no_expexted_ids = 0;
39 protected int $no_new_ids = 0;
40 protected int $no_components = 2; //initUISection calls getCaseId twice, updating new_ids/found_ids; this is to make total numbers match up
41
42 protected array $expected_by_caseids = [];
43
44 public function __construct(
45 protected \ilTemplate $case_tpl,
46 protected ExamplesYamlParser $parser,
47 protected bool $only_new_cases
48 ) {
49 $this->xml = new SimpleXMLElement('<?xml version="1.0"?><sections></sections>');
50 foreach (self::$CASEIDS as $k => $v) {
51 $this->no_expexted_ids += count($v);
52 $this->expected_by_caseids = array_merge($this->expected_by_caseids, array_values($v));
53 }
54 }
55
56 public function getXML(): SimpleXMLElement
57 {
58 print "\n found " . $this->no_components . ' components/cases';
59 print "\n update for " . $this->no_found_ids . ' of ' . $this->no_expexted_ids . ' known IDs';
60 print "\n new cases: " . $this->no_new_ids;
61 print "\n known ids unaccounted for: " . implode(',', $this->expected_by_caseids);
62 print "\n";
63 return $this->xml;
64 }
65
69 public function withData(array $data): self
70 {
71 $xml_sections = $this
72 ->initUISection()
73 ->addChild('sections');
74
75 foreach ($data as $section => $components) {
76 $xml_cases = $this
77 ->addSection($xml_sections, $section, '')
78 ->addChild('cases');
79
80 foreach ($components as $component) {
81 list($component_name, $entry) = $component;
82 $this->no_components += 2;
83 $this->addComponentCases($xml_cases, $section, $component_name, $entry);
84 }
85 }
86
87 return $this;
88 }
89
90 public function addSection(
91 SimpleXMLElement $xml_parent_node,
92 string $name,
93 string $description = '',
94 ): SimpleXMLElement {
95 $xml_section = $xml_parent_node->addChild('section');
96 $xml_section->addChild('name', $name);
97 $xml_section->addChild('description', $description);
98 return $xml_section;
99 }
100
101 protected function addCase(
102 SimpleXMLElement $xml_parent_node,
103 string $id,
104 string $title,
105 string $preconditions,
106 string $steps,
107 string $expected,
108 ): SimpleXMLElement {
109 $xml_case = $xml_parent_node->addChild('case');
110 $xml_case->addChild('id', $id);
111 $xml_case->addChild('title', $title);
112 $xml_case->addChild('template', 'Test Case');
113 $xml_case->addChild('type', 'Other');
114 $xml_case->addChild('priority', '4 - Must Test');
115 $xml_cust = $xml_case->addChild('custom');
116 $xml_cust->addChild('preconds', "\n" . trim($preconditions) . "\n");
117 $xml_cust->addChild('steps', "\n" . trim($steps) . "\n");
118 $xml_cust->addChild('expected', "\n" . trim($expected) . "\n");
119 return $xml_parent_node;
120 }
121
122 protected function initUISection(): SimpleXMLElement
123 {
124 $xml_section = $this->addSection(
125 $this->xml,
126 $this->getBlockContents('suite_title'),
127 $this->getBlockContents('suite_description')
128 );
129 $xml_cases = $xml_section->addChild('cases');
130
131 list($build, $case_id) = $this->getCaseId(self::BASE, self::OPEN);
132 if ($build) {
133 $this->addCase(
134 $xml_cases,
135 $case_id,
136 $this->getBlockContents('suite_case_open_title'),
137 $this->getBlockContents('suite_case_open_precond'),
138 $this->getBlockContents('suite_case_open_steps'),
139 $this->getBlockContents('suite_case_open_expected')
140 );
141 }
142 list($build, $case_id) = $this->getCaseId(self::BASE, self::PREPARE);
143 if ($build) {
144 $this->addCase(
145 $xml_cases,
146 $case_id,
147 $this->getBlockContents('suite_case_validate_title'),
148 $this->getBlockContents('suite_case_validate_precond'),
149 $this->getBlockContents('suite_case_validate_steps'),
150 $this->getBlockContents('suite_case_validate_expected')
151 );
152 }
153
154 return $xml_section;
155 }
156
157 protected function addComponentCases(
158 SimpleXMLElement $xml_parent_node,
159 string $section,
160 string $component_name,
161 ComponentEntry $entry,
162 ): void {
163
164 $preconditions = $this->getBlockContents('preconditions');
165
166 list($build, $case_id) = $this->getCaseId($section . '/' . $component_name, self::SHOW);
167 if ($build) {
168 $steps = $this->getTemplate();
169 $steps->setCurrentBlock('steps_show');
170 $steps->setVariable('SECTION', $section);
171 $steps->setVariable('CLICKPATH', $this->getClickpath($entry));
172 $steps->setVariable('TITLE', $entry->getTitle());
173 $steps->parseCurrentBlock();
174 $steps = $steps->get();
175
176 $expected = $this->getTemplate();
177 $expected_examples = $this->getExpectedExamples($entry->getExamples());
178 $expected->setVariable('EXAMPLE_COUNTER', (string) count($entry->getExamples()));
179 $expected->setVariable('EXPECTED', $expected_examples);
180 $expected = $expected->get();
181 $this->addCase(
182 $xml_parent_node,
183 $case_id,
184 $section . ' - ' . $component_name . ': ' . self::SHOW,
185 $preconditions,
186 $steps,
187 $expected
188 );
189 }
190
191 list($build, $case_id) = $this->getCaseId($section . '/' . $component_name, self::VALIDATE);
192 if ($build) {
193 $steps = $this->getTemplate();
194 $steps->setCurrentBlock('steps_validate');
195 $steps->setVariable('SECTION', $section);
196 $steps->setVariable('CLICKPATH', $this->getClickpath($entry));
197 $steps->setVariable('TITLE', $entry->getTitle());
198 $steps->parseCurrentBlock();
199 $steps = $steps->get();
200
201 $expected = $this->getBlockContents('expected_validate');
202
203 $this->addCase(
204 $xml_parent_node,
205 $case_id,
206 $section . ' - ' . $component_name . ': ' . self::VALIDATE,
207 $preconditions,
208 $steps,
209 $expected
210 );
211 }
212 }
213
214 protected function getTemplate(): ilTemplate
215 {
216 return clone $this->case_tpl;
217 }
218
219 protected function getBlockContents(string $block): string
220 {
221 $contents = $this->getTemplate();
222 $contents->touchBlock($block);
223 return "\n" . trim($contents->get()) . "\n";
224 }
225
226 protected function getClickpath(ComponentEntry $entry): string
227 {
228 $clickpath = array_slice(explode('/', $entry->getPath()), 4, -1);
229 $clickpath[] = $entry->getTitle();
230 return implode(' -> ', $clickpath);
231 }
232
233 protected function getCaseId(string $component_path, string $subkey): array
234 {
235 $case_id = '';
236 if (array_key_exists($component_path, self::$CASEIDS)
237 && array_key_exists($subkey, self::$CASEIDS[$component_path])
238 ) {
239 $case_id = self::$CASEIDS[$component_path][$subkey];
240 $this->no_found_ids += 1;
241 print "\n caseId for: $component_path ($subkey)";
242 unset(
243 $this->expected_by_caseids[
244 array_search($case_id, $this->expected_by_caseids)
245 ]
246 );
247 } else {
248 $this->no_new_ids += 1;
249 print "\n no caseId for: $component_path ($subkey)";
250 }
251
252 $build = ($case_id === '' && $this->only_new_cases)
253 || ($case_id !== '' && !$this->only_new_cases);
254
255 return [$build, $case_id];
256 }
257
258 protected function getExpectedExamples(array $examples): string
259 {
260 $expected_show = '';
261 foreach (array_keys($examples) as $idx => $example) {
262 $expected_show = $expected_show . "\n**" . $idx + 1 . '. ' . ucfirst(str_replace('_', ' ', $example)) . '**';
263 $docs = $this->parser->parseYamlStringArrayFromFile($examples[$example]);
264 if (array_key_exists('expected output', $docs)) {
265 $expected_show .= "\n" . $docs['expected output'] . "\n";
266 }
267 }
268 return $expected_show;
269 }
270}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$components
Stores Information of UI Components parsed from YAML, examples and less files.
addSection(SimpleXMLElement $xml_parent_node, string $name, string $description='',)
getClickpath(ComponentEntry $entry)
getCaseId(string $component_path, string $subkey)
addComponentCases(SimpleXMLElement $xml_parent_node, string $section, string $component_name, ComponentEntry $entry,)
addCase(SimpleXMLElement $xml_parent_node, string $id, string $title, string $preconditions, string $steps, string $expected,)
getBlockContents(string $block)
getExpectedExamples(array $examples)
__construct(protected \ilTemplate $case_tpl, protected ExamplesYamlParser $parser, protected bool $only_new_cases)
SimpleXMLElement $xml
special template class to simplify handling of ITX/PEAR
trait TestrailCaseIds
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...