ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
ExamplesTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once("vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../Base.php");
23
28
33{
38 protected const MAY_NOT_HAVE_EXAMPLES = [
39 \ILIAS\UI\Help\Topic::class,
40 \ILIAS\UI\Component\Progress\State\Bar\State::class,
41 \ILIAS\UI\Component\Input\Field\Node\Node::class,
42 \ILIAS\UI\Component\Input\Field\Node\Async::class,
43 \ILIAS\UI\Component\Input\Field\Node\Leaf::class,
44 ];
45
46 protected static string $path_to_base_factory = "components/ILIAS/UI/src/Factory.php";
47 protected Container $dic;
49
50 public function setUp(): void
51 {
52 //This avoids various index not set warnings, which are only relevant in test context.
53 $_SERVER["REQUEST_SCHEME"] = "http";
54 $_SERVER["SERVER_NAME"] = "localhost";
55 $_SERVER["SERVER_PORT"] = "80";
56 $_SERVER["REQUEST_URI"] = "";
57 $_SERVER['SCRIPT_NAME'] = "";
58 $_SERVER['QUERY_STRING'] = "param=1";
59
60 // allows example Unit Tests to find resources like additional data or css
61 if (!defined('ILIAS_ABSOLUTE_PATH')) {
62 define('ILIAS_ABSOLUTE_PATH', realpath(__DIR__ . '/../../../../../'));
63 }
64
65 //This avoids Undefined index: ilfilehash for the moment
66 $_POST["ilfilehash"] = "";
67 $this->setUpMockDependencies();
68 $this->example_parser = new Crawler\ExamplesYamlParser();
69 }
70
76 protected function setUpMockDependencies(): void
77 {
78 $this->dic = new Container();
79 $this->dic["tpl"] = $this->getTemplateFactory()->getTemplate("tpl.main.html", false, false);
80 $this->dic["lng"] = $this->getLanguage();
81 $this->dic["refinery"] = new \ILIAS\Refinery\Factory(
82 new ILIAS\Data\Factory(),
83 $this->getLanguage()
84 );
85
86 (new InitUIFramework())->init($this->dic);
87
88 $this->dic["ui.template_factory"] = $this->getTemplateFactory();
89
90 $this->dic["ilCtrl"] = $this->getMockBuilder(\ilCtrl::class)->disableOriginalConstructor()->onlyMethods([
91 "getFormActionByClass","setParameterByClass","saveParameterByClass","getLinkTargetByClass", "isAsynch"
92 ])->getMock();
93 $this->dic["ilCtrl"]->method("getFormActionByClass")->willReturn("Testing");
94 $this->dic["ilCtrl"]->method("getLinkTargetByClass")->willReturn("2");
95 $this->dic["ilCtrl"]->method("isAsynch")->willReturn(false);
96
97 $this->dic["upload"] = $this->getMockBuilder(FileUpload::class)->getMock();
98
99 $this->dic["tree"] = $this->getMockBuilder(ilTree::class)
100 ->disableOriginalConstructor()
101 ->onlyMethods(["getNodeData"])->getMock();
102
103 $this->dic["tree"]->method("getNodeData")->willReturn([
104 "ref_id" => "1",
105 "title" => "mock root node",
106 "type" => "crs"
107 ]);
108
109 $component_factory = $this->createMock(ilComponentFactory::class);
110 $component_factory->method("getActivePluginsInSlot")->willReturn(new ArrayIterator());
111 $this->dic["component.factory"] = $component_factory;
112
113 $this->dic["help.text_retriever"] = new ILIAS\UI\Help\TextRetriever\Echoing();
114
115 (new InitHttpServices())->init($this->dic);
116 }
117
122 {
123 global $DIC;
125
126 foreach ($this->getEntriesFromCrawler() as $entry) {
127 if (in_array(trim($entry->getNamespace(), '\\'), self::MAY_NOT_HAVE_EXAMPLES, true)) {
128 continue;
129 }
130 if (!$entry->isAbstract()) {
131 $this->assertGreaterThan(
132 0,
133 count($entry->getExamples()),
134 "Non abstract Component " . $entry->getNamespace()
135 . " does not provide any example. Please provide at least one in " . $entry->getExamplesNamespace() . " at " . $entry->getExamplesPath()
136 );
137 }
138 }
139 }
140
141 #[\PHPUnit\Framework\Attributes\DataProvider('getFullFunctionNamesAndPathExample')]
142 public function testAllExamplesRenderAString(string $example_function_name, string $example_path): void
143 {
144 global $DIC;
146
147 include_once $example_path;
148 try {
149 $this->assertIsString($example_function_name(), " Example $example_function_name does not render a string");
150 } catch (NotImplementedException $e) {
151 $this->assertTrue(true);
152 }
153 }
154
155 #[\PHPUnit\Framework\Attributes\DataProvider('getFullFunctionNamesAndPathExample')]
156 public function testAllExamplesHaveExpectedOutcomeInDocs(string $example_function_name, string $example_path)
157 {
158 $docs = $this->example_parser->parseYamlStringArrayFromFile($example_path);
159 $this->assertArrayHasKey('expected output', $docs);
160 }
161
165 protected static function getEntriesFromCrawler(): Crawler\Entry\ComponentEntries
166 {
167 $crawler = new Crawler\FactoriesCrawler();
168 return $crawler->crawlFactory(self::$path_to_base_factory);
169 }
170
171 public static function getFullFunctionNamesAndPathExample(): array
172 {
173 $function_names = [];
174 foreach (static::getEntriesFromCrawler() as $entry) {
175 foreach ($entry->getExamples() as $name => $example_path) {
176 $function_names[$entry->getExamplesNamespace() . "\\" . $name] = [
177 $entry->getExamplesNamespace() . "\\" . $name,
178 $example_path
179 ];
180 }
181 }
182 return $function_names;
183 }
184
185 #[\PHPUnit\Framework\Attributes\DataProvider('getListOfFullscreenExamples')]
186 public function testFullscreenModeExamples(string $example_function_name, string $example_path): void
187 {
188 global $DIC;
190
191 include_once $example_path;
192 try {
193 $this->assertIsString($example_function_name($DIC), " Example $example_function_name does not render a string");
194 } catch (NotImplementedException $e) {
195 $this->assertTrue(true);
196 }
197 }
198
199 public static function getListOfFullscreenExamples(): array
200 {
201 return [
202 ['ILIAS\UI\examples\MainControls\Footer\base', "components/ILIAS/UI/src/examples/MainControls/Footer/base.php"],
203 ['ILIAS\UI\examples\MainControls\MetaBar\renderMetaBarInFullscreenMode', "components/ILIAS/UI/src/examples/MainControls/MetaBar/base_metabar.php"],
204 ['ILIAS\UI\examples\Layout\Page\Standard\getUIMainbarExampleCondensed', "components/ILIAS/UI/src/examples/Layout/Page/Standard/ui_mainbar.php"],
205 ['ILIAS\UI\examples\Layout\Page\Standard\getUIMainbarExampleFull', "components/ILIAS/UI/src/examples/Layout/Page/Standard/ui_mainbar.php"],
206 ['ILIAS\UI\examples\Layout\Page\Standard\ui', "components/ILIAS/UI/src/examples/Layout/Page/Standard/ui.php"],
207 ['ILIAS\UI\examples\MainControls\ModeInfo\renderModeInfoFullscreenMode', "components/ILIAS/UI/src/examples/MainControls/ModeInfo/modeinfo.php"]
208 ];
209 }
210}
Class ExamplesTest Checks if all examples are implemented and properly returning strings.
static getFullFunctionNamesAndPathExample()
static getListOfFullscreenExamples()
const MAY_NOT_HAVE_EXAMPLES
testAllNonAbstractComponentsShowcaseExamples()
testAllExamplesRenderAString(string $example_function_name, string $example_path)
static string $path_to_base_factory
Crawler ExamplesYamlParser $example_parser
testAllExamplesHaveExpectedOutcomeInDocs(string $example_function_name, string $example_path)
testFullscreenModeExamples(string $example_function_name, string $example_path)
Container $dic
setUpMockDependencies()
Some wiring up of dependencies to get all the examples running.
static getEntriesFromCrawler()
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
This HelpTextRetriever simply echo the purpose and the topics for debugging and development purpose.
Definition: Echoing.php:31
This exception indicates that an UI component was accepted by the JF but is not backed by a real impl...
Provides common functionality for UI tests.
Definition: Base.php:337
Responsible for loading the HTTP Service into the dependency injection container of ILIAS.
This is more or less a copy of the removed InitUIFramework file inside the Init component.
$_POST['cmd']
Definition: lti.php:27
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
global $DIC
Definition: shib_login.php:26
getLanguage()