6
6
7
7
use AppInsightsPHP \Client \Client ;
8
8
use AppInsightsPHP \Client \Configuration ;
9
+ use AppInsightsPHP \Client \FailureCache ;
10
+ use ApplicationInsights \Channel \Contracts \Envelope ;
9
11
use ApplicationInsights \Channel \Contracts \Request_Data ;
10
12
use ApplicationInsights \Channel \Telemetry_Channel ;
11
13
use ApplicationInsights \Telemetry_Client ;
12
14
use PHPUnit \Framework \MockObject \MockObject ;
13
15
use PHPUnit \Framework \TestCase ;
14
16
use Psr \Log \LoggerInterface ;
17
+ use Psr \Log \NullLogger ;
15
18
use Psr \SimpleCache \CacheInterface ;
16
19
17
20
final class ClientTest extends TestCase
18
21
{
19
22
public function test_tracking_when_client_is_disabled ()
20
23
{
21
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
24
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
22
25
23
26
$ client = new Client (
24
27
$ telemetryMock ,
25
- Configuration::createDefault ()
28
+ Configuration::createDefault (),
29
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
30
+ new NullLogger ()
26
31
);
27
32
28
33
$ client ->configuration ()->disable ();
@@ -49,11 +54,13 @@ public function test_tracking_when_client_is_disabled()
49
54
50
55
public function test_tracking_request_when_option_is_enabled ()
51
56
{
52
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
57
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
53
58
54
59
$ client = new Client (
55
60
$ telemetryMock ,
56
- Configuration::createDefault ()
61
+ Configuration::createDefault (),
62
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
63
+ new NullLogger ()
57
64
);
58
65
59
66
$ telemetryMock ->expects ($ this ->once ())
@@ -65,11 +72,13 @@ public function test_tracking_request_when_option_is_enabled()
65
72
66
73
public function test_tracking_request_when_option_is_disabled ()
67
74
{
68
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
75
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
69
76
70
77
$ client = new Client (
71
78
$ telemetryMock ,
72
- Configuration::createDefault ()
79
+ Configuration::createDefault (),
80
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
81
+ new NullLogger ()
73
82
);
74
83
75
84
$ client ->configuration ()->requests ()->disable ();
@@ -88,11 +97,13 @@ public function test_tracking_request_when_option_is_disabled()
88
97
89
98
public function test_tracking_dependencies_when_option_is_enabled ()
90
99
{
91
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
100
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
92
101
93
102
$ client = new Client (
94
103
$ telemetryMock ,
95
- Configuration::createDefault ()
104
+ Configuration::createDefault (),
105
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
106
+ new NullLogger ()
96
107
);
97
108
98
109
$ telemetryMock ->expects ($ this ->once ())
@@ -104,11 +115,13 @@ public function test_tracking_dependencies_when_option_is_enabled()
104
115
105
116
public function test_tracking_dependencies_when_option_is_disabled ()
106
117
{
107
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
118
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
108
119
109
120
$ client = new Client (
110
121
$ telemetryMock ,
111
- Configuration::createDefault ()
122
+ Configuration::createDefault (),
123
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
124
+ new NullLogger ()
112
125
);
113
126
114
127
$ client ->configuration ()->dependencies ()->disable ();
@@ -121,11 +134,13 @@ public function test_tracking_dependencies_when_option_is_disabled()
121
134
122
135
public function test_tracking_dependencies_when_dependency_is_ignored ()
123
136
{
124
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
137
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
125
138
126
139
$ client = new Client (
127
140
$ telemetryMock ,
128
- Configuration::createDefault ()
141
+ Configuration::createDefault (),
142
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
143
+ new NullLogger ()
129
144
);
130
145
131
146
$ client ->configuration ()->dependencies ()->ignore ('dependency_name ' );
@@ -138,11 +153,13 @@ public function test_tracking_dependencies_when_dependency_is_ignored()
138
153
139
154
public function test_tracking_exceptions_when_option_is_enabled ()
140
155
{
141
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
156
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
142
157
143
158
$ client = new Client (
144
159
$ telemetryMock ,
145
- Configuration::createDefault ()
160
+ Configuration::createDefault (),
161
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
162
+ new NullLogger ()
146
163
);
147
164
148
165
$ exception = new \Exception ();
@@ -156,11 +173,13 @@ public function test_tracking_exceptions_when_option_is_enabled()
156
173
157
174
public function test_tracking_exceptions_when_option_is_disabled ()
158
175
{
159
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
176
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
160
177
161
178
$ client = new Client (
162
179
$ telemetryMock ,
163
- Configuration::createDefault ()
180
+ Configuration::createDefault (),
181
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
182
+ new NullLogger ()
164
183
);
165
184
166
185
$ client ->configuration ()->exceptions ()->disable ();
@@ -175,11 +194,13 @@ public function test_tracking_exceptions_when_option_is_disabled()
175
194
176
195
public function test_tracking_exceptions_that_suppose_to_be_ignored ()
177
196
{
178
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
197
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
179
198
180
199
$ client = new Client (
181
200
$ telemetryMock ,
182
- Configuration::createDefault ()
201
+ Configuration::createDefault (),
202
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
203
+ new NullLogger ()
183
204
);
184
205
185
206
$ client ->configuration ()->exceptions ()->ignore (\RuntimeException::class);
@@ -193,11 +214,13 @@ public function test_tracking_exceptions_that_suppose_to_be_ignored()
193
214
194
215
public function test_tracking_traces_when_option_is_enabled ()
195
216
{
196
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
217
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
197
218
198
219
$ client = new Client (
199
220
$ telemetryMock ,
200
- Configuration::createDefault ()
221
+ Configuration::createDefault (),
222
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
223
+ new NullLogger ()
201
224
);
202
225
203
226
$ telemetryMock ->expects ($ this ->once ())
@@ -209,11 +232,13 @@ public function test_tracking_traces_when_option_is_enabled()
209
232
210
233
public function test_tracking_traces_when_option_is_disabled ()
211
234
{
212
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
235
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
213
236
214
237
$ client = new Client (
215
238
$ telemetryMock ,
216
- Configuration::createDefault ()
239
+ Configuration::createDefault (),
240
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
241
+ new NullLogger ()
217
242
);
218
243
219
244
$ client ->configuration ()->traces ()->disable ();
@@ -229,98 +254,102 @@ public function test_flushing_when_client_is_disabled()
229
254
$ configuration = Configuration::createDefault ();
230
255
$ configuration ->disable ();
231
256
232
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class);
233
- $ this ->givenTelemetryChannelIsNotEmpty ($ telemetryMock );
257
+ $ telemetryMock = $ this ->createTelemetryClientMock ();
234
258
235
259
$ telemetryMock ->expects ($ this ->never ())->method ('flush ' );
236
260
237
- $ client = new Client ($ telemetryMock , $ configuration );
261
+ $ client = new Client (
262
+ $ telemetryMock ,
263
+ $ configuration ,
264
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
265
+ new NullLogger ()
266
+ );
238
267
$ client ->flush ();
239
268
}
240
269
241
270
public function test_fallback_logger_during_flush_unexpected_exception ()
242
271
{
243
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
272
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
244
273
$ loggerMock = $ this ->createMock (LoggerInterface::class);
245
274
246
- $ this ->givenTelemetryChannelIsNotEmpty ($ telemetryMock );
247
275
$ telemetryMock ->method ('flush ' )->willThrowException (new \RuntimeException ('Unexpected API exception ' ));
248
276
249
277
$ loggerMock ->expects ($ this ->once ())
250
278
->method ('error ' )
251
279
->with ('Exception occurred while flushing App Insights Telemetry Client: Unexpected API exception ' );
252
280
253
- $ client = new Client ($ telemetryMock , Configuration::createDefault (), null , $ loggerMock );
281
+ $ client = new Client (
282
+ $ telemetryMock ,
283
+ Configuration::createDefault (),
284
+ new FailureCache ($ this ->createMock (CacheInterface::class)),
285
+ $ loggerMock
286
+ );
254
287
$ client ->flush ();
255
288
}
256
289
257
290
public function test_adding_queue_to_failure_cache_on_unexpected_api_exception_and_cache_is_empty ()
258
291
{
259
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
292
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
260
293
$ loggerMock = $ this ->createMock (LoggerInterface::class);
261
294
$ cacheMock = $ this ->createMock (CacheInterface::class);
262
295
263
- $ this ->givenTelemetryChannelIsNotEmpty ($ telemetryMock );
264
296
$ telemetryMock ->method ('flush ' )->willThrowException (new \RuntimeException ('Unexpected API exception ' ));
265
297
$ cacheMock ->method ('has ' )->willReturn (false );
266
298
267
299
$ cacheMock ->expects ($ this ->once ())
268
300
->method ('set ' )
269
- ->with (Client ::CACHE_CHANNEL_KEY , serialize ([' some_log_entry ' ]), Client ::CACHE_CHANNEL_TTL_SEC );
301
+ ->with (FailureCache ::CACHE_CHANNEL_KEY , serialize ([new Envelope () ]), FailureCache ::CACHE_CHANNEL_TTL_SEC );
270
302
271
- $ client = new Client ($ telemetryMock , Configuration::createDefault (), $ cacheMock , $ loggerMock );
303
+ $ client = new Client ($ telemetryMock , Configuration::createDefault (), new FailureCache ( $ cacheMock) , $ loggerMock );
272
304
$ client ->flush ();
273
305
}
274
306
275
307
public function test_adding_queue_to_failure_cache_on_unexpected_api_exception_and_cache_is_not_empty ()
276
308
{
277
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
309
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
278
310
$ loggerMock = $ this ->createMock (LoggerInterface::class);
279
311
$ cacheMock = $ this ->createMock (CacheInterface::class);
280
312
281
- $ this ->givenTelemetryChannelIsNotEmpty ($ telemetryMock );
282
313
$ telemetryMock ->method ('flush ' )->willThrowException (new \RuntimeException ('Unexpected API exception ' ));
283
314
$ cacheMock ->method ('has ' )->willReturn (true );
284
- $ cacheMock ->method ('get ' )->willReturn (serialize ([' some_older_entry ' ]));
315
+ $ cacheMock ->method ('get ' )->willReturn (serialize ([new Envelope () ]));
285
316
286
317
$ cacheMock ->expects ($ this ->once ())
287
318
->method ('set ' )
288
- ->with (Client ::CACHE_CHANNEL_KEY , serialize ([' some_older_entry ' , ' some_log_entry ' ]), Client ::CACHE_CHANNEL_TTL_SEC );
319
+ ->with (FailureCache ::CACHE_CHANNEL_KEY , serialize ([new Envelope (), new Envelope () ]), FailureCache ::CACHE_CHANNEL_TTL_SEC );
289
320
290
- $ client = new Client ($ telemetryMock , Configuration::createDefault (), $ cacheMock , $ loggerMock );
321
+ $ client = new Client ($ telemetryMock , Configuration::createDefault (), new FailureCache ( $ cacheMock) , $ loggerMock );
291
322
$ client ->flush ();
292
323
}
293
324
294
325
public function test_flush_when_cache_is_not_empty ()
295
326
{
296
- $ telemetryMock = $ this ->createMock (Telemetry_Client::class );
327
+ $ telemetryMock = $ this ->createTelemetryClientMock ( );
297
328
$ loggerMock = $ this ->createMock (LoggerInterface::class);
298
329
$ cacheMock = $ this ->createMock (CacheInterface::class);
299
330
300
- $ telemetryChannelMock = $ this ->givenTelemetryChannelIsNotEmpty ($ telemetryMock );
301
331
$ cacheMock ->method ('has ' )->willReturn (true );
302
- $ cacheMock ->method ('get ' )->willReturn (serialize (['some_older_entry ' ]));
332
+ $ cacheMock ->method ('get ' )->willReturn (serialize ([new Envelope ( 'some_older_entry ' ) ]));
303
333
304
334
$ cacheMock ->expects ($ this ->once ())
305
335
->method ('delete ' )
306
- ->with (Client::CACHE_CHANNEL_KEY );
307
-
308
- $ telemetryChannelMock ->expects ($ this ->once ())
309
- ->method ('setQueue ' )
310
- ->with (['some_older_entry ' , 'some_log_entry ' ]);
336
+ ->with (FailureCache::CACHE_CHANNEL_KEY );
311
337
312
338
$ telemetryMock ->expects ($ this ->once ())->method ('flush ' );
313
339
314
- $ client = new Client ($ telemetryMock , Configuration::createDefault (), $ cacheMock , $ loggerMock );
340
+ $ client = new Client ($ telemetryMock , Configuration::createDefault (), new FailureCache ( $ cacheMock) , $ loggerMock );
315
341
$ client ->flush ();
316
342
}
317
343
318
- private function givenTelemetryChannelIsNotEmpty ( MockObject $ telemetryMock ): MockObject
344
+ private function createTelemetryClientMock ( ): MockObject
319
345
{
320
- $ telemetryMock ->method ('getChannel ' )->willReturn ($ telemetryChannelMock = $ this ->createMock (Telemetry_Channel::class));
321
- $ telemetryChannelMock ->method ('getQueue ' )->willReturn (['some_log_entry ' ]);
322
- $ telemetryChannelMock ->method ('getSerializedQueue ' )->willReturn (json_encode (['some_log_entry ' ]));
346
+ $ telemetryClientMock = $ this ->createMock (Telemetry_Client::class);
347
+ $ telemetryClientMock ->method ('getChannel ' )->willReturn (
348
+ $ telemetryChannelMock = $ this ->createMock (Telemetry_Channel::class)
349
+ );
350
+ $ telemetryChannelMock ->method ('getQueue ' )->willReturn ([new Envelope ()]);
351
+ $ telemetryChannelMock ->method ('getSerializedQueue ' )->willReturn (json_encode ([new Envelope ()]));
323
352
324
- return $ telemetryChannelMock ;
353
+ return $ telemetryClientMock ;
325
354
}
326
355
}
0 commit comments