Completed
Push — master ( bc825a...1aba9a )
by William
06:03
created

SyncGithubIssueStatesShellTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 49
dl 0
loc 119
rs 10
c 2
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testMain() 0 66 1
A tearDown() 0 5 1
1
<?php
2
namespace App\Test\TestCase\Shell;
3
4
use App\Shell\SyncGithubIssueStatesShell;
5
use Cake\ORM\TableRegistry;
6
use Cake\TestSuite\TestCase;
7
8
/**
9
 * App\Shell\SyncGithubIssueStatesShell Test Case
10
 */
11
class SyncGithubIssueStatesShellTest extends TestCase
12
{
13
14
    use \phpmock\phpunit\PHPMock;
15
16
    /**
17
     * ConsoleIo mock
18
     *
19
     * @var \Cake\Console\ConsoleIo|\PHPUnit_Framework_MockObject_MockObject
20
     */
21
    public $io;
22
23
    /**
24
     * Test subject
25
     *
26
     * @var \App\Shell\SyncGithubIssueStatesShell
27
     */
28
    public $SyncGithubIssueStates;
29
30
    public $fixtures = [
31
        'app.Reports',
32
    ];
33
34
    /**
35
     * setUp method
36
     *
37
     * @return void
38
     */
39
    public function setUp()
40
    {
41
        parent::setUp();
42
        $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
43
        $this->SyncGithubIssueStates = new SyncGithubIssueStatesShell($this->io);
0 ignored issues
show
Bug introduced by
$this->io of type PHPUnit\Framework\MockObject\MockObject is incompatible with the type Cake\Console\ConsoleIo|null expected by parameter $io of App\Shell\SyncGithubIssu...tesShell::__construct(). ( Ignorable by Annotation )

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

43
        $this->SyncGithubIssueStates = new SyncGithubIssueStatesShell(/** @scrutinizer ignore-type */ $this->io);
Loading history...
44
        $this->Reports = TableRegistry::get('Reports');
0 ignored issues
show
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

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

44
        $this->Reports = /** @scrutinizer ignore-deprecated */ TableRegistry::get('Reports');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug Best Practice introduced by
The property Reports does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
    }
46
47
    /**
48
     * tearDown method
49
     *
50
     * @return void
51
     */
52
    public function tearDown()
53
    {
54
        unset($this->SyncGithubIssueStates);
55
56
        parent::tearDown();
57
    }
58
59
    /**
60
     * Test main method
61
     *
62
     * @return void
63
     */
64
    public function testMain()
65
    {
66
        // Mock functions `curl_exec` and `curl_getinfo` in GithubApiComponent
67
        // so that we don't actually hit the Github Api
68
        $curlExecMock = $this->getFunctionMock('\App\Controller\Component', 'curl_exec');
69
        $curlGetInfoMock = $this->getFunctionMock('\App\Controller\Component', 'curl_getinfo');
70
71
        $issueResponse = file_get_contents(TESTS . 'Fixture' . DS . 'issue_response.json');
72
        $decodedResponse = json_decode($issueResponse, true);
73
        $decodedResponse['state'] = 'closed';
74
        $issueResponseWithClosed = json_encode($decodedResponse);
75
76
        $curlExecMock->expects($this->exactly(3))->willReturnOnConsecutiveCalls(
77
            $issueResponse,
78
            $issueResponse,
79
            $issueResponseWithClosed
80
        );
81
        $curlGetInfoMock->expects($this->exactly(3))->willReturnOnConsecutiveCalls(
82
            200,
83
            200,
84
            200
85
        );
86
87
        // Fetch all linked reports
88
        $reports = $this->Reports->find(
89
            'all',
90
            [
91
                'conditions' => [
92
                    'sourceforge_bug_id IS NOT NULL',
93
                    'NOT' => [
94
                        'status' => 'resolved',
95
                    ]
96
                ],
97
            ]
98
        );
99
        $this->assertEquals(3, $reports->count());
100
101
        $this->SyncGithubIssueStates->main();
102
103
        // Fetch all linked reports
104
        $reports = $this->Reports->find(
105
            'all',
106
            [
107
                'conditions' => [
108
                    'sourceforge_bug_id IS NOT NULL',
109
                    'NOT' => [
110
                        'status' => 'resolved',
111
                    ]
112
                ],
113
            ]
114
        );
115
        $this->assertEquals(2, $reports->count());
116
117
        // Fetch all closed reports
118
        $reports = $this->Reports->find(
119
            'all',
120
            [
121
                'conditions' => [
122
                    'sourceforge_bug_id IS NOT NULL',
123
                    'status' => 'resolved'
124
                ],
125
            ]
126
        );
127
        $this->assertEquals(1, $reports->count());
128
        $report5 = $this->Reports->get(4);
129
        $this->assertEquals('resolved', $report5->status);
130
    }
131
}
132