Completed
Push — master ( d1f22d...905ab2 )
by Daniel
13:43
created

CronTaskControllerTest::testQuietFlagOnOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\CronTask\Tests;
4
5
use Cron\CronExpression;
6
use SilverStripe\ORM\FieldType\DBDatetime;
7
use SilverStripe\CronTask\Controllers\CronTaskController;
8
use SilverStripe\CronTask\CronTaskStatus;
9
use SilverStripe\CronTask\Controllers\CronTask;
10
use SilverStripe\Dev\FunctionalTest;
11
12
/**
13
 * @package crontask
14
 */
15
class CronTaskControllerTest extends FunctionalTest
16
{
17
    /**
18
     * {@inheritDoc}
19
     * @var bool
20
     */
21
    protected $usesDatabase = true;
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    protected function setUp()
27
    {
28
        parent::setUp();
29
        CronTaskTest\TestCron::$times_run = 0;
30
    }
31
32
    /**
33
     * Tests CronTaskController::isTaskDue
34
     */
35
    public function testIsTaskDue()
36
    {
37
        $runner = CronTaskController::create();
38
        $task = new CronTaskTest\TestCron();
39
        $cron = CronExpression::factory($task->getSchedule());
40
41
        // Assuming first run, match the exact time (seconds are ignored)
42
        DBDatetime::set_mock_now('2010-06-20 13:00:10');
43
        $this->assertTrue($runner->isTaskDue($task, $cron));
44
45
        // Assume first run, do not match time just before or just after schedule
46
        DBDatetime::set_mock_now('2010-06-20 13:01:10');
47
        $this->assertFalse($runner->isTaskDue($task, $cron));
48
        DBDatetime::set_mock_now('2010-06-20 12:59:50');
49
        $this->assertFalse($runner->isTaskDue($task, $cron));
50
51
        // Mock a run and test that subsequent runs are properly scheduled
52
        DBDatetime::set_mock_now('2010-06-20 13:30:10');
53
        CronTaskStatus::update_status('SilverStripe\\CronTask\\Tests\\CronTaskTest\\TestCron', true);
54
55
        // Job prior to next hour mark should not run
56
        DBDatetime::set_mock_now('2010-06-20 13:40:00');
57
        $this->assertFalse($runner->isTaskDue($task, $cron));
58
59
        // Jobs just after the next hour mark should run
60
        DBDatetime::set_mock_now('2010-06-20 14:10:00');
61
        $this->assertTrue($runner->isTaskDue($task, $cron));
62
63
        // Jobs somehow delayed a whole day should be run
64
        DBDatetime::set_mock_now('2010-06-21 13:40:00');
65
        $this->assertTrue($runner->isTaskDue($task, $cron));
66
    }
67
68
    /**
69
     * Test CronTaskController::runTask
70
     */
71
    public function testRunTask()
72
    {
73
        $runner = CronTaskController::create();
74
        $runner->setQuiet(true);
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\CronTask\Co...kController::setQuiet() has been deprecated with message: Use setVerbosity instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
75
        $task = new CronTaskTest\TestCron();
76
77
        // Assuming first run, match the exact time (seconds are ignored)
78
        $this->assertEquals(0, CronTaskTest\TestCron::$times_run);
79
        DBDatetime::set_mock_now('2010-06-20 13:00:10');
80
        $runner->runTask($task);
81
        $this->assertEquals(1, CronTaskTest\TestCron::$times_run);
82
83
        // Test that re-requsting the task in the same minute do not retrigger another run
84
        DBDatetime::set_mock_now('2010-06-20 13:00:40');
85
        $runner->runTask($task);
86
        $this->assertEquals(1, CronTaskTest\TestCron::$times_run);
87
88
        // Job prior to next hour mark should not run
89
        DBDatetime::set_mock_now('2010-06-20 13:40:00');
90
        $runner->runTask($task);
91
        $this->assertEquals(1, CronTaskTest\TestCron::$times_run);
92
93
        // Jobs just after the next hour mark should run
94
        DBDatetime::set_mock_now('2010-06-20 14:10:00');
95
        $runner->runTask($task);
96
        $this->assertEquals(2, CronTaskTest\TestCron::$times_run);
97
98
        // Jobs run on the exact next expected date should run
99
        DBDatetime::set_mock_now('2010-06-20 15:00:00');
100
        $runner->runTask($task);
101
        $this->assertEquals(3, CronTaskTest\TestCron::$times_run);
102
103
        // Jobs somehow delayed a whole day should be run
104
        DBDatetime::set_mock_now('2010-06-21 13:40:00');
105
        $runner->runTask($task);
106
        $this->assertEquals(4, CronTaskTest\TestCron::$times_run);
107
    }
108
109
110
    // normal cron output includes the current date/time - we check for that
111
    // the exact output here could vary depending on what other modules are installed
112
    public function testDefaultQuietFlagOutput()
113
    {
114
        $this->loginWithPermission('ADMIN');
115
        $this->expectOutputRegex('#'.SS_Datetime::now()->Format('Y-m-d').'#');
116
        $this->get('dev/cron?debug=1');
117
    }
118
119
    // with the flag set we want no output
120
    public function testQuietFlagOnOutput()
121
    {
122
        $this->loginWithPermission('ADMIN');
123
        $this->expectOutputString('');
124
        $this->get('dev/cron?quiet=1');
125
    }
126
127
}
128