|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\EventDispatcher\Async; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Prophecy\Argument; |
|
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
10
|
|
|
use Psr\Container\ContainerInterface; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Shlinkio\Shlink\EventDispatcher\Async\TaskInterface; |
|
13
|
|
|
use Shlinkio\Shlink\EventDispatcher\Async\TaskRunner; |
|
14
|
|
|
use Swoole\Http\Server as HttpServer; |
|
15
|
|
|
|
|
16
|
|
|
class TaskRunnerTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var TaskRunner */ |
|
19
|
|
|
private $taskRunner; |
|
20
|
|
|
/** @var ObjectProphecy */ |
|
21
|
|
|
private $logger; |
|
22
|
|
|
/** @var ObjectProphecy */ |
|
23
|
|
|
private $container; |
|
24
|
|
|
/** @var HttpServer */ |
|
25
|
|
|
private $server; |
|
26
|
|
|
/** @var ObjectProphecy */ |
|
27
|
|
|
private $task; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->logger = $this->prophesize(LoggerInterface::class); |
|
32
|
|
|
$this->container = $this->prophesize(ContainerInterface::class); |
|
33
|
|
|
$this->task = $this->prophesize(TaskInterface::class); |
|
34
|
|
|
|
|
35
|
|
|
$this->server = $this->createMock(HttpServer::class); |
|
|
|
|
|
|
36
|
|
|
$this->server |
|
37
|
|
|
->expects($this->once()) |
|
38
|
|
|
->method('finish') |
|
39
|
|
|
->with(''); |
|
40
|
|
|
|
|
41
|
|
|
$this->taskRunner = new TaskRunner($this->logger->reveal(), $this->container->reveal()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @test */ |
|
45
|
|
|
public function warningIsLoggedWhenProvidedTaskIsInvalid(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$logWarning = $this->logger->warning('Invalid task provided to task worker: {type}. Task ignored', [ |
|
48
|
|
|
'type' => 'string', |
|
49
|
|
|
]); |
|
50
|
|
|
$logInfo = $this->logger->info(Argument::cetera()); |
|
51
|
|
|
$logError = $this->logger->error(Argument::cetera()); |
|
52
|
|
|
|
|
53
|
|
|
($this->taskRunner)($this->server, 1, 1, 'invalid_task'); |
|
54
|
|
|
|
|
55
|
|
|
$logWarning->shouldHaveBeenCalledOnce(); |
|
56
|
|
|
$logInfo->shouldNotHaveBeenCalled(); |
|
57
|
|
|
$logError->shouldNotHaveBeenCalled(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** @test */ |
|
61
|
|
|
public function properTasksAreRun(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$logWarning = $this->logger->warning(Argument::cetera()); |
|
64
|
|
|
$logInfo = $this->logger->notice('Starting work on task {taskId}: {task}', [ |
|
65
|
|
|
'taskId' => 1, |
|
66
|
|
|
'task' => 'The task', |
|
67
|
|
|
]); |
|
68
|
|
|
$logError = $this->logger->error(Argument::cetera()); |
|
69
|
|
|
$taskToString = $this->task->toString()->willReturn('The task'); |
|
70
|
|
|
$taskRun = $this->task->run($this->container->reveal())->will(function () { |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
($this->taskRunner)($this->server, 1, 1, $this->task->reveal()); |
|
74
|
|
|
|
|
75
|
|
|
$logWarning->shouldNotHaveBeenCalled(); |
|
76
|
|
|
$logInfo->shouldHaveBeenCalledOnce(); |
|
77
|
|
|
$logError->shouldNotHaveBeenCalled(); |
|
78
|
|
|
$taskToString->shouldHaveBeenCalledOnce(); |
|
79
|
|
|
$taskRun->shouldHaveBeenCalledOnce(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @test */ |
|
83
|
|
|
public function errorIsLoggedWhenTasksFail(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$e = new Exception('Error'); |
|
86
|
|
|
|
|
87
|
|
|
$logWarning = $this->logger->warning(Argument::cetera()); |
|
88
|
|
|
$logInfo = $this->logger->notice('Starting work on task {taskId}: {task}', [ |
|
89
|
|
|
'taskId' => 1, |
|
90
|
|
|
'task' => 'The task', |
|
91
|
|
|
]); |
|
92
|
|
|
$logError = $this->logger->error('Error processing task {taskId}: {e}', [ |
|
93
|
|
|
'taskId' => 1, |
|
94
|
|
|
'e' => $e, |
|
95
|
|
|
]); |
|
96
|
|
|
$taskToString = $this->task->toString()->willReturn('The task'); |
|
97
|
|
|
$taskRun = $this->task->run($this->container->reveal())->willThrow($e); |
|
98
|
|
|
|
|
99
|
|
|
($this->taskRunner)($this->server, 1, 1, $this->task->reveal()); |
|
100
|
|
|
|
|
101
|
|
|
$logWarning->shouldNotHaveBeenCalled(); |
|
102
|
|
|
$logInfo->shouldHaveBeenCalledOnce(); |
|
103
|
|
|
$logError->shouldHaveBeenCalledOnce(); |
|
104
|
|
|
$taskToString->shouldHaveBeenCalledOnce(); |
|
105
|
|
|
$taskRun->shouldHaveBeenCalledOnce(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..