ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
BasicPersistenceTest.php
Go to the documentation of this file.
1 <?php
2 
4 
22 use Mockery;
24 
25 require_once("libs/composer/vendor/autoload.php");
26 require_once("./Services/ActiveRecord/Connector/class.arConnector.php");
27 require_once("./Services/ActiveRecord/Connector/class.arConnectorMap.php");
28 
39 class BasicPersistenceTest extends TestCase
40 {
42 
44  protected $bucket;
45 
47  protected $persistence;
48 
49  public function setUp()
50  {
51  $dic = new Container();
52  $dic[Bucket::class] = function ($c) {
53  return new BasicBucket();
54  };
55 
56  $factory = new Injector($dic, new BaseDependencyMap());
57  $this->persistence = BasicPersistence::instance();
58 
59  $bucket = new BasicBucket(Mockery::mock(Persistence::class));
60  $bucket->setUserId(3);
61  $bucket->setState(State::SCHEDULED);
62 
64  $a = $factory->createInstance(PlusJob::class);
66  $b = $factory->createInstance(PlusJob::class);
68  $c = $factory->createInstance(PlusJob::class);
69 
70  $a->setInput([1, 1]);
71  $b->setInput([1, 1]);
72  $c->setInput([$a, $b]);
73 
75  $userInteraction = $factory->createInstance(DownloadInteger::class);
76  $userInteraction->setInput([$c]);
77 
78  $bucket->setTask($userInteraction);
79 
80  $this->bucket = $bucket;
81  }
82 
86  public function testSave()
87  {
89  $bucketConnector = Mockery::namedMock("bucketConnectorMock", \arConnector::class);
91  $valueConnector = Mockery::namedMock("valueConnectorMock", \arConnector::class);
93  $taskConnector = Mockery::namedMock("taskConnectorMock", \arConnector::class);
95  $valueToTaskConnector = Mockery::namedMock("valueToTaskConnectorMock", \arConnector::class);
96 
97  \arConnectorMap::register(new BucketContainer(), $bucketConnector);
98  \arConnectorMap::register(new ValueContainer(), $valueConnector);
99  \arConnectorMap::register(new TaskContainer(), $taskConnector);
100  \arConnectorMap::register(new ValueToTaskContainer(), $valueToTaskConnector);
101 
102  // Bucket is created.
103  $bucketConnector->shouldReceive("nextID")->once()->andReturn(1);
104  $bucketConnector->shouldReceive("create")->once();
105 
106  // Bucket is updated after tasks are added.
107  $bucketConnector->shouldReceive("affectedRows")->once()->andReturn(1);
108  $bucketConnector->shouldReceive("update")->once()->andReturn(true);
109 
110  //For all four tasks
111  for ($i = 0; $i < 4; $i++) {
112  // task is created
113  $taskConnector->shouldReceive("affectedRows")->once()->andReturn(0);
114  $taskConnector->shouldReceive("nextID")->once()->andReturn(1);
115  $taskConnector->shouldReceive("create")->once();
116 
117  // task is updated after values are linked.
118  $taskConnector->shouldReceive("affectedRows")->once()->andReturn(1);
119  $taskConnector->shouldReceive("update")->once();
120  }
121 
122  // Create seven values
123  $valueConnector->shouldReceive("affectedRows")->times(7)->andReturn(0);
124  $valueConnector->shouldReceive("nextID")->times(7)->andReturn(1);
125  $valueConnector->shouldReceive("create")->times(7);
126 
127  // Connect the seven values to the
128  $valueToTaskConnector->shouldReceive("affectedRows")->times(7)->andReturn(0);
129  $valueToTaskConnector->shouldReceive("nextID")->times(7)->andReturn(1);
130  $valueToTaskConnector->shouldReceive("create")->times(7);
131 
132  $this->persistence->setConnector($bucketConnector);
133  $this->persistence->saveBucketAndItsTasks($this->bucket);
134  }
135 
137  {
138  // We have an unknown observer, we can't update it.
139  $this->expectException(SerializationException::class);
140 
141  $this->persistence->updateBucket($this->bucket);
142  }
143 
144  public function testUpdateObserver()
145  {
146  // We do the whole save part.
147  $this->testSave();
149  $observerConnector = Mockery::namedMock("observerConnectorMock", \arConnector::class);
150 
151  \arConnectorMap::register(new BucketContainer(), $observerConnector);
152 
153  // Observer is updated after tasks are added.
154  $observerConnector->shouldReceive("read")->once()->andReturn(1);
155  $observerConnector->shouldReceive("update")->once()->andReturn(true);
156 
157  $this->persistence->setConnector($observerConnector);
158  $this->persistence->updateBucket($this->bucket);
159  }
160 
161  public function testGetObserverIdsOfUser()
162  {
164  $observerConnector = Mockery::namedMock("observerConnectorMock", \arConnector::class);
165 
166  \arConnectorMap::register(new BucketContainer(), $observerConnector);
167  $observerConnector->shouldReceive("readSet")->once()->andReturn([["id" => 2], ["id" => 3]]);
168 
169  $this->persistence->setConnector($observerConnector);
170  $observer_ids = $this->persistence->getBucketIdsOfUser(5);
171  $this->assertEquals($observer_ids, [0 => 2, 1 => 3]);
172  }
173 
174  public function testGetObserverIdsByState()
175  {
177  $observerConnector = Mockery::namedMock("observerConnectorMock", \arConnector::class);
178 
179  \arConnectorMap::register(new BucketContainer(), $observerConnector);
180  $observerConnector->shouldReceive("readSet")->once()->andReturn([["id" => 2], ["id" => 3]]);
181 
182  $this->persistence->setConnector($observerConnector);
183  $observer_ids = $this->persistence->getBucketIdsByState(State::RUNNING);
184  $this->assertEquals($observer_ids, [2 => 2, 3 => 3]);
185  }
186 
187 
188  public function testUserInteraction()
189  {
190  $this->setExpectedException(UserInteractionRequiredException::class);
192  $taskManager = new SyncTaskManager(Mockery::mock(Persistence::class));
194  $taskManager->executeTask($this->bucket->getTask(), new MockObserver());
195  }
196 
197  public function testContinueUserInteraction()
198  {
200  $taskManager = new SyncTaskManager(Mockery::mock(Persistence::class));
201  try {
203  $taskManager->executeTask($this->bucket->getTask(), new NonPersistingObserver($this->bucket));
204  } catch (UserInteractionRequiredException $e) {
205  }
206 
207  $download_integer = new DownloadInteger();
208 
209  // We worked on the task up to the user interaction. The current task should be the download integer interaction.
210  self::assertEquals($this->bucket->getCurrentTask()->getType(), $download_integer->getType());
211 
212  $options = $download_integer->getOptions([]); // Download, Dismiss (the input doesnt matter so we pass an empty array)
213 
214  $dismiss = $download_integer->getRemoveOption();
215 
216  $this->bucket->userInteraction($dismiss); // We "click" Dismiss.
217 
218  // As we dismissed the last user interaction the state is finished.
219  self::assertEquals($this->bucket->getState(), State::FINISHED);
220  }
221 
222  public function testContinueUserInteraction2()
223  {
224  $dic = new Container();
225 
226  $factory = new Injector($dic, new EmptyDependencyMap());
227 
228  $c = $this->bucket->getTask();
230  $x = $factory->createInstance(PlusJob::class);
231 
232  $x->setInput([$c, 1]);
233 
234  // we now have (1 + 1) + (1 + 1) -> User Interaction x -> (x + 1) Where x will be the input of the user interaction so: 4.
235  $this->bucket->setTask($x);
236 
238  $taskManager = new SyncTaskManager(Mockery::mock(Persistence::class));
239  try {
241  $taskManager->executeTask($this->bucket->getTask(), new NonPersistingObserver($this->bucket));
242  } catch (UserInteractionRequiredException $e) {
243  }
244 
245  $download_integer = new DownloadInteger();
246 
247  // We worked on the task up to the user interaction. The current task should be the download integer interaction.
248  self::assertEquals($this->bucket->getCurrentTask()->getType(), $download_integer->getType());
249 
250  $options = $download_integer->getOptions([]); // Download, Dismiss (the input doesn't matter so we pass an empty array)
251 
252  $dismiss = $download_integer->getRemoveOption();
253 
254  $this->bucket->userInteraction($dismiss); // We "click" Dismiss.
255 
256  // As we dismissed the last user interaction the state is finished.
258  $result = $taskManager->executeTask($this->bucket->getTask(), new NonPersistingObserver($this->bucket));
259  self::assertEquals(5, $result->getValue());
260  }
261 }
$result
static register(ActiveRecord $ar, arConnector $connector)
$factory
Definition: metadata.php:43
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:16
$i
Definition: disco.tpl.php:19
$x
Definition: complexTest.php:9