1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Dev\FunctionalTest; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\TextareaField; |
10
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
11
|
|
|
use Symbiote\QueuedJobs\Controllers\QueuedJobsAdmin; |
12
|
|
|
use Symbiote\QueuedJobs\Jobs\PublishItemsJob; |
13
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Tests for the QueuedJobsAdmin ModelAdmin clas |
17
|
|
|
* |
18
|
|
|
* @coversDefaultClass \Symbiote\QueuedJobs\Controllers\QueuedJobsAdmin |
19
|
|
|
* @package queuedjobs |
20
|
|
|
* @author Robbie Averill <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class QueuedJobsAdminTest extends FunctionalTest |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
// protected static $fixture_file = 'QueuedJobsAdminTest.yml'; |
29
|
|
|
|
30
|
|
|
protected $usesDatabase = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var QueuedJobsAdmin |
34
|
|
|
*/ |
35
|
|
|
protected $admin; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get a test class, and mock the job queue for it |
39
|
|
|
* |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
|
|
protected function setUp() |
43
|
|
|
{ |
44
|
|
|
parent::setUp(); |
45
|
|
|
|
46
|
|
|
// The shutdown handler doesn't play nicely with SapphireTest's database handling |
47
|
|
|
QueuedJobService::config()->set('use_shutdown_function', false); |
48
|
|
|
|
49
|
|
|
$this->admin = new QueuedJobsAdmin(); |
50
|
|
|
$this->admin->setRequest(new HTTPRequest('GET', '/')); |
51
|
|
|
$this->admin->getRequest()->setSession($this->session()); |
52
|
|
|
|
53
|
|
|
$mockQueue = $this->createMock(QueuedJobService::class); |
54
|
|
|
$this->admin->jobQueue = $mockQueue; |
55
|
|
|
|
56
|
|
|
$this->logInWithPermission('ADMIN'); |
57
|
|
|
$this->admin->doInit(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Ensure that the JobParams field is added as a Textarea |
62
|
|
|
*/ |
63
|
|
|
public function testConstructorParamsShouldBeATextarea() |
64
|
|
|
{ |
65
|
|
|
$fields = $this->admin->getEditForm('foo', new FieldList())->Fields(); |
66
|
|
|
$this->assertInstanceOf(TextareaField::class, $fields->fieldByName('JobParams')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Ensure that when a multi-line value is entered for JobParams, it is split by new line and each value |
71
|
|
|
* passed to the constructor of the JobType that is created by the reflection in createjob() |
72
|
|
|
* |
73
|
|
|
* @covers ::createjob |
74
|
|
|
*/ |
75
|
|
|
public function testCreateJobWithConstructorParams() |
76
|
|
|
{ |
77
|
|
|
$this->admin->jobQueue |
|
|
|
|
78
|
|
|
->expects($this->once()) |
79
|
|
|
->method('queueJob') |
80
|
|
|
->with($this->callback(function ($job) { |
81
|
|
|
return $job instanceof PublishItemsJob && $job->rootID === 'foo123'; |
82
|
|
|
})); |
83
|
|
|
|
84
|
|
|
$form = $this->admin->getEditForm('foo', new FieldList()); |
85
|
|
|
$form->Fields()->fieldByName('JobParams')->setValue(implode(PHP_EOL, ['foo123', 'bar'])); |
86
|
|
|
$form->Fields()->fieldByName('JobType')->setValue(PublishItemsJob::class); |
87
|
|
|
|
88
|
|
|
$this->admin->createjob($form->getData(), $form); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @covers ::createjob |
93
|
|
|
*/ |
94
|
|
|
public function testCreateJobWithStartAfterOption() |
95
|
|
|
{ |
96
|
|
|
$startTimeAfter = DBDatetime::now(); |
97
|
|
|
|
98
|
|
|
$this->admin->jobQueue |
|
|
|
|
99
|
|
|
->expects($this->once()) |
100
|
|
|
->method('queueJob') |
101
|
|
|
->with( |
102
|
|
|
$this->callback(static function ($job) { |
103
|
|
|
return $job instanceof PublishItemsJob; |
104
|
|
|
}), |
105
|
|
|
$this->callback(static function ($givenStartAfter) use ($startTimeAfter) { |
106
|
|
|
return $givenStartAfter === $startTimeAfter->forTemplate(); |
107
|
|
|
}) |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$form = $this->admin->getEditForm('foo', new FieldList()); |
111
|
|
|
$form->Fields()->fieldByName('JobType')->setValue(PublishItemsJob::class); |
112
|
|
|
$form->Fields()->fieldByName('JobStart')->setValue($startTimeAfter); |
113
|
|
|
|
114
|
|
|
$this->admin->createjob($form->getData(), $form); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: