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

GraphQLClientSpec::it_should_upload_files()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 8.8571
cc 1
eloc 28
nc 1
nop 6
1
<?php
2
3
namespace spec\Yproximite\Api\Client;
4
5
use GuzzleHttp\Psr7\MultipartStream;
6
use Http\Client\HttpClient;
7
use Http\Message\MessageFactory;
8
use PhpSpec\ObjectBehavior;
9
use Prophecy\Argument;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\StreamInterface;
13
use Yproximite\Api\Client\AuthClient;
14
use Yproximite\Api\Client\GraphQLClient;
15
use Yproximite\Api\Exception\UploadEmptyFilesException;
16
17
class GraphQLClientSpec extends ObjectBehavior
18
{
19
    const GRAPHQL_ENDPOINT = 'https://graphql.yproximite.fr';
20
21
    public function it_is_initializable()
22
    {
23
        $this->shouldHaveType(GraphQLClient::class);
24
    }
25
26
    public function let(HttpClient $httpClient, AuthClient $authClient, MessageFactory $messageFactory)
27
    {
28
        $this->beConstructedWith($authClient, self::GRAPHQL_ENDPOINT, $httpClient, $messageFactory);
29
    }
30
31
    public function it_should_send_auth_request_before_graphql_request(
32
        HttpClient $httpClient,
33
        MessageFactory $messageFactory,
34
        RequestInterface $request,
35
        ResponseInterface $response,
36
        StreamInterface $stream,
37
        AuthClient $authClient
38
    ) {
39
        $messageFactory->createRequest('POST', self::GRAPHQL_ENDPOINT, Argument::type('array'), Argument::type(MultipartStream::class))->willReturn($request);
40
        $httpClient->sendRequest($request)->willReturn($response);
41
        $response->getStatusCode()->willReturn(200);
42
        $response->getBody()->willReturn($stream);
43
        $stream->__toString()->willReturn(json_encode([
44
            'me' => ['firstName' => 'Hugo', 'lastName' => 'Alliaume'],
45
        ]));
46
47
        $authClient->isAuthenticated()->willReturn(false)->shouldBeCalled();
48
        $authClient->auth()->shouldBeCalled();
49
50
        $this->query('{ me { firstName lastName } }');
51
    }
52
53
    public function it_should_not_send_auth_request_before_graphql_request_if_user_is_authenticated(
54
        HttpClient $httpClient,
55
        MessageFactory $messageFactory,
56
        RequestInterface $request,
57
        ResponseInterface $response,
58
        StreamInterface $stream,
59
        AuthClient $authClient
60
    ) {
61
        $messageFactory->createRequest('POST', self::GRAPHQL_ENDPOINT, Argument::type('array'), Argument::type(MultipartStream::class))->willReturn($request);
62
        $httpClient->sendRequest($request)->willReturn($response);
63
        $response->getStatusCode()->willReturn(200);
64
        $response->getBody()->willReturn($stream);
65
        $stream->__toString()->willReturn(json_encode([
66
            'me' => ['firstName' => 'Hugo', 'lastName' => 'Alliaume'],
67
        ]));
68
69
        $authClient->getApiToken()->willReturn('<api token>')->shouldBeCalled();
70
        $authClient->isAuthenticated()->willReturn(true)->shouldBeCalled();
71
        $authClient->auth()->shouldNotBeCalled();
72
73
        $this->query('{ me { firstName lastName } }');
74
    }
75
76
    public function it_should_handle_case_auth_response_is_invalid_json()
77
    {
78
    }
79
80
    public function it_should_send_graphql_query()
81
    {
82
    }
83
84
    public function it_should_send_graphql_mutation()
85
    {
86
    }
87
88
    public function it_should_handle_errors()
89
    {
90
    }
91
92
    public function it_should_handle_warnings()
93
    {
94
    }
95
96
    public function it_should_upload_files(
97
        HttpClient $httpClient,
98
        MessageFactory $messageFactory,
99
        RequestInterface $request,
100
        ResponseInterface $response,
101
        StreamInterface $stream,
102
        AuthClient $authClient
103
    ) {
104
        $messageFactory->createRequest('POST', self::GRAPHQL_ENDPOINT, Argument::type('array'), Argument::type(MultipartStream::class))->willReturn($request);
105
        $httpClient->sendRequest($request)->willReturn($response);
106
        $response->getStatusCode()->willReturn(200);
107
        $response->getBody()->willReturn($stream);
108
        $stream->__toString()->willReturn(json_encode([
109
            'data' => [
110
                'uploadMedias' => [
111
                    ['id' => 1, 'name' => 'Logo Yprox-1.png', 'fullpathFilename' => 'https://example.com/media/original/Logo Yprox-1.png'],
112
                    ['id' => 2, 'name' => 'GraphQL-2.png', 'fullpathFilename' => 'https://example.com/media/original/GraphQL-2.png'],
113
                ],
114
            ],
115
        ]));
116
117
        $authClient->getApiToken()->willReturn('<api token>')->shouldBeCalled();
118
        $authClient->isAuthenticated()->willReturn(true)->shouldBeCalled();
119
        $authClient->auth()->shouldNotBeCalled();
120
121
        $response = $this->upload(123, [
122
            ['path' => __DIR__.'/../fixtures/Yproximite.png', 'name' => 'Logo Yprox.png'],
123
            __DIR__.'/../fixtures/GraphQL.png',
124
        ]);
125
126
        $response->hasErrors()->shouldBe(false);
127
        $response->hasWarnings()->shouldBe(false);
128
        $response->getData()->shouldBeLike((object) [
129
            'uploadMedias' => [
130
                ['id' => 1, 'name' => 'Logo Yprox-1.png', 'fullpathFilename' => 'https://example.com/media/original/Logo Yprox-1.png'],
131
                ['id' => 2, 'name' => 'GraphQL-2.png', 'fullpathFilename' => 'https://example.com/media/original/GraphQL-2.png'],
132
            ],
133
        ]);
134
    }
135
136
    public function it_should_handle_case_when_uploading_empty_files()
137
    {
138
        $this->shouldThrow(UploadEmptyFilesException::class)->during('upload', [123]);
139
    }
140
141
    public function it_should_handle_case_when_uploading_is_failing()
142
    {
143
    }
144
}
145