ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
BasicPersistenceTest.php
Go to the documentation of this file.
1 <?php
2 
20 
37 use Mockery;
39 
40 require_once("libs/composer/vendor/autoload.php");
41 require_once("./Services/ActiveRecord/Connector/class.arConnector.php");
42 require_once("./Services/ActiveRecord/Connector/class.arConnectorMap.php");
43 
54 class BasicPersistenceTest extends TestCase
55 {
57 
59  protected $bucket;
60 
62  protected $persistence;
63 
64  public function setUp(): void
65  {
66  $dic = new Container();
67  $dic[Bucket::class] = function ($c) {
68  return new BasicBucket();
69  };
70 
72  $this->persistence = BasicPersistence::instance($this->createMock(\ilDBInterface::class));
73 
74  $bucket = new BasicBucket(Mockery::mock(Persistence::class));
75  $bucket->setUserId(3);
76  $bucket->setState(State::SCHEDULED);
77 
79  $a = $factory->createInstance(PlusJob::class);
81  $b = $factory->createInstance(PlusJob::class);
83  $c = $factory->createInstance(PlusJob::class);
84 
85  $a->setInput([1, 1]);
86  $b->setInput([1, 1]);
87  $c->setInput([$a, $b]);
88 
90  $userInteraction = $factory->createInstance(DownloadInteger::class);
91  $userInteraction->setInput([$c]);
92 
93  $bucket->setTask($userInteraction);
94 
95  $this->bucket = $bucket;
96  }
97 
101  public function testSave()
102  {
104  $bucketConnector = Mockery::namedMock("bucketConnectorMock", \arConnector::class);
106  $valueConnector = Mockery::namedMock("valueConnectorMock", \arConnector::class);
108  $taskConnector = Mockery::namedMock("taskConnectorMock", \arConnector::class);
110  $valueToTaskConnector = Mockery::namedMock("valueToTaskConnectorMock", \arConnector::class);
111 
112  \arConnectorMap::register(new BucketContainer(), $bucketConnector);
113  \arConnectorMap::register(new ValueContainer(), $valueConnector);
114  \arConnectorMap::register(new TaskContainer(), $taskConnector);
115  \arConnectorMap::register(new ValueToTaskContainer(), $valueToTaskConnector);
116 
117  // Bucket is created.
118  $bucketConnector->shouldReceive("nextID")->once()->andReturn(1);
119  $bucketConnector->shouldReceive("create")->once();
120 
121  // Bucket is updated after tasks are added.
122  $bucketConnector->shouldReceive("affectedRows")->once()->andReturn(1);
123  $bucketConnector->shouldReceive("update")->once()->andReturn(true);
124 
125  //For all four tasks
126  for ($i = 0; $i < 4; $i++) {
127  // task is created
128  $taskConnector->shouldReceive("affectedRows")->once()->andReturn(0);
129  $taskConnector->shouldReceive("nextID")->once()->andReturn(1);
130  $taskConnector->shouldReceive("create")->once();
131 
132  // task is updated after values are linked.
133  $taskConnector->shouldReceive("affectedRows")->once()->andReturn(1);
134  $taskConnector->shouldReceive("update")->once();
135  }
136 
137  // Create seven values
138  $valueConnector->shouldReceive("affectedRows")->times(7)->andReturn(0);
139  $valueConnector->shouldReceive("nextID")->times(7)->andReturn(1);
140  $valueConnector->shouldReceive("create")->times(7);
141 
142  // Connect the seven values to the
143  $valueToTaskConnector->shouldReceive("affectedRows")->times(7)->andReturn(0);
144  $valueToTaskConnector->shouldReceive("nextID")->times(7)->andReturn(1);
145  $valueToTaskConnector->shouldReceive("create")->times(7);
146 
147  $this->persistence->setConnector($bucketConnector);
148  $this->persistence->saveBucketAndItsTasks($this->bucket);
149  }
150 
152  {
153  // We have an unknown observer, we can't update it.
154  $this->expectException(SerializationException::class);
155 
156  $this->persistence->updateBucket($this->bucket);
157  }
158 
159  public function testUpdateObserver()
160  {
161  // We do the whole save part.
162  $this->testSave();
164  $observerConnector = Mockery::namedMock("observerConnectorMock", \arConnector::class);
165 
166  \arConnectorMap::register(new BucketContainer(), $observerConnector);
167 
168  // Observer is updated after tasks are added.
169  $observerConnector->shouldReceive("read")->once()->andReturn([1 => new BucketContainer()]);
170  $observerConnector->shouldReceive("update")->once()->andReturn(true);
171 
172  $this->persistence->setConnector($observerConnector);
173  $this->persistence->updateBucket($this->bucket);
174  }
175 
176  public function testGetObserverIdsOfUser()
177  {
179  $observerConnector = Mockery::namedMock("observerConnectorMock", \arConnector::class);
180 
181  \arConnectorMap::register(new BucketContainer(), $observerConnector);
182  $observerConnector->shouldReceive("readSet")->once()->andReturn([["id" => 2], ["id" => 3]]);
183 
184  $this->persistence->setConnector($observerConnector);
185  $observer_ids = $this->persistence->getBucketIdsOfUser(5);
186  $this->assertEquals($observer_ids, [0 => 2, 1 => 3]);
187  }
188 
189  public function testGetObserverIdsByState()
190  {
192  $observerConnector = Mockery::namedMock("observerConnectorMock", \arConnector::class);
193 
194  \arConnectorMap::register(new BucketContainer(), $observerConnector);
195  $observerConnector->shouldReceive("readSet")->once()->andReturn([["id" => 2], ["id" => 3]]);
196 
197  $this->persistence->setConnector($observerConnector);
198  $observer_ids = $this->persistence->getBucketIdsByState(State::RUNNING);
199  $this->assertEquals($observer_ids, [2 => 2, 3 => 3]);
200  }
201 
202 
203  public function testUserInteraction()
204  {
205  $this->expectException(UserInteractionRequiredException::class);
207  $taskManager = new SyncTaskManager(Mockery::mock(Persistence::class));
209  $taskManager->executeTask($this->bucket->getTask(), new MockObserver());
210  }
211 
212  public function testContinueUserInteraction()
213  {
215  $taskManager = new SyncTaskManager(Mockery::mock(Persistence::class));
216  try {
218  $taskManager->executeTask($this->bucket->getTask(), new NonPersistingObserver($this->bucket));
220  }
221 
222  $download_integer = new DownloadInteger();
223 
224  // We worked on the task up to the user interaction. The current task should be the download integer interaction.
225  self::assertEquals($this->bucket->getCurrentTask()->getType(), $download_integer->getType());
226 
227  $options = $download_integer->getOptions([]); // Download, Dismiss (the input doesnt matter so we pass an empty array)
228 
229  $dismiss = $download_integer->getRemoveOption();
230 
231  $this->bucket->userInteraction($dismiss); // We "click" Dismiss.
232 
233  // As we dismissed the last user interaction the state is finished.
234  self::assertEquals($this->bucket->getState(), State::FINISHED);
235  }
236 
237  public function testContinueUserInteraction2()
238  {
239  $dic = new Container();
240 
242 
243  $c = $this->bucket->getTask();
245  $x = $factory->createInstance(PlusJob::class);
246 
247  $x->setInput([$c, 1]);
248 
249  // we now have (1 + 1) + (1 + 1) -> User Interaction x -> (x + 1) Where x will be the input of the user interaction so: 4.
250  $this->bucket->setTask($x);
251 
253  $taskManager = new SyncTaskManager(Mockery::mock(Persistence::class));
254  try {
256  $taskManager->executeTask($this->bucket->getTask(), new NonPersistingObserver($this->bucket));
258  }
259 
260  $download_integer = new DownloadInteger();
261 
262  // We worked on the task up to the user interaction. The current task should be the download integer interaction.
263  self::assertEquals($this->bucket->getCurrentTask()->getType(), $download_integer->getType());
264 
265  $options = $download_integer->getOptions([]); // Download, Dismiss (the input doesn't matter so we pass an empty array)
266 
267  $dismiss = $download_integer->getRemoveOption();
268 
269  $this->bucket->userInteraction($dismiss); // We "click" Dismiss.
270 
271  // As we dismissed the last user interaction the state is finished.
273  $result = $taskManager->executeTask($this->bucket->getTask(), new NonPersistingObserver($this->bucket));
274  self::assertEquals(5, $result->getValue());
275  }
276 }
$c
Definition: cli.php:38
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static register(ActiveRecord $ar, arConnector $connector)
Customizing of pimple-DIC for ILIAS.
Definition: Container.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...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$dic
Definition: result.php:32
$factory
Definition: metadata.php:75
$i
Definition: metadata.php:41