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

DevelopersControllerTest::testCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 88
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 52
nc 1
nop 0
dl 0
loc 88
rs 9.0472
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
/* vim: set expandtab sw=4 ts=4 sts=4: */
3
4
/**
5
 * Tests for Developers Controller
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
namespace App\Test\TestCase\Controller;
20
21
use App\Controller\DevelopersController;
22
use Cake\ORM\TableRegistry;
23
use Cake\TestSuite\IntegrationTestCase;
24
25
/**
26
 * App\Controller\DevelopersController Test Case
27
 */
28
class DevelopersControllerTest extends IntegrationTestCase
0 ignored issues
show
Deprecated Code introduced by
The class Cake\TestSuite\IntegrationTestCase has been deprecated: 3.7.0 Use Cake\TestSuite\IntegrationTestTrait instead ( Ignorable by Annotation )

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

28
class DevelopersControllerTest extends /** @scrutinizer ignore-deprecated */ IntegrationTestCase
Loading history...
29
{
30
    use \phpmock\phpunit\PHPMock;
31
32
    /**
33
     * Fixtures
34
     *
35
     * @var array
36
     */
37
    public $fixtures = [
38
        'app.Developers',
39
        'app.Notifications',
40
    ];
41
42
    public function setUp()
43
    {
44
        $this->Developers = TableRegistry::get('Developers');
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->Developers = /** @scrutinizer ignore-deprecated */ TableRegistry::get('Developers');

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 Developers 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
     * Test login method
49
     *
50
     * @return void
51
     */
52
    public function testLogin()
53
    {
54
        // Empty session during initiation
55
        $this->session([]);
56
57
        $this->get('developers/login');
58
        $this->assertRedirectContains('https://github.com/login/oauth/authorize');
59
        $this->assertRedirectContains('developers%2Fcallback');
60
    }
61
62
    /**
63
     * Test callback method
64
     *
65
     * @return void
66
     */
67
    public function testCallback()
68
    {
69
        // Mock functions `curl_exec` and `curl_getinfo` in GithubApiComponent
70
        // so that we don't actually hit the Github Api
71
        $curlExecMock = $this->getFunctionMock('\App\Controller\Component', 'curl_exec');
72
        $curlGetInfoMock = $this->getFunctionMock('\App\Controller\Component', 'curl_getinfo');
73
74
        $accessTokenResponse = json_encode(['access_token' => 'abc']);
75
        $emptyAccessTokenResponse = json_encode(['access_token' => null]);
76
77
        $nonSuccessUserResponse = json_encode(['message' => 'Unauthorized access']);
78
        $userResponse = file_get_contents(TESTS . 'Fixture' . DS . 'user_response.json');
79
80
        // Github unsuccessful response followed by successful response
81
        $curlExecMock->expects($this->exactly(10))->willReturnOnConsecutiveCalls(
82
            $emptyAccessTokenResponse,
83
            $emptyAccessTokenResponse,
84
            $accessTokenResponse,
85
            $nonSuccessUserResponse,
86
            $accessTokenResponse,
87
            $userResponse,
88
            null,
89
            $accessTokenResponse,
90
            $userResponse,
91
            null
92
        );
93
        $curlGetInfoMock->expects($this->exactly(5))->willReturnOnConsecutiveCalls(
94
            401,
95
            200,
96
            404,
97
            200,
98
            204
99
        );
100
101
        // Case 1.1 Test no access_token in Github response (with last_page not set in session)
102
        // So, empty the session
103
        $this->session([]);
104
105
        $this->get('developers/callback/?code=123123123');
106
        $this->assertRedirect(['controller' => '', 'action' => 'index']);
107
108
        // Case 1.2 Test no access_token in Github response (with last_page set in session)
109
        $this->session(
110
            [
111
                'last_page' => [
112
                    'controller' => 'notifications',
113
                    'action' => 'index'
114
                ],
115
            ]
116
        );
117
118
        $this->get('developers/callback/?code=123123123');
119
        $this->assertRedirect(['controller' => '', 'action' => 'index']);
120
121
        // Case 2. Non successful response code from Github
122
        $this->session(
123
            [
124
                'last_page' => [
125
                    'controller' => 'reports',
126
                    'action' => 'index'
127
                ],
128
            ]
129
        );
130
        $this->get('developers/callback/?code=123123123');
131
        $this->assertRedirect(['controller' => '', 'action' => 'index']);
132
133
134
        // Case 3. Successful response code (new user), check whether session variables are init
135
        $this->get('developers/callback/?code=123123123');
136
        $this->assertSession(3, 'Developer.id');
137
        $this->assertSession(true, 'read_only');
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $expected of Cake\TestSuite\Integrati...stCase::assertSession(). ( Ignorable by Annotation )

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

137
        $this->assertSession(/** @scrutinizer ignore-type */ true, 'read_only');
Loading history...
138
        $this->assertSession('abc', 'access_token');
139
140
        $developer = $this->Developers->get(3);
141
        $this->assertEquals('abc', $developer->access_token);
142
        $this->assertEquals('[email protected]', $developer->email);
143
144
        // Case 4. Successful response code (returning user)
145
        // check whether session variables are init
146
        $this->session(['last_page' => null]);
147
148
        $this->get('developers/callback/?code=123123123');
149
        $this->assertSession(3, 'Developer.id');
150
        $this->assertSession(false, 'read_only');
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $expected of Cake\TestSuite\Integrati...stCase::assertSession(). ( Ignorable by Annotation )

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

150
        $this->assertSession(/** @scrutinizer ignore-type */ false, 'read_only');
Loading history...
151
        $this->assertSession('abc', 'access_token');
152
153
        $developer = $this->Developers->get(3);
154
        $this->assertEquals(1, $developer->has_commit_access);
155
    }
156
157
    /**
158
     * Test logout method
159
     *
160
     * @return void
161
     */
162
    public function testLogout()
163
    {
164
        $this->session(['Developer.id' => 1]);
165
166
        $this->get('developers/logout');
167
        $this->assertSession(null, 'Developer.id');
168
        $this->assertRedirect(['controller' => '', 'action' => 'index']);
169
    }
170
}
171