Failed Conditions
Push — master ( 4f9353...7eeb29 )
by Michel
02:37
created

SyncServiceTest::testExceptionOnTogglError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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);
1 ignored issue
show
Bug introduced by
TogglJira\Jira\Api::class of type string is incompatible with the type array expected by parameter $args of Mockery::mock(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->apiMock = \Mockery::mock(/** @scrutinizer ignore-type */ Api::class);
Loading history...
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
Bug introduced by
$this->hydratorMock of type Mockery\MockInterface is incompatible with the type TogglJira\Hydrator\WorkLogHydrator expected by parameter $workLogHydrator of TogglJira\Service\SyncService::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $this->service = new SyncService($this->apiMock, $this->togglClientMock, /** @scrutinizer ignore-type */ $this->hydratorMock, 'D-Va');
Loading history...
Bug introduced by
$this->apiMock of type Mockery\MockInterface is incompatible with the type TogglJira\Jira\Api expected by parameter $api of TogglJira\Service\SyncService::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $this->service = new SyncService(/** @scrutinizer ignore-type */ $this->apiMock, $this->togglClientMock, $this->hydratorMock, 'D-Va');
Loading history...
Bug introduced by
$this->togglClientMock of type Mockery\MockInterface is incompatible with the type GuzzleHttp\Command\Guzzle\GuzzleClient expected by parameter $togglClient of TogglJira\Service\SyncService::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $this->service = new SyncService($this->apiMock, /** @scrutinizer ignore-type */ $this->togglClientMock, $this->hydratorMock, 'D-Va');
Loading history...
57
58
        $this->service->setLogger($this->loggerMock);
1 ignored issue
show
Bug introduced by
$this->loggerMock of type Mockery\MockInterface is incompatible with the type Psr\Log\LoggerInterface expected by parameter $logger of TogglJira\Service\SyncService::setLogger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $this->service->setLogger(/** @scrutinizer ignore-type */ $this->loggerMock);
Loading history...
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
Bug introduced by
'getTimeEntries' of type string is incompatible with the type array expected by parameter $methodNames of Mockery\MockInterface::shouldReceive(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $this->togglClientMock->shouldReceive(/** @scrutinizer ignore-type */ 'getTimeEntries')
Loading history...
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
Bug introduced by
$workLogEntry of type TogglJira\Entity\WorkLogEntry is incompatible with the type array expected by parameter $args of Mockery\ExpectationInterface::andReturn(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        $this->hydratorMock->shouldReceive('hydrate')->andReturn(/** @scrutinizer ignore-type */ $workLogEntry);
Loading history...
Bug introduced by
$workLogEntry of type TogglJira\Entity\WorkLogEntry is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::andReturn(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        $this->hydratorMock->shouldReceive('hydrate')->andReturn(/** @scrutinizer ignore-type */ $workLogEntry);
Loading history...
95
96
        $this->apiMock->shouldReceive('addWorkLogEntry')->with(
97
            $workLogEntry->getIssueID(),
1 ignored issue
show
Bug introduced by
$workLogEntry->getIssueID() of type string is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            /** @scrutinizer ignore-type */ $workLogEntry->getIssueID(),
Loading history...
98
            $workLogEntry->getTimeSpent(),
1 ignored issue
show
Bug introduced by
$workLogEntry->getTimeSpent() of type integer is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
            /** @scrutinizer ignore-type */ $workLogEntry->getTimeSpent(),
Loading history...
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
Bug introduced by
'getTimeEntries' of type string is incompatible with the type array expected by parameter $methodNames of Mockery\MockInterface::shouldReceive(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
        $this->togglClientMock->shouldReceive(/** @scrutinizer ignore-type */ 'getTimeEntries')
Loading history...
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
Bug introduced by
'Failed to get time entr...from Toggl: Nerf this!' of type string is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
            ->with(/** @scrutinizer ignore-type */ 'Failed to get time entries from Toggl: Nerf this!', \Mockery::any());
Loading history...
Bug introduced by
Mockery::any() of type Mockery\Matcher\Any is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
            ->with('Failed to get time entries from Toggl: Nerf this!', /** @scrutinizer ignore-type */ \Mockery::any());
Loading history...
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')
1 ignored issue
show
Bug introduced by
'getTimeEntries' of type string is incompatible with the type array expected by parameter $methodNames of Mockery\MockInterface::shouldReceive(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
        $this->togglClientMock->shouldReceive(/** @scrutinizer ignore-type */ 'getTimeEntries')
Loading history...
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');
1 ignored issue
show
Bug introduced by
'Could not parse issue s...g, cannot link to Jira' of type string is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
        )->with(/** @scrutinizer ignore-type */ 'Could not parse issue string, cannot link to Jira');
Loading history...
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')
1 ignored issue
show
Bug introduced by
'getTimeEntries' of type string is incompatible with the type array expected by parameter $methodNames of Mockery\MockInterface::shouldReceive(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

196
        $this->togglClientMock->shouldReceive(/** @scrutinizer ignore-type */ 'getTimeEntries')
Loading history...
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
Bug introduced by
$workLogEntry of type TogglJira\Entity\WorkLogEntry is incompatible with the type array expected by parameter $args of Mockery\ExpectationInterface::andReturn(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

206
        $this->hydratorMock->shouldReceive('hydrate')->andReturn(/** @scrutinizer ignore-type */ $workLogEntry);
Loading history...
Bug introduced by
$workLogEntry of type TogglJira\Entity\WorkLogEntry is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::andReturn(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

206
        $this->hydratorMock->shouldReceive('hydrate')->andReturn(/** @scrutinizer ignore-type */ $workLogEntry);
Loading history...
207
208
        $this->apiMock->shouldReceive('addWorkLogEntry')->with(
209
            $workLogEntry->getIssueID(),
1 ignored issue
show
Bug introduced by
$workLogEntry->getIssueID() of type string is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
            /** @scrutinizer ignore-type */ $workLogEntry->getIssueID(),
Loading history...
210
            $workLogEntry->getTimeSpent(),
1 ignored issue
show
Bug introduced by
$workLogEntry->getTimeSpent() of type integer is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

210
            /** @scrutinizer ignore-type */ $workLogEntry->getTimeSpent(),
Loading history...
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
Bug introduced by
Mockery::any() of type Mockery\Matcher\Any is incompatible with the type array<mixed,mixed> expected by parameter $args of Mockery\Expectation::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

222
            ->with("Could not add worklog entry: Nerf this!", /** @scrutinizer ignore-type */ \Mockery::any());
Loading history...
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
}