Total Complexity | 7 |
Total Lines | 216 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
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); |
||
1 ignored issue
–
show
|
|||
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'); |
||
3 ignored issues
–
show
|
|||
57 | |||
58 | $this->service->setLogger($this->loggerMock); |
||
1 ignored issue
–
show
|
|||
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') |
||
1 ignored issue
–
show
|
|||
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); |
||
2 ignored issues
–
show
|
|||
95 | |||
96 | $this->apiMock->shouldReceive('addWorkLogEntry')->with( |
||
97 | $workLogEntry->getIssueID(), |
||
1 ignored issue
–
show
|
|||
98 | $workLogEntry->getTimeSpent(), |
||
1 ignored issue
–
show
|
|||
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') |
||
1 ignored issue
–
show
|
|||
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()); |
||
2 ignored issues
–
show
|
|||
131 | |||
132 | $this->service->sync($dateTime); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return void |
||
137 | * @throws \Exception |
||
138 | */ |
||
139 | public function testSyncWithInvalidTimeEntries(): void |
||
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') |
||
1 ignored issue
–
show
|
|||
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); |
||
2 ignored issues
–
show
|
|||
207 | |||
208 | $this->apiMock->shouldReceive('addWorkLogEntry')->with( |
||
209 | $workLogEntry->getIssueID(), |
||
1 ignored issue
–
show
|
|||
210 | $workLogEntry->getTimeSpent(), |
||
1 ignored issue
–
show
|
|||
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()); |
||
1 ignored issue
–
show
|
|||
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 */ |
||
233 | } |
||
234 | } |
||
235 | } |