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