1 | <?php |
||
19 | class ClientTest extends \PHPUnit_Framework_TestCase |
||
20 | { |
||
21 | /** |
||
22 | * Input object for the Client object. |
||
23 | * |
||
24 | * @var Input |
||
25 | */ |
||
26 | protected $input; |
||
27 | |||
28 | /** |
||
29 | * Options for the Client object. |
||
30 | * |
||
31 | * @var Registry |
||
32 | */ |
||
33 | protected $options; |
||
34 | |||
35 | /** |
||
36 | * Mock HTTP object. |
||
37 | * |
||
38 | * @var \Joomla\Http\Http |
||
39 | */ |
||
40 | protected $client; |
||
41 | |||
42 | /** |
||
43 | * An instance of the object to test. |
||
44 | * |
||
45 | * @var ClientInspector |
||
46 | */ |
||
47 | protected $object; |
||
48 | |||
49 | /** |
||
50 | * The application object to send HTTP headers for redirects. |
||
51 | * |
||
52 | * @var \Joomla\Application\AbstractWebApplication |
||
53 | */ |
||
54 | protected $application; |
||
55 | |||
56 | /** |
||
57 | * Sample JSON string. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $sampleString = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; |
||
62 | |||
63 | /** |
||
64 | * Sample JSON error message. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $errorString = '{"errorCode":401, "message": "Generic error"}'; |
||
69 | |||
70 | /** |
||
71 | * Sets up the fixture, for example, opens a network connection. |
||
72 | * This method is called before a test is executed. |
||
73 | * |
||
74 | * @return void |
||
75 | */ |
||
76 | protected function setUp() |
||
100 | |||
101 | /** |
||
102 | * Provides test data. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function seedAuthenticate() |
||
107 | { |
||
108 | // Token, fail and oauth version. |
||
109 | return array( |
||
110 | array(array('key' => 'valid', 'secret' => 'valid'), false, '1.0'), |
||
111 | array(null, false, '1.0'), |
||
112 | array(null, false, '1.0a'), |
||
113 | array(null, true, '1.0a') |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Tests the constructor to ensure only arrays or ArrayAccess objects are allowed |
||
119 | * |
||
120 | * @expectedException \InvalidArgumentException |
||
121 | */ |
||
122 | public function testConstructorDisallowsNonArrayObjects() |
||
126 | |||
127 | /** |
||
128 | * Tests the authenticate method |
||
129 | * |
||
130 | * @param array $token The passed token. |
||
131 | * @param boolean $fail Mark if should fail or not. |
||
132 | * @param string $version Specify oauth version 1.0 or 1.0a. |
||
133 | * |
||
134 | * @dataProvider seedAuthenticate |
||
135 | */ |
||
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 | |||
241 | /** |
||
242 | * Tests the _generateRequestToken method - failure |
||
243 | * |
||
244 | * @expectedException \DomainException |
||
245 | */ |
||
246 | public function testGenerateRequestTokenFailure() |
||
261 | |||
262 | /** |
||
263 | * Provides test data. |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public function seedOauthRequest() |
||
276 | |||
277 | /** |
||
278 | * Tests the oauthRequest method |
||
279 | * |
||
280 | * @param string $method The request method. |
||
281 | * |
||
282 | * @dataProvider seedOauthRequest |
||
283 | */ |
||
284 | public function testOauthRequest($method) |
||
328 | |||
329 | /** |
||
330 | * Tests the safeEncode |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | public function testSafeEncodeEmpty() |
||
340 | } |
||
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: