@@ -16,20 +16,16 @@ class GraphApiTest extends \PHPUnit\Framework\TestCase
16
16
*/
17
17
public function setUp ()
18
18
{
19
- $ this ->query = <<<'QL'
20
- {
21
- shop {
22
- products(first: 2) {
23
- edges {
24
- node {
25
- id
26
- handle
27
- }
28
- }
29
- }
30
- }
31
- }
32
- QL;
19
+ // Query call
20
+ $ this ->query = [
21
+ '{ shop { products(first: 1) { edges { node { handle id } } } } } ' ,
22
+ ];
23
+
24
+ // Mutation call with variables
25
+ $ this ->mutation = [
26
+ 'mutation collectionCreate($input: CollectionInput!) { collectionCreate(input: $input) { userErrors { field message } collection { id } } } ' ,
27
+ ['input ' => ['title ' => 'Test Collection ' ]],
28
+ ];
33
29
}
34
30
35
31
/**
@@ -52,7 +48,7 @@ public function itShouldReturnBaseUrl()
52
48
$ api ->setShop ('example.myshopify.com ' );
53
49
$ api ->setApiKey ('123 ' );
54
50
$ api ->setApiPassword ('abc ' );
55
- $ api ->graph ($ this ->query );
51
+ $ api ->graph ($ this ->query [ 0 ] );
56
52
57
53
$ lastRequest = $ mock ->getLastRequest ()->getUri ();
58
54
$ this ->assertEquals ('https ' , $ lastRequest ->getScheme ());
@@ -70,7 +66,7 @@ public function itShouldReturnBaseUrl()
70
66
public function itShouldThrowExceptionForMissingDomainOnQuery ()
71
67
{
72
68
$ api = new BasicShopifyAPI ();
73
- $ api ->graph ($ this ->query );
69
+ $ api ->graph ($ this ->query [ 0 ] );
74
70
}
75
71
76
72
/**
@@ -84,7 +80,7 @@ public function itShouldThrowExceptionForMissingApiPasswordOnPrivateQuery()
84
80
{
85
81
$ api = new BasicShopifyAPI (true );
86
82
$ api ->setShop ('example.myshopify.com ' );
87
- $ api ->graph ($ this ->query );
83
+ $ api ->graph ($ this ->query [ 0 ] );
88
84
}
89
85
90
86
/**
@@ -98,15 +94,15 @@ public function itShouldThrowExceptionForMissingAccessTokenOnPublicQuery()
98
94
{
99
95
$ api = new BasicShopifyAPI ();
100
96
$ api ->setShop ('example.myshopify.com ' );
101
- $ api ->graph ($ this ->query );
97
+ $ api ->graph ($ this ->query [ 0 ] );
102
98
}
103
99
104
100
/**
105
101
* @test
106
102
*
107
- * Should get Guzzle response and JSON body
103
+ * Should get Guzzle response and JSON body for success
108
104
*/
109
- public function itShouldReturnGuzzleResponseAndJsonBodyWithApiCallLimits ()
105
+ public function itShouldReturnGuzzleResponseAndJsonBodyForSuccess ()
110
106
{
111
107
$ response = new Response (
112
108
200 ,
@@ -123,23 +119,109 @@ public function itShouldReturnGuzzleResponseAndJsonBodyWithApiCallLimits()
123
119
$ api ->setAccessToken ('!@# ' );
124
120
125
121
// Fake param just to test it receives it
126
- $ request = $ api ->graph ($ this ->query );
122
+ $ request = $ api ->graph ($ this ->query [ 0 ] );
127
123
$ data = $ mock ->getLastRequest ()->getUri ()->getQuery ();
128
124
$ token_header = $ mock ->getLastRequest ()->getHeader ('X-Shopify-Access-Token ' )[0 ];
129
125
126
+ // Assert the response data
130
127
$ this ->assertEquals (true , is_object ($ request ));
131
128
$ this ->assertInstanceOf ('GuzzleHttp\Psr7\Response ' , $ request ->response );
132
129
$ this ->assertEquals (200 , $ request ->response ->getStatusCode ());
130
+ $ this ->assertEquals (false , $ request ->errors );
133
131
$ this ->assertEquals (true , is_object ($ request ->body ));
134
132
$ this ->assertEquals ('gift-card ' , $ request ->body ->shop ->products ->edges [0 ]->node ->handle );
135
133
$ this ->assertEquals ('!@# ' , $ token_header );
136
134
135
+ // Confirm limits have been updated
137
136
$ this ->assertEquals (5 , $ api ->getApiCalls ('graph ' , 'made ' ));
138
137
$ this ->assertEquals (1000 , $ api ->getApiCalls ('graph ' , 'limit ' ));
139
138
$ this ->assertEquals (1000 - 5 , $ api ->getApiCalls ('graph ' , 'left ' ));
140
139
$ this ->assertEquals (['left ' => 1000 - 5 , 'made ' => 5 , 'limit ' => 1000 , 'requestedCost ' => 5 , 'actualCost ' => 5 , 'restoreRate ' => 50 ], $ api ->getApiCalls ('graph ' ));
141
140
}
142
141
142
+ /**
143
+ * @test
144
+ *
145
+ * Should get Guzzle response and JSON body for error
146
+ */
147
+ public function itShouldReturnGuzzleResponseForError ()
148
+ {
149
+ $ response = new Response (
150
+ 200 ,
151
+ [],
152
+ file_get_contents (__DIR__ .'/fixtures/graphql/shop_products_error.json ' )
153
+ );
154
+
155
+ $ mock = new MockHandler ([$ response ]);
156
+ $ client = new Client (['handler ' => $ mock ]);
157
+
158
+ $ api = new BasicShopifyAPI ();
159
+ $ api ->setClient ($ client );
160
+ $ api ->setShop ('example.myshopify.com ' );
161
+ $ api ->setAccessToken ('!@# ' );
162
+
163
+ // Fake param just to test it receives it
164
+ $ request = $ api ->graph ($ this ->query [0 ]);
165
+ $ data = $ mock ->getLastRequest ()->getUri ()->getQuery ();
166
+ $ token_header = $ mock ->getLastRequest ()->getHeader ('X-Shopify-Access-Token ' )[0 ];
167
+
168
+ // Assert the response
169
+ $ this ->assertEquals (true , is_object ($ request ));
170
+ $ this ->assertInstanceOf ('GuzzleHttp\Psr7\Response ' , $ request ->response );
171
+ $ this ->assertEquals (200 , $ request ->response ->getStatusCode ());
172
+ $ this ->assertEquals (true , $ request ->errors );
173
+ $ this ->assertEquals (true , is_array ($ request ->body ));
174
+ $ this ->assertEquals ("Field 'productz' doesn't exist on type 'Shop' " , $ request ->body [0 ]->message );
175
+ $ this ->assertEquals ('!@# ' , $ token_header );
176
+
177
+ // Confirm limits have not been updated since there is no cost
178
+ $ this ->assertEquals (0 , $ api ->getApiCalls ('graph ' , 'made ' ));
179
+ $ this ->assertEquals (1000 , $ api ->getApiCalls ('graph ' , 'limit ' ));
180
+ $ this ->assertEquals (0 , $ api ->getApiCalls ('graph ' , 'left ' ));
181
+ $ this ->assertEquals (['left ' => 0 , 'made ' => 0 , 'limit ' => 1000 , 'restoreRate ' => 50 , 'requestedCost ' => 0 , 'actualCost ' => 0 ], $ api ->getApiCalls ('graph ' ));
182
+ }
183
+
184
+ /**
185
+ * @test
186
+ *
187
+ * Should process query with variables
188
+ */
189
+ public function itShouldProcessQueryWithVariables ()
190
+ {
191
+ $ response = new Response (
192
+ 200 ,
193
+ [],
194
+ file_get_contents (__DIR__ .'/fixtures/graphql/create_collection.json ' )
195
+ );
196
+
197
+ $ mock = new MockHandler ([$ response ]);
198
+ $ client = new Client (['handler ' => $ mock ]);
199
+
200
+ $ api = new BasicShopifyAPI ();
201
+ $ api ->setClient ($ client );
202
+ $ api ->setShop ('example.myshopify.com ' );
203
+ $ api ->setAccessToken ('!@# ' );
204
+
205
+ // Fake param just to test it receives it
206
+ $ request = $ api ->graph ($ this ->mutation [0 ], $ this ->mutation [1 ]);
207
+ $ data = $ mock ->getLastRequest ()->getUri ()->getQuery ();
208
+ $ token_header = $ mock ->getLastRequest ()->getHeader ('X-Shopify-Access-Token ' )[0 ];
209
+
210
+ // Assert the response data
211
+ $ this ->assertEquals (true , is_object ($ request ));
212
+ $ this ->assertInstanceOf ('GuzzleHttp\Psr7\Response ' , $ request ->response );
213
+ $ this ->assertEquals (200 , $ request ->response ->getStatusCode ());
214
+ $ this ->assertEquals (true , is_object ($ request ->body ));
215
+ $ this ->assertEquals ('gid://shopify/Collection/63171592234 ' , $ request ->body ->collectionCreate ->collection ->id );
216
+ $ this ->assertEquals ('!@# ' , $ token_header );
217
+
218
+ // Confirm limits have been updated
219
+ $ this ->assertEquals (11 , $ api ->getApiCalls ('graph ' , 'made ' ));
220
+ $ this ->assertEquals (1000 , $ api ->getApiCalls ('graph ' , 'limit ' ));
221
+ $ this ->assertEquals (1000 - 11 , $ api ->getApiCalls ('graph ' , 'left ' ));
222
+ $ this ->assertEquals (['left ' => 1000 - 11 , 'made ' => 11 , 'limit ' => 1000 , 'requestedCost ' => 11 , 'actualCost ' => 11 , 'restoreRate ' => 50 ], $ api ->getApiCalls ('graph ' ));
223
+ }
224
+
143
225
/**
144
226
* @test
145
227
* @expectedException Exception
0 commit comments