Completed
Push — master ( 225222...7d085e )
by Schlaefer
05:18 queued 02:52
created

CronTest::testSimpleCronJobRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
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 testSimpleCronJobRun()
22
    {
23
        $cron = new Cron();
24
        $mock = $this->getMockBuilder('stdClass')
25
            ->setMethods(['callback'])
26
            ->getMock();
27
        $mock->expects($this->exactly(2))->method('callback');
28
        $cron->addCronJob('foo', '+1 day', [$mock, 'callback']);
29
30
        $mock = $this->getMockBuilder('stdClass')
31
            ->setMethods(['callback'])
32
            ->getMock();
33
        $mock->expects($this->exactly(3))->method('callback');
34
        $cron->addCronJob('bar', '-1 day', [$mock, 'callback']);
35
36
        $cron->execute();
37
        $cron->execute();
38
        $cron->clearHistory();
39
        $cron->execute();
40
    }
41
42
    public function testDueIsUpdatedAndPersisted()
43
    {
44
        $cron = new Cron();
45
46
        $lastRuns = ['run' => time() - 3, 'notRun' => time() + 3];
47
        Cache::write('Plugin.Cron.lastRuns', $lastRuns, 'long');
48
49
        $run = $this->getMockBuilder('stdClass')
50
            ->setMethods(['callback'])
51
            ->getMock();
52
        $run->expects($this->exactly(1))->method('callback');
53
        $newDue = '+1 day';
54
        $cron->addCronJob('run', $newDue, [$run, 'callback']);
55
56
        $notRun = $this->getMockBuilder('stdClass')
57
            ->setMethods(['callback'])
58
            ->getMock();
59
        $notRun->expects($this->never())->method('callback');
60
        $cron->addCronJob('notRun', '+1 day', [$notRun, 'callback']);
61
62
        $cron->execute();
63
64
        $result = Cache::read('Plugin.Cron.lastRuns', 'long');
65
        $this->assertEquals($lastRuns['notRun'], $result['notRun']);
66
        $this->assertWithinRange(strtotime($newDue), $result['run'], 2);
67
    }
68
}
69