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\InvalidResponseException; |
16
|
|
|
use Yproximite\Api\Exception\UploadEmptyFilesException; |
17
|
|
|
|
18
|
|
|
class GraphQLClientSpec extends ObjectBehavior |
19
|
|
|
{ |
20
|
|
|
const GRAPHQL_ENDPOINT = 'https://graphql.yproximite.fr'; |
21
|
|
|
|
22
|
|
|
public function it_is_initializable() |
23
|
|
|
{ |
24
|
|
|
$this->shouldHaveType(GraphQLClient::class); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function let(HttpClient $httpClient, AuthClient $authClient, MessageFactory $messageFactory) |
28
|
|
|
{ |
29
|
|
|
$this->beConstructedWith($authClient, self::GRAPHQL_ENDPOINT, $httpClient, $messageFactory); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function it_should_send_auth_request_before_graphql_request( |
33
|
|
|
HttpClient $httpClient, |
34
|
|
|
MessageFactory $messageFactory, |
35
|
|
|
RequestInterface $request, |
36
|
|
|
ResponseInterface $response, |
37
|
|
|
StreamInterface $stream, |
38
|
|
|
AuthClient $authClient |
39
|
|
|
) { |
40
|
|
|
$messageFactory->createRequest('POST', self::GRAPHQL_ENDPOINT, Argument::type('array'), Argument::type(MultipartStream::class))->willReturn($request); |
41
|
|
|
$httpClient->sendRequest($request)->willReturn($response); |
42
|
|
|
$response->getStatusCode()->willReturn(200); |
43
|
|
|
$response->getBody()->willReturn($stream); |
44
|
|
|
$stream->__toString()->willReturn(json_encode([ |
45
|
|
|
'me' => ['firstName' => 'Hugo'], |
46
|
|
|
])); |
47
|
|
|
|
48
|
|
|
$authClient->isAuthenticated()->willReturn(false)->shouldBeCalled(); |
49
|
|
|
$authClient->auth()->shouldBeCalled(); |
50
|
|
|
|
51
|
|
|
$this->query('{ me { firstName } }'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function it_should_not_send_auth_request_before_graphql_request_if_user_is_authenticated( |
|
|
|
|
55
|
|
|
HttpClient $httpClient, |
56
|
|
|
MessageFactory $messageFactory, |
57
|
|
|
RequestInterface $request, |
58
|
|
|
ResponseInterface $response, |
59
|
|
|
StreamInterface $stream, |
60
|
|
|
AuthClient $authClient |
61
|
|
|
) { |
62
|
|
|
$messageFactory->createRequest('POST', self::GRAPHQL_ENDPOINT, Argument::type('array'), Argument::type(MultipartStream::class))->willReturn($request); |
63
|
|
|
$httpClient->sendRequest($request)->willReturn($response); |
64
|
|
|
$response->getStatusCode()->willReturn(200); |
65
|
|
|
$response->getBody()->willReturn($stream); |
66
|
|
|
$stream->__toString()->willReturn(json_encode([ |
67
|
|
|
'me' => ['firstName' => 'Hugo'], |
68
|
|
|
])); |
69
|
|
|
|
70
|
|
|
$authClient->getApiToken()->willReturn('<api token>')->shouldBeCalled(); |
71
|
|
|
$authClient->isAuthenticated()->willReturn(true)->shouldBeCalled(); |
72
|
|
|
$authClient->auth()->shouldNotBeCalled(); |
73
|
|
|
|
74
|
|
|
$this->query('{ me { firstName } }'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function it_should_send_graphql_query() |
78
|
|
|
{ |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function it_should_send_graphql_mutation() |
82
|
|
|
{ |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function it_should_upload_files( |
86
|
|
|
HttpClient $httpClient, |
87
|
|
|
MessageFactory $messageFactory, |
88
|
|
|
RequestInterface $request, |
89
|
|
|
ResponseInterface $response, |
90
|
|
|
StreamInterface $stream, |
91
|
|
|
AuthClient $authClient |
92
|
|
|
) { |
93
|
|
|
$messageFactory->createRequest('POST', self::GRAPHQL_ENDPOINT, Argument::type('array'), Argument::type(MultipartStream::class))->willReturn($request); |
94
|
|
|
$httpClient->sendRequest($request)->willReturn($response); |
95
|
|
|
$response->getStatusCode()->willReturn(200); |
96
|
|
|
$response->getBody()->willReturn($stream); |
97
|
|
|
$stream->__toString()->willReturn(json_encode([ |
98
|
|
|
'data' => [ |
99
|
|
|
'uploadMedias' => [ |
100
|
|
|
['id' => 1, 'name' => 'Logo Yprox-1.png', 'fullpathFilename' => 'https://example.com/media/original/Logo Yprox-1.png'], |
101
|
|
|
['id' => 2, 'name' => 'GraphQL-2.png', 'fullpathFilename' => 'https://example.com/media/original/GraphQL-2.png'], |
102
|
|
|
], |
103
|
|
|
], |
104
|
|
|
])); |
105
|
|
|
|
106
|
|
|
$authClient->getApiToken()->willReturn('<api token>')->shouldBeCalled(); |
107
|
|
|
$authClient->isAuthenticated()->willReturn(true)->shouldBeCalled(); |
108
|
|
|
$authClient->auth()->shouldNotBeCalled(); |
109
|
|
|
|
110
|
|
|
$response = $this->upload(123, [ |
111
|
|
|
['path' => __DIR__.'/../fixtures/Yproximite.png', 'name' => 'Logo Yprox.png'], |
112
|
|
|
__DIR__.'/../fixtures/GraphQL.png', |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
$response->hasErrors()->shouldBe(false); |
116
|
|
|
$response->hasWarnings()->shouldBe(false); |
117
|
|
|
$response->getUploadedMedias()->shouldBeLike([ |
118
|
|
|
['id' => 1, 'name' => 'Logo Yprox-1.png', 'fullpathFilename' => 'https://example.com/media/original/Logo Yprox-1.png'], |
119
|
|
|
['id' => 2, 'name' => 'GraphQL-2.png', 'fullpathFilename' => 'https://example.com/media/original/GraphQL-2.png'], |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function it_should_handle_case_when_uploading_empty_files() |
124
|
|
|
{ |
125
|
|
|
$this->shouldThrow(UploadEmptyFilesException::class)->during('upload', [123]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function it_should_handle_errors() |
129
|
|
|
{ |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function it_should_handle_warnings() |
133
|
|
|
{ |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function it_should_handle_invalid_json( |
|
|
|
|
137
|
|
|
HttpClient $httpClient, |
138
|
|
|
MessageFactory $messageFactory, |
139
|
|
|
RequestInterface $request, |
140
|
|
|
ResponseInterface $response, |
141
|
|
|
StreamInterface $stream, |
142
|
|
|
AuthClient $authClient |
143
|
|
|
) { |
144
|
|
|
$messageFactory->createRequest('POST', self::GRAPHQL_ENDPOINT, Argument::type('array'), Argument::type(MultipartStream::class))->willReturn($request); |
145
|
|
|
$httpClient->sendRequest($request)->willReturn($response); |
146
|
|
|
$response->getStatusCode()->willReturn(200); |
147
|
|
|
$response->getBody()->willReturn($stream); |
148
|
|
|
$stream->__toString()->willReturn('invalid JSON'); |
149
|
|
|
|
150
|
|
|
$authClient->getApiToken()->willReturn('<api token>')->shouldBeCalled(); |
151
|
|
|
$authClient->isAuthenticated()->willReturn(true)->shouldBeCalled(); |
152
|
|
|
$authClient->auth()->shouldNotBeCalled(); |
153
|
|
|
|
154
|
|
|
$this->shouldThrow(InvalidResponseException::class)->during('query', ['{ me { firstName } }']); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.