|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
6
|
|
|
use SilverStripe\Core\Config\Config; |
|
7
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
|
8
|
|
|
use Symbiote\QueuedJobs\Jobs\CleanupJob; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Andrew Aitken-Fincham <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class CleanupJobTest extends AbstractTest |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritDoc} |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static $fixture_file = 'CleanupJobFixture.yml'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritDoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
// Have to set a fake time here to work with |
|
27
|
|
|
// the LastEdited dates in the fixture |
|
28
|
|
|
DBDatetime::set_mock_now("2002-02-03 02:02:02"); |
|
29
|
|
|
parent::setUp(); |
|
30
|
|
|
Config::modify()->set(\Symbiote\QueuedJobs\Services\QueuedJobService::class, 'use_shutdown_function', false); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritDoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function tearDown() |
|
37
|
|
|
{ |
|
38
|
|
|
parent::tearDown(); |
|
39
|
|
|
DBDatetime::clear_mock_now(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
View Code Duplication |
public function testByDays() |
|
43
|
|
|
{ |
|
44
|
|
|
$job = new CleanupJob(); |
|
45
|
|
|
Config::modify()->set(CleanupJob::class, 'cleanup_method', 'age'); |
|
46
|
|
|
Config::modify()->set(CleanupJob::class, 'cleanup_value', 30); |
|
47
|
|
|
Config::inst()->remove(CleanupJob::class, 'cleanup_statuses'); |
|
|
|
|
|
|
48
|
|
|
Config::modify()->set( |
|
49
|
|
|
CleanupJob::class, |
|
50
|
|
|
'cleanup_statuses', |
|
51
|
|
|
array('Broken', 'Complete') |
|
52
|
|
|
); |
|
53
|
|
|
$job->process(); |
|
54
|
|
|
$data = $job->getJobData(); |
|
55
|
|
|
$this->assertContains("2 jobs cleaned up.", $data->messages[0]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
public function testByNumber() |
|
59
|
|
|
{ |
|
60
|
|
|
$job = new CleanupJob(); |
|
61
|
|
|
Config::modify()->set(CleanupJob::class, 'cleanup_method', 'number'); |
|
62
|
|
|
Config::modify()->set(CleanupJob::class, 'cleanup_value', 3); |
|
63
|
|
|
Config::inst()->remove(CleanupJob::class, 'cleanup_statuses'); |
|
|
|
|
|
|
64
|
|
|
Config::modify()->set( |
|
65
|
|
|
CleanupJob::class, |
|
66
|
|
|
'cleanup_statuses', |
|
67
|
|
|
array('Broken', 'Complete') |
|
68
|
|
|
); |
|
69
|
|
|
$job->process(); |
|
70
|
|
|
$data = $job->getJobData(); |
|
71
|
|
|
$this->assertContains("2 jobs cleaned up.", $data->messages[0]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
public function testByStatus() |
|
75
|
|
|
{ |
|
76
|
|
|
$job = new CleanupJob(); |
|
77
|
|
|
Config::modify()->set(CleanupJob::class, 'cleanup_method', 'number'); |
|
78
|
|
|
Config::modify()->set(CleanupJob::class, 'cleanup_value', 3); |
|
79
|
|
|
Config::inst()->remove(CleanupJob::class, 'cleanup_statuses'); |
|
|
|
|
|
|
80
|
|
|
Config::modify()->set( |
|
81
|
|
|
CleanupJob::class, |
|
82
|
|
|
'cleanup_statuses', |
|
83
|
|
|
array('Broken', 'Complete', 'New') |
|
84
|
|
|
); |
|
85
|
|
|
$job->process(); |
|
86
|
|
|
$data = $job->getJobData(); |
|
87
|
|
|
$this->assertContains("3 jobs cleaned up.", $data->messages[0]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
View Code Duplication |
public function testNoCleanup() |
|
91
|
|
|
{ |
|
92
|
|
|
$job = new CleanupJob(); |
|
93
|
|
|
Config::modify()->set(CleanupJob::class, 'cleanup_method', 'number'); |
|
94
|
|
|
Config::modify()->set(CleanupJob::class, 'cleanup_value', 6); |
|
95
|
|
|
Config::inst()->remove(CleanupJob::class, 'cleanup_statuses'); |
|
|
|
|
|
|
96
|
|
|
Config::modify()->set( |
|
97
|
|
|
CleanupJob::class, |
|
98
|
|
|
'cleanup_statuses', |
|
99
|
|
|
array('Broken', 'Complete', 'New') |
|
100
|
|
|
); |
|
101
|
|
|
$job->process(); |
|
102
|
|
|
$data = $job->getJobData(); |
|
103
|
|
|
$this->assertContains("No jobs to clean up.", $data->messages[0]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: