1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TogglJiraTest\Service; |
5
|
|
|
|
6
|
|
|
use AJT\Toggl\TogglClient; |
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use Mockery\Exception; |
9
|
|
|
use Mockery\MockInterface; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use TogglJira\Entity\WorkLogEntry; |
13
|
|
|
use TogglJira\Hydrator\WorkLogHydrator; |
14
|
|
|
use TogglJira\Jira\Api; |
15
|
|
|
use TogglJira\Service\SyncService; |
16
|
|
|
|
17
|
|
|
class SyncServiceTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var MockInterface |
21
|
|
|
*/ |
22
|
|
|
private $apiMock; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var MockInterface |
26
|
|
|
*/ |
27
|
|
|
private $togglClientMock; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var MockInterface |
31
|
|
|
*/ |
32
|
|
|
private $hydratorMock; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MockInterface |
36
|
|
|
*/ |
37
|
|
|
private $loggerMock; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var SyncService |
41
|
|
|
*/ |
42
|
|
|
private $service; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function setUp(): void |
48
|
|
|
{ |
49
|
|
|
\ Mockery::getConfiguration()->allowMockingNonExistentMethods(true); |
50
|
|
|
|
51
|
|
|
$this->apiMock = \Mockery::mock(Api::class); |
|
|
|
|
52
|
|
|
$this->togglClientMock = \Mockery::mock(TogglClient::class); |
53
|
|
|
$this->hydratorMock = \Mockery::mock(WorkLogHydrator::class); |
54
|
|
|
$this->loggerMock = \Mockery::mock(LoggerInterface::class); |
55
|
|
|
|
56
|
|
|
$this->service = new SyncService($this->apiMock, $this->togglClientMock, $this->hydratorMock, 'D-Va'); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->service->setLogger($this->loggerMock); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
|
public function testSync(): void |
66
|
|
|
{ |
67
|
|
|
$dateTime = '2017-04-15T23:35:00+02:00'; |
68
|
|
|
|
69
|
|
|
$timeEntries = [ |
70
|
|
|
[ |
71
|
|
|
'description' => 'SLR-76 Soldier 76', |
72
|
|
|
'duration' => 76, |
73
|
|
|
'comment' => 'Soldier 76, reporting for duty', |
74
|
|
|
'at' => '2018-02-15' |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
'description' => 'DVA-76 D-Va', |
78
|
|
|
'duration' => 42, |
79
|
|
|
'comment' => 'Nerf this!', |
80
|
|
|
'at' => '2018-02-15' |
81
|
|
|
], |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
$this->togglClientMock->shouldReceive('getTimeEntries') |
|
|
|
|
85
|
|
|
->with(['start_date' => $dateTime]) |
86
|
|
|
->andReturn($timeEntries); |
87
|
|
|
|
88
|
|
|
$workLogEntry = new WorkLogEntry(); |
89
|
|
|
$workLogEntry->setIssueID('SLR-76'); |
90
|
|
|
$workLogEntry->setSpentOn(new DateTimeImmutable($dateTime)); |
91
|
|
|
$workLogEntry->setComment('SLR-76'); |
92
|
|
|
$workLogEntry->setTimeSpent(76); |
93
|
|
|
|
94
|
|
|
$this->hydratorMock->shouldReceive('hydrate')->andReturn($workLogEntry); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->apiMock->shouldReceive('addWorkLogEntry')->with( |
97
|
|
|
$workLogEntry->getIssueID(), |
|
|
|
|
98
|
|
|
$workLogEntry->getTimeSpent(), |
|
|
|
|
99
|
|
|
'D-Va', |
100
|
|
|
$workLogEntry->getComment(), |
101
|
|
|
$dateTime |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$this->loggerMock |
105
|
|
|
->shouldReceive('info') |
106
|
|
|
->with("Found time entry for user story {$workLogEntry->getIssueID()}"); |
107
|
|
|
|
108
|
|
|
$this->loggerMock |
109
|
|
|
->shouldReceive('info') |
110
|
|
|
->with("Added worklog entry for issue {$workLogEntry->getIssueID()}"); |
111
|
|
|
|
112
|
|
|
$this->loggerMock->shouldReceive('info')->with('All done for today, time to go home!'); |
113
|
|
|
|
114
|
|
|
$this->service->sync($dateTime); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return void |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
|
|
public function testExceptionOnTogglError(): void |
122
|
|
|
{ |
123
|
|
|
$dateTime = '2017-04-15T23:35:00+02:00'; |
124
|
|
|
|
125
|
|
|
$this->togglClientMock->shouldReceive('getTimeEntries') |
|
|
|
|
126
|
|
|
->with(['start_date' => $dateTime]) |
127
|
|
|
->andThrow(\Exception::class, 'Nerf this!'); |
128
|
|
|
|
129
|
|
|
$this->loggerMock->shouldReceive('error') |
130
|
|
|
->with('Failed to get time entries from Toggl: Nerf this!', \Mockery::any()); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$this->service->sync($dateTime); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return void |
137
|
|
|
* @throws \Exception |
138
|
|
|
*/ |
139
|
|
|
public function testSyncWithInvalidTimeEntries(): void |
140
|
|
|
{ |
141
|
|
|
$dateTime = '2017-04-15T23:35:00+02:00'; |
142
|
|
|
|
143
|
|
|
$timeEntries = [ |
144
|
|
|
[ |
145
|
|
|
'description' => 'SLR76 Soldier 76', |
146
|
|
|
'duration' => 76, |
147
|
|
|
'comment' => 'Soldier 76, not reporting for duty', |
148
|
|
|
'at' => '2018-02-15' |
149
|
|
|
], |
150
|
|
|
[ |
151
|
|
|
'description' => 'SLR-76 Soldier 76', |
152
|
|
|
'duration' => -1, |
153
|
|
|
'comment' => 'Soldier 76, not reporting for duty', |
154
|
|
|
'at' => '2018-02-15' |
155
|
|
|
], |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$this->togglClientMock->shouldReceive('getTimeEntries') |
|
|
|
|
159
|
|
|
->with(['start_date' => $dateTime]) |
160
|
|
|
->andReturn($timeEntries); |
161
|
|
|
|
162
|
|
|
$this->loggerMock->shouldReceive('warning' |
163
|
|
|
)->with('Could not parse issue string, cannot link to Jira'); |
|
|
|
|
164
|
|
|
|
165
|
|
|
$this->loggerMock->shouldReceive('info') |
166
|
|
|
->with('0 seconds, or timer still running for SLR-76, skipping'); |
167
|
|
|
|
168
|
|
|
$this->loggerMock->shouldReceive('info')->with('All done for today, time to go home!'); |
169
|
|
|
|
170
|
|
|
$this->service->sync($dateTime); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return void |
175
|
|
|
* @throws \Exception |
176
|
|
|
*/ |
177
|
|
|
public function testSyncJiraException(): void |
178
|
|
|
{ |
179
|
|
|
$dateTime = '2017-04-15T23:35:00+02:00'; |
180
|
|
|
|
181
|
|
|
$timeEntries = [ |
182
|
|
|
[ |
183
|
|
|
'description' => 'SLR-76 Soldier 76', |
184
|
|
|
'duration' => 76, |
185
|
|
|
'comment' => 'Soldier 76, reporting for duty', |
186
|
|
|
'at' => '2018-02-15' |
187
|
|
|
], |
188
|
|
|
[ |
189
|
|
|
'description' => 'DVA-76 D-Va', |
190
|
|
|
'duration' => 42, |
191
|
|
|
'comment' => 'Nerf this!', |
192
|
|
|
'at' => '2018-02-15' |
193
|
|
|
], |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
$this->togglClientMock->shouldReceive('getTimeEntries') |
|
|
|
|
197
|
|
|
->with(['start_date' => $dateTime]) |
198
|
|
|
->andReturn($timeEntries); |
199
|
|
|
|
200
|
|
|
$workLogEntry = new WorkLogEntry(); |
201
|
|
|
$workLogEntry->setIssueID('SLR-76'); |
202
|
|
|
$workLogEntry->setSpentOn(new DateTimeImmutable($dateTime)); |
203
|
|
|
$workLogEntry->setComment('SLR-76'); |
204
|
|
|
$workLogEntry->setTimeSpent(76); |
205
|
|
|
|
206
|
|
|
$this->hydratorMock->shouldReceive('hydrate')->andReturn($workLogEntry); |
|
|
|
|
207
|
|
|
|
208
|
|
|
$this->apiMock->shouldReceive('addWorkLogEntry')->with( |
209
|
|
|
$workLogEntry->getIssueID(), |
|
|
|
|
210
|
|
|
$workLogEntry->getTimeSpent(), |
|
|
|
|
211
|
|
|
'D-Va', |
212
|
|
|
$workLogEntry->getComment(), |
213
|
|
|
$dateTime |
214
|
|
|
)->andThrow(\Exception::class, 'Nerf this!'); |
215
|
|
|
|
216
|
|
|
$this->loggerMock |
217
|
|
|
->shouldReceive('info') |
218
|
|
|
->with("Found time entry for user story {$workLogEntry->getIssueID()}"); |
219
|
|
|
|
220
|
|
|
$this->loggerMock |
221
|
|
|
->shouldReceive('error') |
222
|
|
|
->with("Could not add worklog entry: Nerf this!", \Mockery::any()); |
|
|
|
|
223
|
|
|
|
224
|
|
|
$this->loggerMock->shouldReceive('info')->with('All done for today, time to go home!'); |
225
|
|
|
|
226
|
|
|
$this->service->sync($dateTime); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function tearDown()/* The :void return type declaration that should be here would cause a BC issue */ |
230
|
|
|
{ |
231
|
|
|
if ($container = \Mockery::getContainer()) { |
232
|
|
|
$this->addToAssertionCount($container->mockery_getExpectationCount()); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |