ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AchieveCommandTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Setup;
24use PHPUnit\Framework\TestCase;
25use Symfony\Component\Console\Tester\CommandTester;
27use ILIAS\Data\Factory as DataFactory;
28use Symfony\Component\Console\Input\InputInterface;
31use Symfony\Component\Console\Output\StreamOutput;
33use ilLanguage;
34use ilSetupAgent;
40use LogicException;
41
42class TestConfig implements Config
43{
44 public function getConfig(string $name)
45 {
46 return ["a" => "b"];
47 }
48
49 public function getKeys()
50 {
51 return ["a"];
52 }
53}
54
56{
57 public function readAgentConfig(Agent $agent, InputInterface $input, ?string $use_config_field = null): ?Config
58 {
59 return new Setup\ConfigCollection(["Test" => new TestConfig()]);
60 }
61}
62
63class AchieveCommandTest extends TestCase
64{
68 protected $config_reader;
69
73 protected $agent_finder;
74
78 protected $refinery;
79
83 protected $command;
84
85 public function setUp(): void
86 {
87 $this->config_reader = $this->createMock(Setup\CLI\ConfigReader::class);
88 $this->agent_finder = $this->createMock(Setup\AgentFinder::class);
89 $this->refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
90 $this->command = new Setup\CLI\AchieveCommand($this->agent_finder, $this->config_reader, [], $this->refinery);
91 }
92
93 public function testBasicFunctionality(): void
94 {
95 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
96
97 $agent = $this->createMock(Setup\AgentCollection::class);
98 $config_reader = $this->createMock(Setup\CLI\ConfigReader::class);
99 $agent_finder = $this->createMock(Setup\AgentFinder::class);
101
102 $tester = new CommandTester($command);
103
104 $config = $this->createMock(Setup\ConfigCollection::class);
105 $config_file = "config_file";
106 $config_file_content = ["config_file"];
107 $objective_name = "my.objective";
108
109 $objective = $this->createMock(Setup\Objective::class);
110 $env = $this->createMock(Setup\Environment::class);
111
113 ->expects($this->once())
114 ->method("readConfigFile")
115 ->with($config_file)
116 ->willReturn($config_file_content);
117
119 ->expects($this->once())
120 ->method("getAgents")
121 ->with()
122 ->willReturn($agent);
123
124 $agent
125 ->expects($this->once())
126 ->method("hasConfig")
127 ->willReturn(true);
128
129 $agent
130 ->expects($this->once())
131 ->method("getArrayToConfigTransformation")
132 ->with()
133 ->willReturn($refinery->custom()->transformation(function ($v) use ($config_file_content, $config) {
134 $this->assertEquals($v, $config_file_content);
135 return $config;
136 }));
137
138 $namedObjectives = [
139 "my.objective" => new Setup\ObjectiveConstructor(
140 "My Objective",
141 static function () use ($objective): Setup\Objective {
142 return new Setup\ObjectiveCollection(
143 "My Objective",
144 false,
145 $objective
146 );
147 }
148 )
149 ];
150
151 $agent
152 ->expects($this->once())
153 ->method("getNamedObjectives")
154 ->willReturn($namedObjectives);
155
156 $objective
157 ->expects($this->once())
158 ->method("getPreconditions")
159 ->willReturn([]);
160
161 $objective
162 ->method("isApplicable")
163 ->willReturn(true);
164
165 $objective
166 ->expects($this->once())
167 ->method("achieve")
168 ->willReturn($env);
169
170 $tester->execute([
171 "config" => $config_file,
172 "objective" => $objective_name
173 ]);
174 }
175
176 public function testAchieveObjective(): void
177 {
178 $refinery = new Refinery($this->createMock(DataFactory::class), $this->createMock(\ilLanguage::class));
179
180 $agent = $this->createMock(Setup\AgentCollection::class);
181 $config_reader = $this->createMock(Setup\CLI\ConfigReader::class);
182 $agent_finder = $this->createMock(Setup\AgentFinder::class);
184
185 $tester = new CommandTester($command);
186
187 $config = $this->createMock(Setup\ConfigCollection::class);
188 $config_file = "config_file";
189 $config_file_content = ["config_file"];
190 $objective_name = "my.objective";
191
192 $objective = $this->createMock(Setup\Objective::class);
193 $env = $this->createMock(Setup\Environment::class);
194
196 ->expects($this->once())
197 ->method("readConfigFile")
198 ->with($config_file)
199 ->willReturn($config_file_content);
200
202 ->expects($this->once())
203 ->method("getAgents")
204 ->with()
205 ->willReturn($agent);
206
207 $agent
208 ->expects($this->once())
209 ->method("hasConfig")
210 ->willReturn(true);
211
212 $agent
213 ->expects($this->once())
214 ->method("getArrayToConfigTransformation")
215 ->with()
216 ->willReturn($refinery->custom()->transformation(function ($v) use ($config_file_content, $config) {
217 $this->assertEquals($v, $config_file_content);
218 return $config;
219 }));
220
221
222 $seen_config = null;
223 $agent
224 ->expects($this->once())
225 ->method("getNamedObjectives")
226 ->will($this->returnCallback(function ($config) use (&$seen_config, $objective) {
227 $seen_config = $config;
228 return [
229 "my.objective" => new Setup\ObjectiveConstructor(
230 "My Objective",
231 static function () use ($objective): Setup\Objective {
232 return $objective;
233 }
234 )
235 ];
236 }));
237
238 $objective
239 ->expects($this->once())
240 ->method("getPreconditions")
241 ->willReturn([]);
242
243 $objective
244 ->method("isApplicable")
245 ->willReturn(true);
246
247 $objective
248 ->expects($this->once())
249 ->method("achieve")
250 ->willReturn($env);
251
252 $tester->execute([
253 "config" => $config_file,
254 "objective" => $objective_name
255 ]);
256
257 $this->assertSame($seen_config, $config);
258 }
259}
Builds data types.
Definition: Factory.php:36
An agent that is just a collection of some other agents.
Achieves an objective.
A collection of some configurations.
A objective collection is a objective that is achieved once all subobjectives are achieved.
readAgentConfig(Agent $agent, InputInterface $input, ?string $use_config_field=null)
language handling
Class ilUICoreSetupAgent.
A transformation is a function from one datatype to another.
A agent is some component that performs part of the setup process.
Definition: Agent.php:30
A configuration for the setup.
Definition: Config.php:27
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...