-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScheduleAssertions.php
73 lines (62 loc) · 2.35 KB
/
ScheduleAssertions.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
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Testing\Assertions\Application;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Foundation\Application;
use LastDragon_ru\LaraASP\Testing\Assertions\Application\ScheduleMatchers\CallbackEventMatcher;
use LastDragon_ru\LaraASP\Testing\Assertions\Application\ScheduleMatchers\CommandMatcher;
use LastDragon_ru\LaraASP\Testing\Assertions\Application\ScheduleMatchers\DescriptionMatcher;
use PHPUnit\Framework\Assert;
use function array_filter;
use function count;
use function sprintf;
trait ScheduleAssertions {
// <editor-fold desc="Abstract">
// =========================================================================
abstract protected function app(): Application;
// </editor-fold>
// <editor-fold desc="Assertions">
// =========================================================================
/**
* Asserts that Schedule contains task.
*/
public function assertScheduled(string $expected, ?string $message = null): void {
$message ??= sprintf('The `%s` is not scheduled.', $expected);
$scheduled = $this->isScheduledEvent($expected);
Assert::assertTrue($scheduled, $message);
}
// </editor-fold>
// <editor-fold desc="Helpers">
// =========================================================================
/**
* @internal
*/
private function isScheduledEvent(string $task): bool {
return count($this->getScheduledEvents($task)) === 1;
}
/**
* @internal
* @return array<array-key, Event>
*/
private function getScheduledEvents(string $task): array {
$container = $this->app();
$schedule = $container->make(Schedule::class);
$matchers = [
new DescriptionMatcher(),
new CommandMatcher($container),
new CallbackEventMatcher(),
];
$events = array_filter($schedule->events(), static function (Event $event) use ($matchers, $task): bool {
$match = false;
foreach ($matchers as $matcher) {
if ($matcher->isMatch($event, $task)) {
$match = true;
break;
}
}
return $match;
});
return $events;
}
//</editor-fold>
}