Completed
Pull Request — master (#9)
by Hugo
01:44
created

AuthClientSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 5
1
<?php
2
3
namespace spec\Yproximite\Api\Client;
4
5
use Http\Client\HttpClient;
6
use Http\Message\MessageFactory;
7
use PhpSpec\ObjectBehavior;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\StreamInterface;
11
use Yproximite\Api\Client\AuthClient;
12
use Yproximite\Api\Exception\AuthenticationException;
13
use Yproximite\Api\Exception\InvalidResponseException;
14
15
class AuthClientSpec extends ObjectBehavior
16
{
17
    const LOGIN_ENDPOINT = 'https://api.yproximite.fr/login_check';
18
19
    public function it_is_initializable()
20
    {
21
        $this->shouldHaveType(AuthClient::class);
22
    }
23
24
    public function let(HttpClient $httpClient, MessageFactory $messageFactory, RequestInterface $tokenRequest, ResponseInterface $tokenResponse, StreamInterface $tokenStream)
25
    {
26
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
27
        $body    = http_build_query(['api_key' => '<api key>']);
28
29
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest);
30
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
31
        $tokenResponse->getStatusCode()->willReturn(200);
32
        $tokenResponse->getBody()->willReturn($tokenStream);
33
        $tokenStream->__toString()->willReturn('{"token": "<jwt_token>"}');
34
35
        $this->beConstructedWith('<api key>', self::LOGIN_ENDPOINT, $httpClient, $messageFactory);
36
    }
37
38
    public function it_should_authenticate_user()
39
    {
40
        $this->auth();
41
        $this->getApiToken()->shouldReturn('<jwt_token>');
42
        $this->isAuthenticated()->shouldReturn(true);
43
    }
44
45 View Code Duplication
    public function it_should_throw_authentication_exception_if_api_key_is_invalid(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
        HttpClient $httpClient,
47
        MessageFactory $messageFactory,
48
        RequestInterface $tokenRequest,
49
        ResponseInterface $tokenResponse,
50
        StreamInterface $tokenStream
51
    ) {
52
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
53
        $body    = http_build_query(['api_key' => '<api key>']);
54
55
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest);
56
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
57
        $tokenResponse->getStatusCode()->willReturn(401);
58
        $tokenResponse->getBody()->willReturn($tokenStream);
59
        $tokenStream->__toString()->willReturn('{"message": "Invalid Credentials", "code": 401}');
60
61
        $this->shouldThrow(AuthenticationException::class)->during('auth');
62
        $this->getApiToken()->shouldBeNull();
63
        $this->isAuthenticated()->shouldReturn(false);
64
    }
65
66 View Code Duplication
    public function it_should_throw_invalid_response_exception_if_invalid_json(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
        HttpClient $httpClient,
68
        MessageFactory $messageFactory,
69
        RequestInterface $tokenRequest,
70
        ResponseInterface $tokenResponse,
71
        StreamInterface $tokenStream
72
    ) {
73
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
74
        $body    = http_build_query(['api_key' => '<api key>']);
75
76
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest);
77
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
78
        $tokenResponse->getStatusCode()->willReturn(200);
79
        $tokenResponse->getBody()->willReturn($tokenStream);
80
        $tokenStream->__toString()->willReturn('{"invalid JSON",}');
81
82
        $this->shouldThrow(InvalidResponseException::class)->during('auth');
83
        $this->getApiToken()->shouldBeNull();
84
        $this->isAuthenticated()->shouldReturn(false);
85
    }
86
}
87