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

AuthClientSpec   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 52.46 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 32
loc 61
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A it_is_initializable() 0 4 1
A let() 0 10 1
A it_should_authenticate_user() 0 6 1
A it_should_throw_authentication_exception_if_api_key_is_invalid() 16 16 1
A it_should_throw_invalid_response_exception_if_invalid_json() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    function it_is_initializable()
20
    {
21
        $this->shouldHaveType(AuthClient::class);
22
    }
23
24
    function let(HttpClient $httpClient, MessageFactory $messageFactory, RequestInterface $tokenRequest, ResponseInterface $tokenResponse, StreamInterface $tokenStream)
25
    {
26
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, [], http_build_query(['api_key' => '<api key>']))->willReturn($tokenRequest);
27
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
28
        $tokenResponse->getStatusCode()->willReturn(200);
29
        $tokenResponse->getBody()->willReturn($tokenStream);
30
        $tokenStream->__toString()->willReturn('{"token": "<jwt_token>"}');
31
32
        $this->beConstructedWith('<api key>', self::LOGIN_ENDPOINT, $httpClient, $messageFactory);
33
    }
34
35
    function it_should_authenticate_user()
36
    {
37
        $this->auth();
38
        $this->isAuthenticated()->shouldReturn(true);
39
        $this->getApiToken()->shouldReturn('<jwt_token>');
40
    }
41
42 View Code Duplication
    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...
43
        HttpClient $httpClient,
44
        MessageFactory $messageFactory,
45
        RequestInterface $tokenRequest,
46
        ResponseInterface $tokenResponse,
47
        StreamInterface $tokenStream
48
    ) {
49
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, [], http_build_query(['api_key' => '<api key>']))->willReturn($tokenRequest);
50
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
51
        $tokenResponse->getStatusCode()->willReturn(401);
52
        $tokenResponse->getBody()->willReturn($tokenStream);
53
        $tokenStream->__toString()->willReturn('{"message": "Invalid Credentials", "code": 401}');
54
55
        $this->shouldThrow(AuthenticationException::class)->during('auth');
56
        $this->isAuthenticated()->shouldReturn(false);
57
    }
58
59 View Code Duplication
    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...
60
        HttpClient $httpClient,
61
        MessageFactory $messageFactory,
62
        RequestInterface $tokenRequest,
63
        ResponseInterface $tokenResponse,
64
        StreamInterface $tokenStream
65
    ) {
66
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, [], http_build_query(['api_key' => '<api key>']))->willReturn($tokenRequest);
67
        $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse);
68
        $tokenResponse->getStatusCode()->willReturn(200);
69
        $tokenResponse->getBody()->willReturn($tokenStream);
70
        $tokenStream->__toString()->willReturn('{"invalid JSON",}');
71
72
        $this->shouldThrow(InvalidResponseException::class)->during('auth');
73
        $this->isAuthenticated()->shouldReturn(false);
74
    }
75
}
76