Completed
Pull Request — master (#9)
by Hugo
02:11
created

AuthClientSpec::it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 Yproximite\Api\Client\AuthClient;
10
use Yproximite\Api\Exception\AuthenticationException;
11
12
class AuthClientSpec extends ObjectBehavior
13
{
14
    const LOGIN_ENDPOINT = 'https://api.yproximite.fr/login_check';
15
16
    function it_is_initializable()
17
    {
18
        $this->shouldHaveType(AuthClient::class);
19
    }
20
21
    function let(HttpClient $httpClient, MessageFactory $messageFactory)
22
    {
23
        $this->beConstructedWith('<api key>', self::LOGIN_ENDPOINT, $httpClient, $messageFactory);
24
    }
25
26
    function it_should_throw_authentication_exception_if_api_key_is_invalid()
27
    {
28
        $this->shouldThrow(AuthenticationException::class)->during('auth');
29
        $this->isAuthenticated()->shouldReturn(false);
30
    }
31
32
    function it_should_throw_authentication_exception_if_response_is_invalid_json()
33
    {
34
        $this->shouldThrow(AuthenticationException::class)->during('auth');
35
        $this->isAuthenticated()->shouldReturn(false);
36
    }
37
38
    function it_should_auth_if_api_key_is_valid(MessageFactory $messageFactory, RequestInterface $request)
39
    {
40
        $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, ['api_key' => '<api key>'])->willReturn($request);
41
42
        $this->auth();
43
        $this->isAuthenticated()->shouldReturn(true);
44
    }
45
}
46