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

AuthClientSpec::it_should_authenticate_user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
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)
25
    {
26
        $this->beConstructedWith('<api key>', self::LOGIN_ENDPOINT, $httpClient, $messageFactory);
27
    }
28
29 View Code Duplication
    public function it_should_authenticate_user(
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...
30
        HttpClient $httpClient,
31
        MessageFactory $messageFactory,
32
        RequestInterface $tokenRequest,
33
        ResponseInterface $tokenResponse,
34
        StreamInterface $tokenStream
35
    ) {
36
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
37
        $body    = http_build_query(['api_key' => '<api key>']);
38
39
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest);
40
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
41
        $tokenResponse->getStatusCode()->willReturn(200);
42
        $tokenResponse->getBody()->willReturn($tokenStream);
43
        $tokenStream->__toString()->willReturn('{"token": "<jwt_token>"}');
44
45
        $this->auth();
46
        $this->getApiToken()->shouldReturn('<jwt_token>');
47
        $this->isAuthenticated()->shouldReturn(true);
48
    }
49
50 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...
51
        HttpClient $httpClient,
52
        MessageFactory $messageFactory,
53
        RequestInterface $tokenRequest,
54
        ResponseInterface $tokenResponse,
55
        StreamInterface $tokenStream
56
    ) {
57
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
58
        $body    = http_build_query(['api_key' => '<api key>']);
59
60
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest);
61
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
62
        $tokenResponse->getStatusCode()->willReturn(401);
63
        $tokenResponse->getBody()->willReturn($tokenStream);
64
        $tokenStream->__toString()->willReturn('{"message": "Bad credentials", "code": 401}');
65
66
        $this->shouldThrow(AuthenticationException::class)->during('auth');
67
        $this->getApiToken()->shouldBeNull();
68
        $this->isAuthenticated()->shouldReturn(false);
69
    }
70
71 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...
72
        HttpClient $httpClient,
73
        MessageFactory $messageFactory,
74
        RequestInterface $tokenRequest,
75
        ResponseInterface $tokenResponse,
76
        StreamInterface $tokenStream
77
    ) {
78
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
79
        $body    = http_build_query(['api_key' => '<api key>']);
80
81
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest);
82
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
83
        $tokenResponse->getStatusCode()->willReturn(200);
84
        $tokenResponse->getBody()->willReturn($tokenStream);
85
        $tokenStream->__toString()->willReturn('{"invalid JSON",}');
86
87
        $this->shouldThrow(InvalidResponseException::class)->during('auth');
88
        $this->getApiToken()->shouldBeNull();
89
        $this->isAuthenticated()->shouldReturn(false);
90
    }
91
}
92