-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
ErrorNotificationTest.php
165 lines (146 loc) · 5.8 KB
/
ErrorNotificationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace EthanYehuda\CronjobManager\Test\Integration;
use EthanYehuda\CronjobManager\Api\ScheduleManagementInterface;
use EthanYehuda\CronjobManager\Api\ScheduleRepositoryInterface;
use EthanYehuda\CronjobManager\Model\ErrorNotification;
use EthanYehuda\CronjobManager\Model\ErrorNotificationEmail;
use EthanYehuda\CronjobManager\Plugin\Cron\Model\ScheduleResourcePlugin;
use EthanYehuda\CronjobManager\Test\Util\FakeClock;
use EthanYehuda\CronjobManager\Test\Util\FakeJobConfig;
use Magento\Cron\Model\ConfigInterface;
use Magento\Cron\Model\Schedule;
use Magento\Cron\Observer\ProcessCronQueueObserver;
use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Event;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* @magentoAppArea crontab
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
class ErrorNotificationTest extends TestCase
{
const NOW = '2019-02-09 18:33:00';
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var ScheduleManagementInterface
*/
private $scheduleManagement;
/**
* @var ScheduleRepositoryInterface
*/
private $scheduleRepository;
/**
* @var FakeClock
*/
private $clock;
/**
* @var \Magento\Framework\App\CacheInterface
*/
private $cache;
/**
* @var ErrorNotification|\PHPUnit_Framework_MockObject_MockObject
*/
private $errorNotification;
/**
* @var ProcessCronQueueObserver
*/
private $processCronQueueObserver;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->objectManager->configure(
[
'preferences' => [
ConfigInterface::class => FakeJobConfig::class,
Clock::class => FakeClock::class,
],
]
);
$this->clock = $this->objectManager->get(Clock::class);
$this->clock->setTimestamp(strtotime(self::NOW));
$this->setUpMocks();
$this->scheduleManagement = $this->objectManager->get(ScheduleManagementInterface::class);
$this->scheduleRepository = $this->objectManager->get(ScheduleRepositoryInterface::class);
$this->cache = $this->objectManager->get(\Magento\Framework\App\CacheInterface::class);
$this->processCronQueueObserver = $this->objectManager->get(ProcessCronQueueObserver::class);
$this->cleanSchedule();
}
private function cleanSchedule(): void
{
foreach ($this->scheduleRepository->getList(new SearchCriteria())->getItems() as $schedule) {
$this->scheduleRepository->delete($schedule);
}
// Crontab cache contains last schedule generation timestamp
$this->cache->clean(['crontab']);
}
private function setUpMocks(): void
{
$dateTime = $this->createMock(DateTime::class);
$dateTime->method('gmtTimestamp')->willReturnCallback([$this->clock, 'now']);
$this->objectManager->addSharedInstance($dateTime, DateTime::class);
$this->errorNotification = $this->createMock(ErrorNotification::class);
$this->objectManager->addSharedInstance($this->errorNotification, ErrorNotification::class);
$this->objectManager->addSharedInstance($this->errorNotification, ErrorNotificationEmail::class);
}
public function testSentIfScheduleHasErrorStatusProcessedByScheduleManagement()
{
$this->markTestSkipped('Schedule management fails immediately instead of saving error status');
$this->givenCronjobThrows(new \Exception('Fake error message'), $executed);
$this->thenErrorNotificationShouldBeSentWithMessage('Fake error message');
$this->whenCronjobsAreProcessedByScheduleManagement();
}
public function testSentIfScheduleHasErrorStatusProcessedByQueue()
{
$this->givenCronjobThrows(new \Exception('Fake error message'));
$this->thenErrorNotificationShouldBeSentWithMessage('Fake error message');
$this->whenCronjobsAreProcessedByQueue();
}
private function whenCronjobsAreProcessedByScheduleManagement(): void
{
$schedule = $this->scheduleManagement->schedule(FakeJobConfig::JOB_ID, $this->clock->now());
$this->scheduleManagement->execute((int)$schedule->getId());
}
private function whenCronjobsAreProcessedByQueue(): void
{
$this->objectManager->get(\Magento\Framework\App\Console\Request::class)->setParams(
[ProcessCronQueueObserver::STANDALONE_PROCESS_STARTED => '1']
);
$this->processCronQueueObserver->execute(new \Magento\Framework\Event\Observer);
}
private function givenCronjobThrows(\Exception $exception): void
{
FakeJobConfig::setCallback(
function () use (&$executed, $exception) {
throw $exception;
}
);
}
private function thenErrorNotificationShouldBeSentWithMessage(string $expectedMessage): void
{
$this->errorNotification->expects($this->once())->method('sendFor')->with(
$this->callback(
function (Schedule $schedule) use ($expectedMessage) {
$this->assertEquals(
Schedule::STATUS_ERROR,
$schedule->getStatus(),
'Schedule with status error should be sent as notification'
);
$this->assertEquals(
$expectedMessage,
$schedule->getMessages(),
'Schedule with messages from exception should be sent as notification'
);
return true;
}
)
);
}
}