1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use GuzzleHttp\Client; |
4
|
|
|
use GuzzleHttp\Subscriber\Mock; |
5
|
|
|
use GuzzleHttp\Message\Response; |
6
|
|
|
use badams\MicrosoftTranslator\MicrosoftTranslator; |
7
|
|
|
|
8
|
|
|
class AuthTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
public function testSetClient() |
11
|
|
|
{ |
12
|
|
|
$client = new Client(); |
13
|
|
|
$content = GuzzleHttp\Stream\Stream::factory('{"access_token":"valid"}'); |
14
|
|
|
$mock = new Mock([new Response(200, [], $content)]); |
15
|
|
|
$client->getEmitter()->attach($mock); |
16
|
|
|
|
17
|
|
|
$translator = new MicrosoftTranslator($client); |
18
|
|
|
|
19
|
|
|
$reflection = new ReflectionClass($translator); |
20
|
|
|
|
21
|
|
|
$translator->setClient('client_id', 'client_secret'); |
22
|
|
|
|
23
|
|
|
$clientId = $reflection->getProperty('clientId'); |
24
|
|
|
$clientId->setAccessible(true); |
25
|
|
|
$this->assertEquals('client_id', $clientId->getValue($translator)); |
26
|
|
|
|
27
|
|
|
$clientSecret = $reflection->getProperty('clientSecret'); |
28
|
|
|
$clientSecret->setAccessible(true); |
29
|
|
|
$this->assertEquals('client_secret', $clientSecret->getValue($translator)); |
30
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testInvalidClient() |
34
|
|
|
{ |
35
|
|
|
$client = new Client(); |
36
|
|
|
|
37
|
|
|
$content = GuzzleHttp\Stream\Stream::factory('{"error" : "invalid_client", "error_description" : "ACS50012: Authentication failed."}'); |
38
|
|
|
|
39
|
|
|
$mock = new Mock([ |
40
|
|
|
new Response(400, [], $content), |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$client->getEmitter()->attach($mock); |
44
|
|
|
|
45
|
|
|
$translator = new MicrosoftTranslator($client); |
46
|
|
|
$translator->setClient('client_id', 'client_secret'); |
47
|
|
|
|
48
|
|
|
$this->setExpectedException( |
49
|
|
|
'\badams\MicrosoftTranslator\Exceptions\AuthException', |
50
|
|
|
'ACS50012: Authentication failed.' |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$reflection = new ReflectionClass($translator); |
54
|
|
|
$updateAccessToken = $reflection->getMethod('updateAccessToken'); |
55
|
|
|
$updateAccessToken->setAccessible(true); |
56
|
|
|
$updateAccessToken->invoke($translator); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.