Passed
Push — master ( 083ff7...c84473 )
by William
03:02
created

SyncGithubIssueStatesShellTest::testExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 37
nc 1
nop 0
dl 0
loc 65
rs 9.328
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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