Completed
Pull Request — master (#19)
by Stig
05:04 queued 02:56
created

CronTaskControllerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 3
c 2
b 1
f 1
lcom 0
cbo 6
dl 0
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
B testIsTaskDue() 0 32 1
B testRunTask() 0 37 1
1
<?php
2
3
class CronTaskControllerTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    public function setUp()
7
    {
8
        parent::setUp();
9
        CronTaskTest_TestCron::$times_run = 0;
10
    }
11
12
    /**
13
     * Tests CronTaskController::isTaskDue
14
     */
15
    public function testIsTaskDue()
16
    {
17
        $runner = CronTaskController::create();
18
        $task = new CronTaskTest_TestCron();
19
        $cron = Cron\CronExpression::factory($task->getSchedule());
20
21
        // Assuming first run, match the exact time (seconds are ignored)
22
        SS_Datetime::set_mock_now('2010-06-20 13:00:10');
23
        $this->assertTrue($runner->isTaskDue($task, $cron));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
25
        // Assume first run, do not match time just before or just after schedule
26
        SS_Datetime::set_mock_now('2010-06-20 13:01:10');
27
        $this->assertFalse($runner->isTaskDue($task, $cron));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
        SS_Datetime::set_mock_now('2010-06-20 12:59:50');
29
        $this->assertFalse($runner->isTaskDue($task, $cron));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31
        // Mock a run and test that subsequent runs are properly scheduled
32
        SS_Datetime::set_mock_now('2010-06-20 13:30:10');
33
        CronTaskStatus::update_status('CronTaskTest_TestCron', true);
34
35
        // Job prior to next hour mark should not run
36
        SS_Datetime::set_mock_now('2010-06-20 13:40:00');
37
        $this->assertFalse($runner->isTaskDue($task, $cron));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
39
        // Jobs just after the next hour mark should run
40
        SS_Datetime::set_mock_now('2010-06-20 14:10:00');
41
        $this->assertTrue($runner->isTaskDue($task, $cron));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43
        // Jobs somehow delayed a whole day should be run
44
        SS_Datetime::set_mock_now('2010-06-21 13:40:00');
45
        $this->assertTrue($runner->isTaskDue($task, $cron));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
    }
47
48
    /**
49
     * Test CronTaskController::runTask
50
     */
51
    public function testRunTask()
52
    {
53
        $runner = CronTaskController::create();
54
        $runner->setQuiet(true);
55
        $task = new CronTaskTest_TestCron();
56
57
        // Assuming first run, match the exact time (seconds are ignored)
58
        $this->assertEquals(0, CronTaskTest_TestCron::$times_run);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
        SS_Datetime::set_mock_now('2010-06-20 13:00:10');
60
        $runner->runTask($task);
61
        $this->assertEquals(1, CronTaskTest_TestCron::$times_run);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
63
        // Test that re-requsting the task in the same minute do not retrigger another run
64
        SS_Datetime::set_mock_now('2010-06-20 13:00:40');
65
        $runner->runTask($task);
66
        $this->assertEquals(1, CronTaskTest_TestCron::$times_run);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
        // Job prior to next hour mark should not run
69
        SS_Datetime::set_mock_now('2010-06-20 13:40:00');
70
        $runner->runTask($task);
71
        $this->assertEquals(1, CronTaskTest_TestCron::$times_run);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
73
        // Jobs just after the next hour mark should run
74
        SS_Datetime::set_mock_now('2010-06-20 14:10:00');
75
        $runner->runTask($task);
76
        $this->assertEquals(2, CronTaskTest_TestCron::$times_run);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
78
        // Jobs run on the exact next expected date should run
79
        SS_Datetime::set_mock_now('2010-06-20 15:00:00');
80
        $runner->runTask($task);
81
        $this->assertEquals(3, CronTaskTest_TestCron::$times_run);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83
        // Jobs somehow delayed a whole day should be run
84
        SS_Datetime::set_mock_now('2010-06-21 13:40:00');
85
        $runner->runTask($task);
86
        $this->assertEquals(4, CronTaskTest_TestCron::$times_run);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CronTaskControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
    }
88
}
89
90
91
class CronTaskTest_TestCron implements TestOnly, CronTask
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
92
{
93
94
    public static $times_run = 0;
95
96
    public function getSchedule()
97
    {
98
        // Use hourly schedule
99
        return '0 * * * *';
100
    }
101
102
    public function process()
103
    {
104
        ++self::$times_run;
105
    }
106
}
107