ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ilComponentDefinitionReaderTest.php
Go to the documentation of this file.
1 <?php
2 
21 
23 {
24 }
26 {
27 }
28 
29 class ilComponentDefinitionReaderTest extends TestCase
30 {
34 
35  public static array $components = [
36  ["components/ILIAS", "A_Module", "/path/to/module.xml"],
37  ["components/ILIAS", "A_Service", "/other/path/to/service.xml"]
38  ];
39 
40  protected function setUp(): void
41  {
42  $this->processor1 = $this->createMock(ilComponentDefinitionProcessorMock1::class);
43  $this->processor2 = $this->createMock(ilComponentDefinitionProcessorMock2::class);
44 
45  $this->reader = new class ($this->processor1, $this->processor2) extends ilComponentDefinitionReader {
46  protected function getComponents(): Iterator
47  {
49  }
50  public $read_files = [];
51  protected function readFile(string $path): string
52  {
53  $this->read_files[] = $path;
54  if ($path === "/path/to/module.xml") {
55  return
56 '<?xml version = "1.0" encoding = "UTF-8"?>
57 <module a1="a1" a2="a2">
58  <tag1>
59  <tag11></tag11>
60  </tag1>
61  <tag2></tag2>
62 </module>';
63  }
64  if ($path === "/other/path/to/service.xml") {
65  return
66 '<?xml version = "1.0" encoding = "UTF-8"?>
67 <service>
68 </service>';
69  }
70  return "";
71  }
72  };
73  }
74 
75  public function testPurge(): void
76  {
77  $this->processor1
78  ->expects($this->once())
79  ->method("purge")
80  ->with();
81  $this->processor2
82  ->expects($this->once())
83  ->method("purge")
84  ->with();
85 
86  $this->reader->purge();
87  }
88 
89  public function testGetComponents(): void
90  {
91  $reader = new class () extends ilComponentDefinitionReader {
92  public function __construct()
93  {
94  }
95  public function _getComponents(): array
96  {
97  return iterator_to_array($this->getComponents());
98  }
99  };
100 
101  $components = $reader->_getComponents();
102 
103  $this->assertIsArray($components);
104  $this->assertContains(["components/ILIAS", "Course", realpath(__DIR__ . "/../../../../components/ILIAS/Course/module.xml")], $components);
105  $this->assertContains(["components/ILIAS", "Component", realpath(__DIR__ . "/../../../../components/ILIAS/Component/service.xml")], $components);
106  }
107 
108  public function testReadComponentDefinitions(): void
109  {
110  $processor1_stack = [];
111  $this->processor1
112  ->method("beginComponent")
113  ->willReturnCallback(function ($s1, $s2) use (&$processor1_stack) {
114  $processor1_stack[] = "beginComponent";
115  $processor1_stack[] = $s1;
116  $processor1_stack[] = $s2;
117  });
118  $this->processor1
119  ->method("endComponent")
120  ->willReturnCallback(function ($s1, $s2) use (&$processor1_stack) {
121  $processor1_stack[] = "endComponent";
122  $processor1_stack[] = $s1;
123  $processor1_stack[] = $s2;
124  });
125  $this->processor1
126  ->method("beginTag")
127  ->willReturnCallback(function ($s1, $s2) use (&$processor1_stack) {
128  $processor1_stack[] = "beginTag";
129  $processor1_stack[] = $s1;
130  $processor1_stack[] = $s2;
131  });
132  $this->processor1
133  ->method("endTag")
134  ->willReturnCallback(function ($s1) use (&$processor1_stack) {
135  $processor1_stack[] = "endTag";
136  $processor1_stack[] = $s1;
137  });
138 
139  $processor2_stack = [];
140  $this->processor2
141  ->method("beginComponent")
142  ->willReturnCallback(function ($s1, $s2) use (&$processor2_stack) {
143  $processor2_stack[] = "beginComponent";
144  $processor2_stack[] = $s1;
145  $processor2_stack[] = $s2;
146  });
147  $this->processor2
148  ->method("endComponent")
149  ->willReturnCallback(function ($s1, $s2) use (&$processor2_stack) {
150  $processor2_stack[] = "endComponent";
151  $processor2_stack[] = $s1;
152  $processor2_stack[] = $s2;
153  });
154  $this->processor2
155  ->method("beginTag")
156  ->willReturnCallback(function ($s1, $s2) use (&$processor2_stack) {
157  $processor2_stack[] = "beginTag";
158  $processor2_stack[] = $s1;
159  $processor2_stack[] = $s2;
160  });
161  $this->processor2
162  ->method("endTag")
163  ->willReturnCallback(function ($s1) use (&$processor2_stack) {
164  $processor2_stack[] = "endTag";
165  $processor2_stack[] = $s1;
166  });
167 
168 
169  $this->reader->readComponentDefinitions();
170 
171 
172  $this->assertEquals([self::$components[0][2], self::$components[1][2]], $this->reader->read_files);
173 
174  $expected_processor_stack = [
175  "beginComponent",
176  self::$components[0][1], // A_Module
177  self::$components[0][0], // Modules
178  "beginTag",
179  "module", ["a1" => "a1", "a2" => "a2"],
180  "beginTag",
181  "tag1", [],
182  "beginTag",
183  "tag11", [],
184  "endTag",
185  "tag11",
186  "endTag",
187  "tag1",
188  "beginTag",
189  "tag2", [],
190  "endTag",
191  "tag2",
192  "endTag",
193  "module",
194  "endComponent",
195  self::$components[0][1], // A_Module
196  self::$components[0][0], // Modules
197  "beginComponent",
198  self::$components[1][1], // A_Service
199  self::$components[1][0], // Services
200  "beginTag",
201  "service", [],
202  "endTag",
203  "service",
204  "endComponent",
205  self::$components[1][1], // A_Service
206  self::$components[1][0], // Services
207  ];
208  $this->assertEquals($expected_processor_stack, $processor1_stack);
209  $this->assertEquals($expected_processor_stack, $processor2_stack);
210  }
211 }
ilComponentDefinitionProcessor $processor1
ilComponentDefinitionProcessor $processor2
An ilComponentDefinitionProcessor processes some attributes from a component.xml (i.e.
$path
Definition: ltiservices.php:30
$components
__construct(Container $dic, ilPlugin $plugin)