ILIAS  release_8 Revision v8.24
ilCtrlTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/* Copyright (c) 2021 Thibeau Fuhrer <thf@studer-raimann.ch> Extended GPL, see docs/LICENSE */
6
7use PHPUnit\Framework\TestCase;
9use Psr\Http\Message\ServerRequestInterface;
11use ILIAS\Refinery\Factory as Refinery;
12
18class ilCtrlTest extends TestCase
19{
20 public function testCallBaseClassWithoutBaseClass(): void
21 {
22 $ctrl = $this->getMockedCtrl();
23
24 $this->expectException(ilCtrlException::class);
25 $this->expectExceptionMessage(ilCtrl::class . "::callBaseClass was not given a baseclass and the request doesn't include one either.");
26 $ctrl->callBaseClass();
27 }
28
30 {
31 $structure = $this->createMock(ilCtrlStructureInterface::class);
32 $structure
33 ->method('isBaseClass')
34 ->willReturn(false)
35 ;
36
37 $ctrl = new ilCtrl(
38 $structure,
39 $this->createMock(ilCtrlTokenRepositoryInterface::class),
40 $this->createMock(ilCtrlPathFactoryInterface::class),
41 $this->createMock(ilCtrlContextInterface::class),
42 $this->createMock(ResponseSenderStrategy::class),
43 $this->createMock(ServerRequestInterface::class),
44 $this->createMock(RequestWrapper::class),
45 $this->createMock(RequestWrapper::class),
46 $this->createMock(Refinery::class),
47 $this->createMock(ilComponentFactory::class),
48 $this->createMock(ilCtrlQueryParserInterface::class)
49 );
50
51 $invalid_baseclass = ilCtrlInvalidGuiClass::class;
52
53 $this->expectException(ilCtrlException::class);
54 $this->expectExceptionMessage("Provided class '$invalid_baseclass' is not a baseclass");
55 $ctrl->callBaseClass($invalid_baseclass);
56 }
57
59 {
60 $ctrl = $this->getMockedCtrl();
61
62 require_once __DIR__ . '/Data/GUI/class.ilCtrlInvalidGuiClass.php';
63
64 $this->expectException(ilCtrlException::class);
65 $this->expectExceptionMessage(ilCtrlInvalidGuiClass::class . " doesn't implement executeCommand().");
66 $ctrl->forwardCommand(new ilCtrlInvalidGuiClass());
67 }
68
69 public function testForwardCommandWithValidObject(): void
70 {
71 $ctrl = $this->getMockedCtrl();
72
73 require_once __DIR__ . '/Data/GUI/class.ilCtrlCommandClass1TestGUI.php';
74
75 $this->assertEquals(
76 ilCtrlCommandClass1TestGUI::class,
77 $ctrl->forwardCommand(new ilCtrlCommandClass1TestGUI())
78 );
79 }
80
81 public function testGetHtmlWithInvalidObject(): void
82 {
83 $ctrl = $this->getMockedCtrl();
84
85 require_once __DIR__ . '/Data/GUI/class.ilCtrlInvalidGuiClass.php';
86
87 $this->expectException(ilCtrlException::class);
88 $this->expectExceptionMessage(ilCtrlInvalidGuiClass::class . " doesn't implement getHTML().");
89 $ctrl->getHTML(new ilCtrlInvalidGuiClass());
90 }
91
92 public function testGetHtmlWithValidObject(): void
93 {
94 $ctrl = $this->getMockedCtrl();
95
96 require_once __DIR__ . '/Data/GUI/class.ilCtrlCommandClass2TestGUI.php';
97
98 $this->assertEquals('foo', $ctrl->getHTML(new ilCtrlCommandClass2TestGUI()));
99 }
100
102 {
103 $ctrl = $this->getMockedCtrl();
104
105 require_once __DIR__ . '/Data/GUI/class.ilCtrlCommandClass2TestGUI.php';
106
107 $this->assertEquals('bar', $ctrl->getHTML(new ilCtrlCommandClass2TestGUI(), ['bar']));
108 }
109
110 public function testGetCmdWithoutProvidedCmd(): void
111 {
112 $ctrl = $this->getMockedCtrl();
113
114 // @TODO: change this to assertNull() once null coalescing operators are removed.
115 $this->assertEmpty($ctrl->getCmd());
116 }
117
119 {
120 $ctrl = new ilCtrl(
121 $this->createMock(ilCtrlStructureInterface::class),
122 $this->createMock(ilCtrlTokenRepositoryInterface::class),
123 $this->createMock(ilCtrlPathFactoryInterface::class),
124 $this->createMock(ilCtrlContextInterface::class),
125 $this->createMock(ResponseSenderStrategy::class),
126 $this->createMock(ServerRequestInterface::class),
127 $this->createMock(RequestWrapper::class),
128 $this->createMock(RequestWrapper::class),
129 $this->createMock(Refinery::class),
130 $this->createMock(ilComponentFactory::class),
131 $this->createMock(ilCtrlQueryParserInterface::class)
132 );
133
134 $fallback_cmd = 'fallback_cmd_test';
135
136 $this->assertEquals(
137 $fallback_cmd,
138 $ctrl->getCmd($fallback_cmd)
139 );
140 }
141
142 // @TODO: coming soon, I promise!
143
144 /*
145 public function testGetCmdWithUnsafePostCmd() : void
146 {
147
148 }
149
150 public function testGetCmdWithSafePostCmd() : void
151 {
152
153 }
154
155 public function testGetCmdWithUnsafeGetCmd() : void
156 {
157
158 }
159
160 public function testGetCmdWithSafeGetCmd() : void
161 {
162
163 }
164
165 public function testGetCmdWithValidCsrfToken() : void
166 {
167
168 }
169
170 public function testGetCmdWithInvalidCsrfToken() : void
171 {
172
173 }
174
175 public function testGetNextClass() : void
176 {
177
178 }
179
180 public function testGetParameterArrayByClass() : void
181 {
182
183 }
184
185 public function testGetLinkTargetByClass() : void
186 {
187
188 }
189
190 public function testGetFormActionByClass() : void
191 {
192
193 }
194
195 public function testGetParentReturnByClass() : void
196 {
197
198 }
199 */
200
207 private function getMockedCtrl(): ilCtrlInterface
208 {
209 return new ilCtrl(
210 $this->createMock(ilCtrlStructureInterface::class),
211 $this->createMock(ilCtrlTokenRepositoryInterface::class),
212 $this->createMock(ilCtrlPathFactoryInterface::class),
213 $this->createMock(ilCtrlContextInterface::class),
214 $this->createMock(ResponseSenderStrategy::class),
215 $this->createMock(ServerRequestInterface::class),
216 $this->createMock(RequestWrapper::class),
217 $this->createMock(RequestWrapper::class),
218 $this->createMock(Refinery::class),
219 $this->createMock(ilComponentFactory::class),
220 $this->createMock(ilCtrlQueryParserInterface::class)
221 );
222 }
223}
Builds data types.
Definition: Factory.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilCtrlCommandClass2TestGUI.
Class ilCtrlInvalidGuiClass.
Class ilCtrlTest.
Definition: ilCtrlTest.php:19
testCallBaseClassWithoutBaseClass()
Definition: ilCtrlTest.php:20
testGetHtmlWithInvalidObject()
Definition: ilCtrlTest.php:81
testGetHtmlWithValidObjectAndParameters()
Definition: ilCtrlTest.php:101
testGetCmdWithoutProvidedCmdAndFallback()
Definition: ilCtrlTest.php:118
testCallBaseClassWithInvalidProvidedBaseClass()
Definition: ilCtrlTest.php:29
testForwardCommandWithValidObject()
Definition: ilCtrlTest.php:69
testGetHtmlWithValidObject()
Definition: ilCtrlTest.php:92
testGetCmdWithoutProvidedCmd()
Definition: ilCtrlTest.php:110
testForwardCommandWithInvalidObject()
Definition: ilCtrlTest.php:58
getMockedCtrl()
Helper function that returns an ilCtrl instance with mocked dependencies.
Definition: ilCtrlTest.php:207
Class ilCtrl provides processing control methods.
Interface RequestWrapper.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...