2
2
3
3
namespace Softonic \GraphQL \Test ;
4
4
5
+ use Exception ;
6
+ use GuzzleHttp \ClientInterface ;
7
+ use GuzzleHttp \Exception \ServerException ;
8
+ use GuzzleHttp \Exception \TransferException ;
5
9
use PHPUnit \Framework \TestCase ;
10
+ use Psr \Http \Message \RequestInterface ;
11
+ use Psr \Http \Message \ResponseInterface ;
12
+ use RuntimeException ;
6
13
use Softonic \GraphQL \Client ;
14
+ use Softonic \GraphQL \Response ;
15
+ use Softonic \GraphQL \ResponseBuilder ;
16
+ use UnexpectedValueException ;
7
17
8
18
class ClientTest extends TestCase
9
19
{
@@ -13,18 +23,18 @@ class ClientTest extends TestCase
13
23
14
24
public function setUp ()
15
25
{
16
- $ this ->httpClient = $ this ->createMock (\ GuzzleHttp \ ClientInterface::class);
17
- $ this ->mockGraphqlResponseBuilder = $ this ->createMock (\ Softonic \ GraphQL \ ResponseBuilder::class);
26
+ $ this ->httpClient = $ this ->createMock (ClientInterface::class);
27
+ $ this ->mockGraphqlResponseBuilder = $ this ->createMock (ResponseBuilder::class);
18
28
$ this ->client = new Client ($ this ->httpClient , $ this ->mockGraphqlResponseBuilder );
19
29
}
20
30
21
31
public function testSimpleQueryWhenHasNetworkErrors ()
22
32
{
23
33
$ this ->httpClient ->expects ($ this ->once ())
24
34
->method ('request ' )
25
- ->willThrowException (new \ GuzzleHttp \ Exception \ TransferException ('library error ' ));
35
+ ->willThrowException (new TransferException ('library error ' ));
26
36
27
- $ this ->expectException (\ RuntimeException::class);
37
+ $ this ->expectException (RuntimeException::class);
28
38
$ this ->expectExceptionMessage ('Network Error. ' );
29
39
30
40
$ query = $ this ->getSimpleQuery ();
@@ -35,9 +45,10 @@ public function testCanRetrievePreviousExceptionWhenSimpleQueryHasErrors()
35
45
{
36
46
$ previousException = null ;
37
47
try {
38
- $ originalException = new \ GuzzleHttp \ Exception \ ServerException (
48
+ $ originalException = new ServerException (
39
49
'Server side error ' ,
40
- $ this ->createMock (\Psr \Http \Message \RequestInterface::class)
50
+ $ this ->createMock (RequestInterface::class),
51
+ $ this ->createMock (ResponseInterface::class)
41
52
);
42
53
43
54
$ this ->httpClient ->expects ($ this ->once ())
@@ -46,7 +57,7 @@ public function testCanRetrievePreviousExceptionWhenSimpleQueryHasErrors()
46
57
47
58
$ query = $ this ->getSimpleQuery ();
48
59
$ this ->client ->query ($ query );
49
- } catch (\ Exception $ e ) {
60
+ } catch (Exception $ e ) {
50
61
$ previousException = $ e ->getPrevious ();
51
62
} finally {
52
63
$ this ->assertSame ($ originalException , $ previousException );
@@ -57,11 +68,11 @@ public function testSimpleQueryWhenInvalidJsonIsReceived()
57
68
{
58
69
$ query = $ this ->getSimpleQuery ();
59
70
60
- $ mockHttpResponse = $ this ->createMock (\ Psr \ Http \ Message \ ResponseInterface::class);
71
+ $ mockHttpResponse = $ this ->createMock (ResponseInterface::class);
61
72
$ this ->mockGraphqlResponseBuilder ->expects ($ this ->once ())
62
73
->method ('build ' )
63
74
->with ($ mockHttpResponse )
64
- ->willThrowException (new \ UnexpectedValueException ('Invalid JSON response. ' ));
75
+ ->willThrowException (new UnexpectedValueException ('Invalid JSON response. ' ));
65
76
$ this ->httpClient ->expects ($ this ->once ())
66
77
->method ('request ' )
67
78
->with (
@@ -75,16 +86,16 @@ public function testSimpleQueryWhenInvalidJsonIsReceived()
75
86
)
76
87
->willReturn ($ mockHttpResponse );
77
88
78
- $ this ->expectException (\ UnexpectedValueException::class);
89
+ $ this ->expectException (UnexpectedValueException::class);
79
90
$ this ->expectExceptionMessage ('Invalid JSON response. ' );
80
91
81
92
$ this ->client ->query ($ query );
82
93
}
83
94
84
95
public function testSimpleQuery ()
85
96
{
86
- $ mockResponse = $ this ->createMock (\ Softonic \ GraphQL \ Response::class);
87
- $ mockHttpResponse = $ this ->createMock (\ Psr \ Http \ Message \ ResponseInterface::class);
97
+ $ mockResponse = $ this ->createMock (Response::class);
98
+ $ mockHttpResponse = $ this ->createMock (ResponseInterface::class);
88
99
89
100
$ response = [
90
101
'data ' => [
@@ -114,13 +125,13 @@ public function testSimpleQuery()
114
125
->willReturn ($ mockHttpResponse );
115
126
116
127
$ response = $ this ->client ->query ($ query );
117
- $ this ->assertInstanceOf (\ Softonic \ GraphQL \ Response::class, $ response );
128
+ $ this ->assertInstanceOf (Response::class, $ response );
118
129
}
119
130
120
131
public function testQueryWithVariables ()
121
132
{
122
- $ mockResponse = $ this ->createMock (\ Softonic \ GraphQL \ Response::class);
123
- $ mockHttpResponse = $ this ->createMock (\ Psr \ Http \ Message \ ResponseInterface::class);
133
+ $ mockResponse = $ this ->createMock (Response::class);
134
+ $ mockHttpResponse = $ this ->createMock (ResponseInterface::class);
124
135
125
136
$ response = [
126
137
'data ' => [
@@ -155,7 +166,7 @@ public function testQueryWithVariables()
155
166
->willReturn ($ mockHttpResponse );
156
167
157
168
$ response = $ this ->client ->query ($ query , $ variables );
158
- $ this ->assertInstanceOf (\ Softonic \ GraphQL \ Response::class, $ response );
169
+ $ this ->assertInstanceOf (Response::class, $ response );
159
170
}
160
171
161
172
private function getSimpleQuery ()
0 commit comments