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