1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Cron\Test; |
14
|
|
|
|
15
|
|
|
use Cake\Cache\Cache; |
16
|
|
|
use Cron\Lib\Cron; |
17
|
|
|
use Saito\Test\SaitoTestCase; |
18
|
|
|
|
19
|
|
|
class CronTest extends SaitoTestCase |
20
|
|
|
{ |
21
|
|
|
public function testSimpleCronJobRunEmptyPersistance() |
22
|
|
|
{ |
23
|
|
|
$cron = new Cron(); |
24
|
|
|
$mock = $this->getMockBuilder('stdClass') |
25
|
|
|
->setMethods(['callback']) |
26
|
|
|
->getMock(); |
27
|
|
|
$mock->expects($this->once())->method('callback'); |
28
|
|
|
$cron->addCronJob('foo', '+1 day', [$mock, 'callback']); |
29
|
|
|
|
30
|
|
|
$cron->execute(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testDueIsReadUpdatedAndWritten() |
34
|
|
|
{ |
35
|
|
|
$cron = new Cron(); |
36
|
|
|
|
37
|
|
|
$lastRuns = ['run' => time() - 3, 'notRun' => time() + 3]; |
38
|
|
|
Cache::write('Plugin.Cron.lastRuns', $lastRuns, 'long'); |
39
|
|
|
|
40
|
|
|
$run = $this->getMockBuilder('stdClass') |
41
|
|
|
->setMethods(['callback']) |
42
|
|
|
->getMock(); |
43
|
|
|
$run->expects($this->exactly(1))->method('callback'); |
44
|
|
|
$newDue = '+1 day'; |
45
|
|
|
$cron->addCronJob('run', $newDue, [$run, 'callback']); |
46
|
|
|
|
47
|
|
|
$notRun = $this->getMockBuilder('stdClass') |
48
|
|
|
->setMethods(['callback']) |
49
|
|
|
->getMock(); |
50
|
|
|
$notRun->expects($this->never())->method('callback'); |
51
|
|
|
$cron->addCronJob('notRun', '+1 day', [$notRun, 'callback']); |
52
|
|
|
|
53
|
|
|
$cron->execute(); |
54
|
|
|
|
55
|
|
|
$result = Cache::read('Plugin.Cron.lastRuns', 'long'); |
56
|
|
|
$this->assertEquals($lastRuns['notRun'], $result['notRun']); |
57
|
|
|
$this->assertWithinRange(strtotime($newDue), $result['run'], 2); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testGc() |
61
|
|
|
{ |
62
|
|
|
$lastRuns = ['pastNotRun' => time() - 3, 'futureNotRun' => time() + 3]; |
63
|
|
|
Cache::write('Plugin.Cron.lastRuns', $lastRuns, 'long'); |
64
|
|
|
|
65
|
|
|
$cron = new Cron(); |
66
|
|
|
$cron->execute(); |
67
|
|
|
|
68
|
|
|
$result = Cache::read('Plugin.Cron.lastRuns', 'long'); |
69
|
|
|
$this->assertArrayNotHasKey('pastNotRun', $result); |
70
|
|
|
$this->assertArrayHasKey('futureNotRun', $result); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|