Conditions | 1 |
Paths | 1 |
Total Lines | 88 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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'); |
||
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'); |
||
151 | $this->assertSession('abc', 'access_token'); |
||
152 | |||
153 | $developer = $this->Developers->get(3); |
||
154 | $this->assertEquals(1, $developer->has_commit_access); |
||
155 | } |
||
171 |