Conditions | 5 |
Paths | 9 |
Total Lines | 104 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
136 | public function testAuthenticate($token, $fail, $version) |
||
137 | { |
||
138 | // Already got some credentials stored? |
||
139 | if (!is_null($token)) |
||
140 | { |
||
141 | $this->object->setToken($token); |
||
142 | $result = $this->object->authenticate(); |
||
143 | $this->assertEquals($result, $token); |
||
144 | } |
||
145 | else |
||
146 | { |
||
147 | $this->object->setOption('requestTokenURL', 'https://example.com/request_token'); |
||
148 | $this->object->setOption('authoriseURL', 'https://example.com/authorize'); |
||
149 | $this->object->setOption('accessTokenURL', 'https://example.com/access_token'); |
||
150 | |||
151 | // Request token. |
||
152 | $returnData = new \stdClass; |
||
153 | $returnData->code = 200; |
||
154 | $returnData->body = 'oauth_token=token&oauth_token_secret=secret&oauth_callback_confirmed=true'; |
||
155 | |||
156 | $this->client->expects($this->at(0)) |
||
157 | ->method('post') |
||
158 | ->with($this->object->getOption('requestTokenURL')) |
||
159 | ->willReturn($returnData); |
||
160 | |||
161 | $input = TestHelper::getValue($this->object, 'input'); |
||
162 | $input->set('oauth_verifier', null); |
||
163 | TestHelper::setValue($this->object, 'input', $input); |
||
164 | |||
165 | if (strcmp($version, '1.0a') === 0) |
||
166 | { |
||
167 | $this->object->setOption('callback', 'TEST_URL'); |
||
168 | } |
||
169 | |||
170 | $this->object->authenticate(); |
||
171 | |||
172 | $token = $this->object->getToken(); |
||
173 | $this->assertEquals($token['key'], 'token'); |
||
174 | $this->assertEquals($token['secret'], 'secret'); |
||
175 | |||
176 | // Access token. |
||
177 | $input = TestHelper::getValue($this->object, 'input'); |
||
178 | |||
179 | if (strcmp($version, '1.0a') === 0) |
||
180 | { |
||
181 | TestHelper::setValue($this->object, 'version', $version); |
||
182 | $data = array('oauth_verifier' => 'verifier', 'oauth_token' => 'token'); |
||
183 | } |
||
184 | else |
||
185 | { |
||
186 | TestHelper::setValue($this->object, 'version', $version); |
||
187 | $data = array('oauth_token' => 'token'); |
||
188 | } |
||
189 | |||
190 | TestHelper::setValue($input, 'data', $data); |
||
191 | |||
192 | // Get mock session |
||
193 | $mockSession = $this->getMock('Joomla\\Session\\SessionInterface'); |
||
194 | |||
195 | if ($fail) |
||
196 | { |
||
197 | $mockSession->expects($this->at(0)) |
||
198 | ->method('get') |
||
199 | ->with('oauth_token.key') |
||
200 | ->willReturn('bad'); |
||
201 | |||
202 | $mockSession->expects($this->at(1)) |
||
203 | ->method('get') |
||
204 | ->with('oauth_token.secret') |
||
205 | ->willReturn('session'); |
||
206 | |||
207 | $this->application->setSession($mockSession); |
||
208 | |||
209 | $this->setExpectedException('DomainException'); |
||
210 | $result = $this->object->authenticate(); |
||
211 | } |
||
212 | |||
213 | $mockSession->expects($this->at(0)) |
||
214 | ->method('get') |
||
215 | ->with('oauth_token.key') |
||
216 | ->willReturn('token'); |
||
217 | |||
218 | $mockSession->expects($this->at(1)) |
||
219 | ->method('get') |
||
220 | ->with('oauth_token.secret') |
||
221 | ->willReturn('secret'); |
||
222 | |||
223 | $this->application->setSession($mockSession); |
||
224 | |||
225 | $returnData = new \stdClass; |
||
226 | $returnData->code = 200; |
||
227 | $returnData->body = 'oauth_token=token_key&oauth_token_secret=token_secret'; |
||
228 | |||
229 | $this->client->expects($this->at(0)) |
||
230 | ->method('post') |
||
231 | ->with($this->object->getOption('accessTokenURL')) |
||
232 | ->willReturn($returnData); |
||
233 | |||
234 | $result = $this->object->authenticate(); |
||
235 | |||
236 | $this->assertEquals($result['key'], 'token_key'); |
||
237 | $this->assertEquals($result['secret'], 'token_secret'); |
||
238 | } |
||
239 | } |
||
240 | |||
341 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: