ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ExamplesTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once("vendor/composer/vendor/autoload.php");
22 require_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  ];
42 
43  protected static string $path_to_base_factory = "components/ILIAS/UI/src/Factory.php";
44  protected Container $dic;
45  protected Crawler\ExamplesYamlParser $example_parser;
46 
47  public function setUp(): void
48  {
49  //This avoids various index not set warnings, which are only relevant in test context.
50  $_SERVER["REQUEST_SCHEME"] = "http";
51  $_SERVER["SERVER_NAME"] = "localhost";
52  $_SERVER["SERVER_PORT"] = "80";
53  $_SERVER["REQUEST_URI"] = "";
54  $_SERVER['SCRIPT_NAME'] = "";
55  $_SERVER['QUERY_STRING'] = "param=1";
56 
57  //This avoids Undefined index: ilfilehash for the moment
58  $_POST["ilfilehash"] = "";
59  $this->setUpMockDependencies();
60  $this->example_parser = new Crawler\ExamplesYamlParser();
61  }
62 
68  protected function setUpMockDependencies(): void
69  {
70  $this->dic = new Container();
71  $this->dic["tpl"] = $this->getTemplateFactory()->getTemplate("tpl.main.html", false, false);
72  $this->dic["lng"] = $this->getLanguage();
73  $this->dic["refinery"] = new \ILIAS\Refinery\Factory(
74  new ILIAS\Data\Factory(),
75  $this->getLanguage()
76  );
77  (new InitUIFramework())->init($this->dic);
78 
79  $this->dic["ui.template_factory"] = $this->getTemplateFactory();
80 
81  $this->dic["ilCtrl"] = $this->getMockBuilder(\ilCtrl::class)->disableOriginalConstructor()->onlyMethods([
82  "getFormActionByClass","setParameterByClass","saveParameterByClass","getLinkTargetByClass", "isAsynch"
83  ])->getMock();
84  $this->dic["ilCtrl"]->method("getFormActionByClass")->willReturn("Testing");
85  $this->dic["ilCtrl"]->method("getLinkTargetByClass")->willReturn("2");
86  $this->dic["ilCtrl"]->method("isAsynch")->willReturn(false);
87 
88  $this->dic["upload"] = $this->getMockBuilder(FileUpload::class)->getMock();
89 
90  $this->dic["tree"] = $this->getMockBuilder(ilTree::class)
91  ->disableOriginalConstructor()
92  ->onlyMethods(["getNodeData"])->getMock();
93 
94  $this->dic["tree"]->method("getNodeData")->willReturn([
95  "ref_id" => "1",
96  "title" => "mock root node",
97  "type" => "crs"
98  ]);
99 
100  $component_factory = $this->createMock(ilComponentFactory::class);
101  $component_factory->method("getActivePluginsInSlot")->willReturn(new ArrayIterator());
102  $this->dic["component.factory"] = $component_factory;
103 
104  $this->dic["help.text_retriever"] = new ILIAS\UI\Help\TextRetriever\Echoing();
105 
106  (new InitHttpServices())->init($this->dic);
107  }
108 
113  {
114  global $DIC;
115  $DIC = $this->dic;
116 
117  foreach ($this->getEntriesFromCrawler() as $entry) {
118  if (in_array(trim($entry->getNamespace(), '\\'), self::MAY_NOT_HAVE_EXAMPLES, true)) {
119  continue;
120  }
121  if (!$entry->isAbstract()) {
122  $this->assertGreaterThan(
123  0,
124  count($entry->getExamples()),
125  "Non abstract Component " . $entry->getNamespace()
126  . " does not provide any example. Please provide at least one in " . $entry->getExamplesNamespace() . " at " . $entry->getExamplesPath()
127  );
128  }
129  }
130  }
131 
135  public function testAllExamplesRenderAString(string $example_function_name, string $example_path): void
136  {
137  global $DIC;
138  $DIC = $this->dic;
139 
140  include_once $example_path;
141  try {
142  $this->assertIsString($example_function_name(), " Example $example_function_name does not render a string");
143  } catch (NotImplementedException $e) {
144  $this->assertTrue(true);
145  }
146  }
147 
151  public function testAllExamplesHaveExpectedOutcomeInDocs(string $example_function_name, string $example_path)
152  {
153  $docs = $this->example_parser->parseYamlStringArrayFromFile($example_path);
154  $this->assertArrayHasKey('expected output', $docs);
155  }
156 
160  protected static function getEntriesFromCrawler(): Crawler\Entry\ComponentEntries
161  {
162  $crawler = new Crawler\FactoriesCrawler();
163  return $crawler->crawlFactory(self::$path_to_base_factory);
164  }
165 
166  public static function getFullFunctionNamesAndPathExample(): array
167  {
168  $function_names = [];
169  foreach (static::getEntriesFromCrawler() as $entry) {
170  foreach ($entry->getExamples() as $name => $example_path) {
171  $function_names[$entry->getExamplesNamespace() . "\\" . $name] = [
172  $entry->getExamplesNamespace() . "\\" . $name,
173  $example_path
174  ];
175  }
176  }
177  return $function_names;
178  }
179 
183  public function testFullscreenModeExamples(string $example_function_name, string $example_path): void
184  {
185  global $DIC;
186  $DIC = $this->dic;
187 
188  include_once $example_path;
189  try {
190  $this->assertIsString($example_function_name($DIC), " Example $example_function_name does not render a string");
191  } catch (NotImplementedException $e) {
192  $this->assertTrue(true);
193  }
194  }
195 
196  public static function getListOfFullscreenExamples(): array
197  {
198  return [
199  ['ILIAS\UI\examples\MainControls\Footer\base', "components/ILIAS/UI/src/examples/MainControls/Footer/base.php"],
200  ['ILIAS\UI\examples\MainControls\MetaBar\renderMetaBarInFullscreenMode', "components/ILIAS/UI/src/examples/MainControls/MetaBar/base_metabar.php"],
201  ['ILIAS\UI\examples\Layout\Page\Standard\getUIMainbarExampleCondensed', "components/ILIAS/UI/src/examples/Layout/Page/Standard/ui_mainbar.php"],
202  ['ILIAS\UI\examples\Layout\Page\Standard\getUIMainbarExampleFull', "components/ILIAS/UI/src/examples/Layout/Page/Standard/ui_mainbar.php"],
203  ['ILIAS\UI\examples\Layout\Page\Standard\ui', "components/ILIAS/UI/src/examples/Layout/Page/Standard/ui.php"],
204  ['ILIAS\UI\examples\MainControls\ModeInfo\renderModeInfoFullscreenMode', "components/ILIAS/UI/src/examples/MainControls/ModeInfo/modeinfo.php"]
205  ];
206  }
207 }
static getListOfFullscreenExamples()
Class ExamplesTest Checks if all examples are implemented and properly returning strings.
Interface Observer Contains several chained tasks and infos about them.
setUpMockDependencies()
Some wiring up of dependencies to get all the examples running.
Crawler ExamplesYamlParser $example_parser
$_POST['cmd']
Definition: lti.php:27
This exception indicates that an UI component was accepted by the JF but is not backed by a real impl...
static string $path_to_base_factory
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
Responsible for loading the UI Framework into the dependency injection container of ILIAS...
static getEntriesFromCrawler()
getLanguage()
static getFullFunctionNamesAndPathExample()
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
testAllExamplesRenderAString(string $example_function_name, string $example_path)
getFullFunctionNamesAndPathExample
global $DIC
Definition: shib_login.php:25
testAllExamplesHaveExpectedOutcomeInDocs(string $example_function_name, string $example_path)
getFullFunctionNamesAndPathExample
testFullscreenModeExamples(string $example_function_name, string $example_path)
getListOfFullscreenExamples
Container $dic
const MAY_NOT_HAVE_EXAMPLES
testAllNonAbstractComponentsShowcaseExamples()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Crawler.php:21
Responsible for loading the HTTP Service into the dependency injection container of ILIAS...