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