1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Test\TestCase\Shell; |
4
|
|
|
|
5
|
|
|
use Cake\Command\Command; |
6
|
|
|
use Cake\TestSuite\ConsoleIntegrationTestTrait; |
7
|
|
|
use Cake\TestSuite\TestCase; |
8
|
|
|
use phpmock\phpunit\PHPMock; |
9
|
|
|
|
10
|
|
|
use function file_get_contents; |
11
|
|
|
use function json_decode; |
12
|
|
|
use function json_encode; |
13
|
|
|
|
14
|
|
|
use const DS; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* App\Shell\SyncGithubIssueStatesShell Test Case |
18
|
|
|
*/ |
19
|
|
|
class SyncGithubIssueStatesShellTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
use ConsoleIntegrationTestTrait; |
22
|
|
|
use PHPMock; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Fixtures. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
public $fixtures = ['app.Reports', 'app.Developers', 'app.Notifications']; |
30
|
|
|
|
31
|
|
|
public function setUp(): void |
32
|
|
|
{ |
33
|
|
|
parent::setUp(); |
34
|
|
|
$this->useCommandRunner(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test execute method |
39
|
|
|
*/ |
40
|
|
|
public function testExecute(): void |
41
|
|
|
{ |
42
|
|
|
$Reports = $this->getTableLocator()->get('Reports'); |
43
|
|
|
// Mock functions `curl_exec` and `curl_getinfo` in GithubApiComponent |
44
|
|
|
// so that we don't actually hit the Github Api |
45
|
|
|
$curlExecMock = $this->getFunctionMock('\App\Controller\Component', 'curl_exec'); |
46
|
|
|
$curlGetInfoMock = $this->getFunctionMock('\App\Controller\Component', 'curl_getinfo'); |
47
|
|
|
|
48
|
|
|
$issueResponse = file_get_contents(TESTS . 'Fixture' . DS . 'issue_response.json'); |
49
|
|
|
$decodedResponse = json_decode($issueResponse, true); |
50
|
|
|
$decodedResponse['state'] = 'closed'; |
51
|
|
|
$issueResponseWithClosed = json_encode($decodedResponse); |
52
|
|
|
|
53
|
|
|
$curlExecMock->expects($this->exactly(3))->willReturnOnConsecutiveCalls( |
54
|
|
|
$issueResponse, |
55
|
|
|
$issueResponse, |
56
|
|
|
$issueResponseWithClosed |
57
|
|
|
); |
58
|
|
|
$curlGetInfoMock->expects($this->exactly(3))->willReturnOnConsecutiveCalls( |
59
|
|
|
200, |
60
|
|
|
200, |
61
|
|
|
200 |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
// Fetch all linked reports |
65
|
|
|
$reports = $Reports->find( |
66
|
|
|
'all', |
67
|
|
|
[ |
68
|
|
|
'conditions' => [ |
69
|
|
|
'sourceforge_bug_id IS NOT NULL', |
70
|
|
|
'NOT' => ['status' => 'resolved'], |
71
|
|
|
], |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
$this->assertEquals(3, $reports->count()); |
75
|
|
|
|
76
|
|
|
// Run the shell command |
77
|
|
|
$this->exec('sync_github_issue_states'); |
78
|
|
|
$this->assertExitCode(Command::CODE_SUCCESS); |
79
|
|
|
|
80
|
|
|
// Fetch all linked reports |
81
|
|
|
$reports = $Reports->find( |
82
|
|
|
'all', |
83
|
|
|
[ |
84
|
|
|
'conditions' => [ |
85
|
|
|
'sourceforge_bug_id IS NOT NULL', |
86
|
|
|
'NOT' => ['status' => 'resolved'], |
87
|
|
|
], |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
$this->assertEquals(2, $reports->count()); |
91
|
|
|
|
92
|
|
|
// Fetch all closed reports |
93
|
|
|
$reports = $Reports->find( |
94
|
|
|
'all', |
95
|
|
|
[ |
96
|
|
|
'conditions' => [ |
97
|
|
|
'sourceforge_bug_id IS NOT NULL', |
98
|
|
|
'status' => 'resolved', |
99
|
|
|
], |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
$this->assertEquals(1, $reports->count()); |
103
|
|
|
$report5 = $Reports->get(4); |
104
|
|
|
$this->assertEquals('resolved', $report5->status); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|