1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Test\unit; |
4
|
|
|
|
5
|
|
|
use Audiens\AdobeClient\Auth; |
6
|
|
|
use Audiens\AdobeClient\Authentication\AuthStrategyInterface; |
7
|
|
|
use Test\TestCase; |
8
|
|
|
use GuzzleHttp\ClientInterface; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use GuzzleHttp\Psr7\Stream; |
11
|
|
|
use Prophecy\Argument; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AuthTest |
15
|
|
|
*/ |
16
|
|
|
class AuthTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @test |
20
|
|
|
*/ |
21
|
|
|
public function should_append_the_authorization_token_when_performing_any_request() |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
$client_id = 'sample_client_id'; |
25
|
|
|
$secret_key = 'sample_secret_key'; |
26
|
|
|
$username = 'sample_username'; |
27
|
|
|
$password = 'sample_password'; |
28
|
|
|
$token = 'a_sample_token123456789'; |
29
|
|
|
|
30
|
|
|
$dummyStream = $this->prophesize(Stream::class); |
31
|
|
|
$dummyStream->getContents("{'response:{}'}"); |
32
|
|
|
|
33
|
|
|
$dummyResponse = $this->prophesize(Response::class); |
34
|
|
|
$dummyResponse->getBody()->willReturn($dummyStream->reveal()); |
35
|
|
|
$dummyResponse->getStatusCode()->willReturn(200); |
36
|
|
|
|
37
|
|
|
$authStrategy = $this->prophesize(AuthStrategyInterface::class); |
38
|
|
|
|
39
|
|
|
$authStrategy->authenticate($client_id, $secret_key, $username, $password, Argument::any())->willReturn($token)->shouldBeCalled(); |
40
|
|
|
|
41
|
|
|
$expectedRequestOptions = |
42
|
|
|
[ |
43
|
|
|
'headers' => [ |
44
|
|
|
'Authorization' => ['Bearer ' . $token], |
45
|
|
|
] |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$client = $this->prophesize(ClientInterface::class); |
49
|
|
|
$client->request('POST', 'random_url', $expectedRequestOptions)->willReturn($dummyResponse->reveal())->shouldBeCalled(); |
50
|
|
|
|
51
|
|
|
$auth = new Auth($client_id, $secret_key, $username, $password, $client->reveal(), $authStrategy->reveal()); |
52
|
|
|
$auth->request('POST', 'random_url', []); |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $token |
59
|
|
|
* |
60
|
|
|
* @return Response |
61
|
|
|
*/ |
62
|
|
|
protected function getTokenResponse($token) |
63
|
|
|
{ |
64
|
|
|
|
65
|
|
|
$responseBody = json_encode( |
66
|
|
|
[ |
67
|
|
|
'access_token' => $token, |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return new Response(200, [], $responseBody); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|