|
254 | 254 | }); |
255 | 255 | }); |
256 | 256 |
|
| 257 | +it('terminates directly when using trackNow while deferred is enabled', function () { |
| 258 | + // Given we have a user |
| 259 | + $user = new SegmentTestUser('abcd'); |
| 260 | + |
| 261 | + // And we are deferring |
| 262 | + setDefer(true); |
| 263 | + |
| 264 | + // And we have set a write key |
| 265 | + setWriteKey(); |
| 266 | + |
| 267 | + // And we are faking the Http facade |
| 268 | + Http::fake(); |
| 269 | + |
| 270 | + // And we call the track method |
| 271 | + Segment::forUser($user)->trackNow('Something Happened', [ |
| 272 | + 'name' => 'special', |
| 273 | + ]); |
| 274 | + |
| 275 | + // Then we have made the calls to Segment |
| 276 | + Http::assertSent(function (Request $request) { |
| 277 | + return $request->hasHeader('Content-Type', 'application/json') |
| 278 | + && $request->hasHeader('Authorization', 'Bearer '.base64_encode('key_1234:')) |
| 279 | + && $request->url() === 'https://api.segment.io/v1/batch' |
| 280 | + && $request['context'] === [] |
| 281 | + && count($request['batch']) === 1 |
| 282 | + && arraysMatch($request['batch'][0], [ |
| 283 | + 'type' => 'track', |
| 284 | + 'userId' => 'abcd', |
| 285 | + 'timestamp' => (new DateTime())->format('Y-m-d\TH:i:s\Z'), |
| 286 | + 'properties' => [ |
| 287 | + 'name' => 'special', |
| 288 | + ], |
| 289 | + 'event' => 'Something Happened', |
| 290 | + ]); |
| 291 | + }); |
| 292 | +}); |
| 293 | + |
| 294 | +it('terminates directly when using identifyNow while deferred is enabled', function () { |
| 295 | + // Given we have a user |
| 296 | + $user = new SegmentTestUser('abcd'); |
| 297 | + |
| 298 | + // And we are deferring |
| 299 | + setDefer(true); |
| 300 | + |
| 301 | + // And we have set a write key |
| 302 | + setWriteKey(); |
| 303 | + |
| 304 | + // And we are faking the Http facade |
| 305 | + Http::fake(); |
| 306 | + |
| 307 | + // And we call the track method |
| 308 | + Segment::forUser($user)->identifyNow([ |
| 309 | + 'seen_email' => true, |
| 310 | + ]); |
| 311 | + |
| 312 | + // Then we have made the calls to Segment |
| 313 | + Http::assertSent(function (Request $request) { |
| 314 | + return $request->hasHeader('Content-Type', 'application/json') |
| 315 | + && $request->hasHeader('Authorization', 'Bearer '.base64_encode('key_1234:')) |
| 316 | + && $request->url() === 'https://api.segment.io/v1/batch' |
| 317 | + && $request['context'] === [] |
| 318 | + && count($request['batch']) === 1 |
| 319 | + && arraysMatch($request['batch'][0], [ |
| 320 | + 'type' => 'identify', |
| 321 | + 'userId' => 'abcd', |
| 322 | + 'timestamp' => (new DateTime())->format('Y-m-d\TH:i:s\Z'), |
| 323 | + 'traits' => [ |
| 324 | + 'seen_email' => true, |
| 325 | + ], |
| 326 | + ]); |
| 327 | + }); |
| 328 | +}); |
| 329 | + |
| 330 | +it('terminates directly when using trackNow while deferred is enabled with global user and context', function () { |
| 331 | + // Given we have a user |
| 332 | + $user = new SegmentTestUser('abcd'); |
| 333 | + |
| 334 | + // And we are deferring |
| 335 | + setDefer(true); |
| 336 | + |
| 337 | + // And we have set a write key |
| 338 | + setWriteKey(); |
| 339 | + |
| 340 | + // And we have set global user |
| 341 | + Segment::setGlobalUser($user); |
| 342 | + |
| 343 | + // And we have set global context |
| 344 | + Segment::setGlobalContext([ |
| 345 | + 'ip' => '127.0.0.1', |
| 346 | + ]); |
| 347 | + |
| 348 | + // And we are faking the Http facade |
| 349 | + Http::fake(); |
| 350 | + |
| 351 | + // And we call the track method |
| 352 | + Segment::trackNow('Something Happened', [ |
| 353 | + 'name' => 'special', |
| 354 | + ]); |
| 355 | + |
| 356 | + // Then we have made the calls to Segment |
| 357 | + Http::assertSent(function (Request $request) { |
| 358 | + return $request->hasHeader('Content-Type', 'application/json') |
| 359 | + && $request->hasHeader('Authorization', 'Bearer '.base64_encode('key_1234:')) |
| 360 | + && $request->url() === 'https://api.segment.io/v1/batch' |
| 361 | + && $request['context'] === ['ip' => '127.0.0.1'] |
| 362 | + && count($request['batch']) === 1 |
| 363 | + && arraysMatch($request['batch'][0], [ |
| 364 | + 'type' => 'track', |
| 365 | + 'userId' => 'abcd', |
| 366 | + 'timestamp' => (new DateTime())->format('Y-m-d\TH:i:s\Z'), |
| 367 | + 'properties' => [ |
| 368 | + 'name' => 'special', |
| 369 | + ], |
| 370 | + 'event' => 'Something Happened', |
| 371 | + ]); |
| 372 | + }); |
| 373 | +}); |
| 374 | + |
| 375 | +it('terminates directly when using identifyNow while deferred is enabled with global user and context', function () { |
| 376 | + // Given we have a user |
| 377 | + $user = new SegmentTestUser('abcd'); |
| 378 | + |
| 379 | + // And we are deferring |
| 380 | + setDefer(true); |
| 381 | + |
| 382 | + // And we have set a write key |
| 383 | + setWriteKey(); |
| 384 | + |
| 385 | + // And we have set global user |
| 386 | + Segment::setGlobalUser($user); |
| 387 | + |
| 388 | + // And we have set global context |
| 389 | + Segment::setGlobalContext([ |
| 390 | + 'ip' => '127.0.0.1', |
| 391 | + ]); |
| 392 | + |
| 393 | + // And we are faking the Http facade |
| 394 | + Http::fake(); |
| 395 | + |
| 396 | + // And we call the track method |
| 397 | + Segment::identifyNow([ |
| 398 | + 'seen_email' => true, |
| 399 | + ]); |
| 400 | + |
| 401 | + // Then we have made the calls to Segment |
| 402 | + Http::assertSent(function (Request $request) { |
| 403 | + return $request->hasHeader('Content-Type', 'application/json') |
| 404 | + && $request->hasHeader('Authorization', 'Bearer '.base64_encode('key_1234:')) |
| 405 | + && $request->url() === 'https://api.segment.io/v1/batch' |
| 406 | + && $request['context'] === ['ip' => '127.0.0.1'] |
| 407 | + && count($request['batch']) === 1 |
| 408 | + && arraysMatch($request['batch'][0], [ |
| 409 | + 'type' => 'identify', |
| 410 | + 'userId' => 'abcd', |
| 411 | + 'timestamp' => (new DateTime())->format('Y-m-d\TH:i:s\Z'), |
| 412 | + 'traits' => [ |
| 413 | + 'seen_email' => true, |
| 414 | + ], |
| 415 | + ]); |
| 416 | + }); |
| 417 | +}); |
| 418 | + |
257 | 419 | it('does not sent tracking events when not enabled', function () { |
258 | 420 | // Given we have a user |
259 | 421 | $user = new SegmentTestUser('abcd'); |
|
0 commit comments