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

AuthClientSpec   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 55.56 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 40
loc 72
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 13 1
A it_should_authenticate_user() 0 6 1
A it_should_throw_authentication_exception_if_api_key_is_invalid() 20 20 1
A it_should_throw_invalid_response_exception_if_invalid_json() 20 20 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
    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