ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
CronJobManagerTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
28 {
29  private function createClockFactoryMock(): \ILIAS\Data\Clock\ClockFactory
30  {
31  $clock_factory = $this->createMock(ILIAS\Data\Clock\ClockFactory::class);
32  $now = new DateTimeImmutable('@' . time());
33  $clock_factory->method('system')->willReturn(
34  new class ($now) implements \ILIAS\Data\Clock\ClockInterface {
35  private DateTimeImmutable $now;
36 
37  public function __construct(DateTimeImmutable $now)
38  {
39  $this->now = $now;
40  }
41 
42  public function now(): DateTimeImmutable
43  {
44  return $this->now;
45  }
46  }
47  );
48 
49  return $clock_factory;
50  }
51 
53  {
54  $db = $this->createMock(ilDBInterface::class);
55  $setting = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->getMock();
56  $logger = $this->getMockBuilder(ilLogger::class)->disableOriginalConstructor()->getMock();
57  $repository = $this->createMock(JobRepository::class);
58  $job = $this->createMock(CronJob::class);
59  $user = $this->getMockBuilder(ilObjUser::class)
60  ->disableOriginalConstructor()
61  ->onlyMethods(['getId'])
62  ->getMock();
63 
64  $clock_factory = $this->createClockFactoryMock();
65 
66  $cronManager = new JobManagerImpl(
67  $repository,
68  $db,
69  $setting,
70  $logger,
71  $clock_factory
72  );
73 
74  $repository->expects($this->once())->method('updateJobResult')->with(
75  $job,
76  $clock_factory->system()->now(),
77  $user,
78  $this->isInstanceOf(JobResult::class),
79  true
80  );
81 
82  $repository->expects($this->once())->method('resetJob')->with(
83  $job
84  );
85 
86  $repository->expects($this->once())->method('activateJob')->with(
87  $job,
88  $clock_factory->system()->now(),
89  $user,
90  true
91  );
92 
93  $job->expects($this->once())->method('activationWasToggled')->with(
94  $db,
95  $setting,
96  true
97  );
98 
99  $cronManager->resetJob($job, $user);
100  }
101 
103  {
104  $db = $this->createMock(ilDBInterface::class);
105  $setting = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->getMock();
106  $logger = $this->getMockBuilder(ilLogger::class)->disableOriginalConstructor()->getMock();
107  $repository = $this->createMock(JobRepository::class);
108  $job = $this->createMock(CronJob::class);
109  $user = $this->getMockBuilder(ilObjUser::class)
110  ->disableOriginalConstructor()
111  ->onlyMethods(['getId'])
112  ->getMock();
113 
114  $clock_factory = $this->createClockFactoryMock();
115 
116  $cronManager = new JobManagerImpl(
117  $repository,
118  $db,
119  $setting,
120  $logger,
121  $clock_factory
122  );
123 
124  $consecutive = [
125  [$job, $clock_factory->system()->now(), $user, true],
126  [$job, $clock_factory->system()->now(), $user, false]
127  ];
128  $repository
129  ->expects($this->exactly(2))
130  ->method('activateJob')
131  ->willReturnCallback(
132  function ($job, $date, $user, $flag) use (&$consecutive): void {
133  [$ejob, $edate, $euser, $eflag] = array_shift($consecutive);
134  $this->assertEquals($ejob, $job);
135  $this->assertEquals($edate, $date);
136  $this->assertEquals($euser, $user);
137  $this->assertEquals($eflag, $flag);
138  }
139  );
140 
141  $job->expects($this->exactly(2))->method('activationWasToggled')->with(
142  $db,
143  $setting,
144  true
145  );
146 
147  $cronManager->activateJob($job, $user, true);
148  $cronManager->activateJob($job, $user, false);
149  }
150 
152  {
153  $db = $this->createMock(ilDBInterface::class);
154  $setting = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->getMock();
155  $logger = $this->getMockBuilder(ilLogger::class)->disableOriginalConstructor()->getMock();
156  $repository = $this->createMock(JobRepository::class);
157  $job = $this->createMock(CronJob::class);
158  $user = $this->getMockBuilder(ilObjUser::class)
159  ->disableOriginalConstructor()
160  ->onlyMethods(['getId'])
161  ->getMock();
162 
163  $clock_factory = $this->createClockFactoryMock();
164 
165  $cronManager = new JobManagerImpl(
166  $repository,
167  $db,
168  $setting,
169  $logger,
170  $clock_factory
171  );
172 
173  $consecutive = [
174  [$job, $clock_factory->system()->now(), $user, true],
175  [$job, $clock_factory->system()->now(), $user, false]
176  ];
177  $repository
178  ->expects($this->exactly(2))
179  ->method('deactivateJob')
180  ->willReturnCallback(
181  function ($job, $date, $user, $flag) use (&$consecutive): void {
182  [$ejob, $edate, $euser, $eflag] = array_shift($consecutive);
183  $this->assertEquals($ejob, $job);
184  $this->assertEquals($edate, $date);
185  $this->assertEquals($euser, $user);
186  $this->assertEquals($eflag, $flag);
187  }
188  );
189 
190  $job->expects($this->exactly(2))->method('activationWasToggled')->with(
191  $db,
192  $setting,
193  false
194  );
195 
196  $cronManager->deactivateJob($job, $user, true);
197  $cronManager->deactivateJob($job, $user, false);
198  }
199 }
Interface Observer Contains several chained tasks and infos about them.
__construct(Container $dic, ilPlugin $plugin)
testCronManagerNotifiesJobWhenJobGetsDeactivated()