1
+ <?php
2
+ namespace SmartEmailing \v3 \Tests ;
3
+
4
+ use Dotenv \Dotenv ;
5
+ use GuzzleHttp \Client ;
6
+ use GuzzleHttp \Exception \BadResponseException ;
7
+ use GuzzleHttp \Handler \MockHandler ;
8
+ use GuzzleHttp \Psr7 \Request ;
9
+ use GuzzleHttp \Psr7 \Response ;
10
+ use PHPUnit \Framework \TestCase ;
11
+ use Psr \Http \Message \ResponseInterface ;
12
+ use SmartEmailing \v3 \Api ;
13
+ use SmartEmailing \v3 \Exceptions \RequestException ;
14
+ use SmartEmailing \v3 \Request \AbstractRequest ;
15
+ use SmartEmailing \v3 \Request \Response as InternalResponse ;
16
+
17
+ class BaseTestCase extends TestCase
18
+ {
19
+ protected $ username ;
20
+ protected $ apiKey ;
21
+
22
+ /**
23
+ * Constructs a test case with the given name. Setups default api-key/username
24
+ *
25
+ * @param string $name
26
+ * @param array $data
27
+ * @param string $dataName
28
+ */
29
+ public function __construct ($ name = null , array $ data = [], $ dataName = '' )
30
+ {
31
+ parent ::__construct ($ name , $ data , $ dataName );
32
+
33
+ // Load the Env variables
34
+ $ dotEnv = new Dotenv (__DIR__ .'/../ ' );
35
+ $ dotEnv ->load ();
36
+
37
+ // Setup the username/api-key
38
+ $ this ->username = $ this ->env ('USERNAME ' , 'username ' );
39
+ $ this ->apiKey = $ this ->env ('API_KEY ' , 'password ' );
40
+ }
41
+
42
+ /**
43
+ * Creates a tests for send request that will check if correct parameters are send to clients request method
44
+ *
45
+ * @param Api|\PHPUnit_Framework_MockObject_MockObject $apiStub
46
+ * @param AbstractRequest $request
47
+ * @param string $endpointName
48
+ * @param string $httpMethod
49
+ * @param array|null|object $options
50
+ */
51
+ protected function createEndpointTest ($ apiStub , $ request , $ endpointName , $ httpMethod = 'GET ' ,
52
+ $ options = [])
53
+ {
54
+ // Build the client that will mock the client->request method
55
+ $ client = $ this ->createMock (Client::class);
56
+ $ response = $ this ->createMock (ResponseInterface::class);
57
+
58
+ // Make a response that is valid and ok - prevent exception
59
+ $ response ->expects ($ this ->once ())->method ('getBody ' )->willReturn ('{
60
+ "status": "ok",
61
+ "meta": [
62
+ ],
63
+ "message": "Hi there! API version 3 here!"
64
+ } ' );
65
+
66
+ // Build customizable options to check
67
+ $ optionsCheck = null ;
68
+
69
+ if (is_null ($ options )) {
70
+ $ optionsCheck = $ this ->anything ();
71
+ } else if (is_object ($ options )) {
72
+ $ optionsCheck = $ options ;
73
+ } else {
74
+ $ optionsCheck = $ this ->equalTo ($ options );
75
+ }
76
+
77
+ // The send method will trigger the request once with given properties (request methods)
78
+ $ client ->expects ($ this ->once ())->method ('request ' )->with (
79
+ $ this ->equalTo ($ httpMethod ), $ this ->equalTo ($ endpointName ), $ optionsCheck
80
+
81
+ )->willReturn ($ response );
82
+
83
+ $ apiStub ->method ('client ' )->willReturn ($ client );
84
+ $ request ->send ();
85
+ }
86
+
87
+ /**
88
+ * Creates a response mock and runs the send method. Then checks for the response result.
89
+ *
90
+ * @param Api|\PHPUnit_Framework_MockObject_MockObject $apiStub
91
+ * @param AbstractRequest $request
92
+ * @param string $responseText
93
+ * @param string $responseMessage
94
+ * @param string $responseStatus
95
+ * @param array $meta
96
+ * @param string $responseClass
97
+ * @param int $responseCode
98
+ *
99
+ * @return InternalResponse
100
+ */
101
+ public function createSendResponse ($ apiStub , $ request , $ responseText , $ responseMessage ,
102
+ $ responseStatus = InternalResponse::SUCCESS , array $ meta = [],
103
+ $ responseClass = InternalResponse::class, $ responseCode = 200 )
104
+ {
105
+
106
+ $ this ->createMockHandlerToApi ($ apiStub , $ responseText , $ responseCode );
107
+
108
+ // Run the request
109
+ $ response = $ request ->send ();
110
+
111
+ $ this ->assertResponse ($ response , $ responseClass , $ responseStatus , $ responseMessage , $ meta );
112
+ return $ response ;
113
+ }
114
+
115
+ /**
116
+ * Creates a response mock and runs the send method. Then checks for the response result.
117
+ *
118
+ * @param Api|\PHPUnit_Framework_MockObject_MockObject $apiStub
119
+ * @param AbstractRequest $request
120
+ * @param string $responseText
121
+ * @param string $responseMessage
122
+ * @param string $responseStatus
123
+ * @param array $meta
124
+ * @param string $responseClass
125
+ * @param int $responseCode
126
+ *
127
+ * @return RequestException
128
+ */
129
+ public function createSendErrorResponse ($ apiStub , $ request , $ responseText , $ responseMessage ,
130
+ $ responseStatus = InternalResponse::SUCCESS , array $ meta = [],
131
+ $ responseClass = InternalResponse::class, $ responseCode = 200 )
132
+ {
133
+
134
+ $ this ->createMockHandlerToApi ($ apiStub , $ responseText , $ responseCode );
135
+
136
+ try {
137
+ // Run the request
138
+ $ response = $ request ->send ();
139
+
140
+ $ this ->fail ('The send request should raise an exception when Guzzle raises RequestException
141
+ (non 200 status code) or API returns 200 status code with error status in json ' );
142
+ } catch (RequestException $ exception ) {
143
+ $ this ->assertResponse ($ exception ->response (), $ responseClass , $ responseStatus , $ responseMessage , $ meta );
144
+ return $ exception ;
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Creates a MockHandler with a response and mocks the client in mocked api
150
+ *
151
+ * @param Api|\PHPUnit_Framework_MockObject_MockObject $apiStub
152
+ * @param string $responseText
153
+ * @param int $responseCode
154
+ */
155
+ protected function createMockHandlerToApi ($ apiStub , $ responseText , $ responseCode )
156
+ {
157
+ $ responseQueue = [];
158
+ if ($ responseCode > 300 ) {
159
+ $ responseQueue [] = new BadResponseException (
160
+ 'Client error ' , new Request ('GET ' , 'test ' ), new Response ($ responseCode , [], $ responseText )
161
+ );
162
+ } else {
163
+ $ responseQueue [] = new Response ($ responseCode , [], $ responseText );
164
+ }
165
+
166
+ // Return own responses
167
+ $ handler = new MockHandler ($ responseQueue );
168
+
169
+ // Build the client
170
+ $ client = new Client (['handler ' => $ handler ]);
171
+
172
+ // Replace the client
173
+ $ apiStub ->method ('client ' )->willReturn ($ client );
174
+ }
175
+
176
+ /**
177
+ * @param InternalResponse $response
178
+ * @param string $responseClass
179
+ * @param string $responseStatus
180
+ * @param string $responseMessage
181
+ * @param array $meta
182
+ */
183
+ protected function assertResponse ($ response , $ responseClass , $ responseStatus , $ responseMessage , array $ meta )
184
+ {
185
+ // Check the response
186
+ $ this ->assertInstanceOf ($ responseClass , $ response );
187
+ $ this ->assertEquals ($ responseStatus , $ response ->status ());
188
+ $ this ->assertEquals ($ responseMessage , $ response ->message ());
189
+ $ this ->assertEquals ($ meta , $ response ->meta ());
190
+ }
191
+
192
+ /**
193
+ * Creates new API
194
+ *
195
+ * @param string|null $apiUrl
196
+ *
197
+ * @return Api
198
+ */
199
+ protected function createApi ($ apiUrl = null )
200
+ {
201
+ return new Api ($ this ->username , $ this ->apiKey , $ apiUrl );
202
+ }
203
+
204
+ /**
205
+ * Gets the value of an environment variable.
206
+ *
207
+ * @param string $key
208
+ * @param mixed $default
209
+ *
210
+ * @return mixed
211
+ */
212
+ function env ($ key , $ default = null )
213
+ {
214
+ $ value = getenv ($ key );
215
+
216
+ if ($ value === false ) {
217
+ return $ default ;
218
+ }
219
+
220
+ switch (strtolower ($ value )) {
221
+ case 'true ' :
222
+ case '(true) ' :
223
+ return true ;
224
+ case 'false ' :
225
+ case '(false) ' :
226
+ return false ;
227
+ case 'empty ' :
228
+ case '(empty) ' :
229
+ return '' ;
230
+ case 'null ' :
231
+ case '(null) ' :
232
+ return null ;
233
+ }
234
+ return $ value ;
235
+ }
236
+ }
0 commit comments