/
SyncGithubIssueStatesShellTest.php
137 lines (121 loc) · 3.64 KB
/
SyncGithubIssueStatesShellTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace App\Test\TestCase\Shell;
use App\Shell\SyncGithubIssueStatesShell;
use Cake\Console\ConsoleIo;
use Cake\Http\BaseApplication;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use phpmock\phpunit\PHPMock;
use PHPUnit_Framework_MockObject_MockObject;
use const DS;
use function file_get_contents;
use function json_decode;
use function json_encode;
/**
* App\Shell\SyncGithubIssueStatesShell Test Case
*/
class SyncGithubIssueStatesShellTest extends TestCase
{
use PHPMock;
/**
* ConsoleIo mock
*
* @var ConsoleIo|PHPUnit_Framework_MockObject_MockObject
*/
public $io;
/**
* Test subject
*
* @var SyncGithubIssueStatesShell
*/
public $SyncGithubIssueStates;
/**
* Fixtures.
*
* @var array
*/
public $fixtures = ['app.Reports'];
/**
* setUp method
*/
public function setUp(): void
{
parent::setUp();
$this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
/** @var BaseApplication $app */
$app = $this->getMockForAbstractClass(
BaseApplication::class,
[__DIR__ . '/../../../config']
);
$this->SyncGithubIssueStates = new SyncGithubIssueStatesShell($app);
$this->Reports = TableRegistry::getTableLocator()->get('Reports');
}
/**
* tearDown method
*/
public function tearDown(): void
{
unset($this->SyncGithubIssueStates);
parent::tearDown();
}
/**
* Test main method
*/
public function testMain(): void
{
// Mock functions `curl_exec` and `curl_getinfo` in GithubApiComponent
// so that we don't actually hit the Github Api
$curlExecMock = $this->getFunctionMock('\App\Controller\Component', 'curl_exec');
$curlGetInfoMock = $this->getFunctionMock('\App\Controller\Component', 'curl_getinfo');
$issueResponse = file_get_contents(TESTS . 'Fixture' . DS . 'issue_response.json');
$decodedResponse = json_decode($issueResponse, true);
$decodedResponse['state'] = 'closed';
$issueResponseWithClosed = json_encode($decodedResponse);
$curlExecMock->expects($this->exactly(3))->willReturnOnConsecutiveCalls(
$issueResponse,
$issueResponse,
$issueResponseWithClosed
);
$curlGetInfoMock->expects($this->exactly(3))->willReturnOnConsecutiveCalls(
200,
200,
200
);
// Fetch all linked reports
$reports = $this->Reports->find(
'all',
[
'conditions' => [
'sourceforge_bug_id IS NOT NULL',
'NOT' => ['status' => 'resolved'],
],
]
);
$this->assertEquals(3, $reports->count());
$this->SyncGithubIssueStates->main();
// Fetch all linked reports
$reports = $this->Reports->find(
'all',
[
'conditions' => [
'sourceforge_bug_id IS NOT NULL',
'NOT' => ['status' => 'resolved'],
],
]
);
$this->assertEquals(2, $reports->count());
// Fetch all closed reports
$reports = $this->Reports->find(
'all',
[
'conditions' => [
'sourceforge_bug_id IS NOT NULL',
'status' => 'resolved',
],
]
);
$this->assertEquals(1, $reports->count());
$report5 = $this->Reports->get(4);
$this->assertEquals('resolved', $report5->status);
}
}