@@ -19,7 +19,7 @@ class Test_OD_URL_Metric_Group_Collection extends WP_UnitTestCase {
19
19
* @return array<string, mixed> Data.
20
20
*/
21
21
public function data_provider_test_construction (): array {
22
- $ current_etag = od_get_current_url_metrics_etag ( new OD_Tag_Visitor_Registry () );
22
+ $ current_etag = md5 ( '' );
23
23
24
24
return array (
25
25
'no_breakpoints_ok ' => array (
@@ -402,16 +402,22 @@ static function ( OD_URL_Metric $url_metric ): int {
402
402
*/
403
403
public function data_provider_test_get_group_for_viewport_width (): array {
404
404
$ current_time = microtime ( true );
405
+ $ current_etag = md5 ( '' );
405
406
406
407
$ none_needed_data = array (
407
- 'url_metrics ' => ( function () use ( $ current_time ): array {
408
+ 'url_metrics ' => ( function () use ( $ current_time, $ current_etag ): array {
408
409
return array_merge (
409
410
array_fill (
410
411
0 ,
411
412
3 ,
412
413
new OD_URL_Metric (
413
414
array_merge (
414
- $ this ->get_sample_url_metric ( array ( 'viewport_width ' => 400 ) )->jsonSerialize (),
415
+ $ this ->get_sample_url_metric (
416
+ array (
417
+ 'viewport_width ' => 400 ,
418
+ 'etag ' => $ current_etag ,
419
+ )
420
+ )->jsonSerialize (),
415
421
array ( 'timestamp ' => $ current_time )
416
422
)
417
423
)
@@ -421,14 +427,20 @@ public function data_provider_test_get_group_for_viewport_width(): array {
421
427
3 ,
422
428
new OD_URL_Metric (
423
429
array_merge (
424
- $ this ->get_sample_url_metric ( array ( 'viewport_width ' => 600 ) )->jsonSerialize (),
430
+ $ this ->get_sample_url_metric (
431
+ array (
432
+ 'viewport_width ' => 600 ,
433
+ 'etag ' => $ current_etag ,
434
+ )
435
+ )->jsonSerialize (),
425
436
array ( 'timestamp ' => $ current_time )
426
437
)
427
438
)
428
439
)
429
440
);
430
441
} )(),
431
442
'current_time ' => $ current_time ,
443
+ 'current_etag ' => $ current_etag ,
432
444
'breakpoints ' => array ( 480 ),
433
445
'sample_size ' => 3 ,
434
446
'freshness_ttl ' => HOUR_IN_SECONDS ,
@@ -508,6 +520,34 @@ public function data_provider_test_get_group_for_viewport_width(): array {
508
520
),
509
521
)
510
522
),
523
+
524
+ 'url-metric-stale-etag ' => array_merge (
525
+ ( static function ( $ data ): array {
526
+ $ url_metrics_data = $ data ['url_metrics ' ][ count ( $ data ['url_metrics ' ] ) - 1 ]->jsonSerialize ();
527
+ $ url_metrics_data ['etag ' ] = md5 ( 'something new! ' );
528
+ $ data ['url_metrics ' ][ count ( $ data ['url_metrics ' ] ) - 1 ] = new OD_URL_Metric ( $ url_metrics_data );
529
+ return $ data ;
530
+ } )( $ none_needed_data ),
531
+ array (
532
+ 'expected_return ' => array (
533
+ array (
534
+ 'minimumViewportWidth ' => 0 ,
535
+ 'complete ' => true ,
536
+ ),
537
+ array (
538
+ 'minimumViewportWidth ' => 481 ,
539
+ 'complete ' => false ,
540
+ ),
541
+ ),
542
+ 'expected_is_group_complete ' => array (
543
+ 200 => true ,
544
+ 400 => true ,
545
+ 480 => true ,
546
+ 481 => false ,
547
+ 500 => false ,
548
+ ),
549
+ )
550
+ ),
511
551
);
512
552
}
513
553
@@ -523,14 +563,14 @@ public function data_provider_test_get_group_for_viewport_width(): array {
523
563
*
524
564
* @param OD_URL_Metric[] $url_metrics URL Metrics.
525
565
* @param float $current_time Current time.
566
+ * @param non-empty-string $current_etag Current ETag.
526
567
* @param int[] $breakpoints Breakpoints.
527
568
* @param int $sample_size Sample size.
528
569
* @param int $freshness_ttl Freshness TTL.
529
570
* @param array<int, mixed> $expected_return Expected return.
530
571
* @param array<int, bool> $expected_is_group_complete Expected is group complete.
531
572
*/
532
- public function test_get_group_for_viewport_width ( array $ url_metrics , float $ current_time , array $ breakpoints , int $ sample_size , int $ freshness_ttl , array $ expected_return , array $ expected_is_group_complete ): void {
533
- $ current_etag = od_get_current_url_metrics_etag ( new OD_Tag_Visitor_Registry () );
573
+ public function test_get_group_for_viewport_width ( array $ url_metrics , float $ current_time , string $ current_etag , array $ breakpoints , int $ sample_size , int $ freshness_ttl , array $ expected_return , array $ expected_is_group_complete ): void {
534
574
$ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , $ current_etag , $ breakpoints , $ sample_size , $ freshness_ttl );
535
575
$ this ->assertSame (
536
576
$ expected_return ,
@@ -563,7 +603,7 @@ static function ( OD_URL_Metric_Group $group ): array {
563
603
public function test_is_every_group_populated (): void {
564
604
$ breakpoints = array ( 480 , 800 );
565
605
$ sample_size = 3 ;
566
- $ current_etag = od_get_current_url_metrics_etag ( new OD_Tag_Visitor_Registry () );
606
+ $ current_etag = md5 ( '' );
567
607
$ group_collection = new OD_URL_Metric_Group_Collection (
568
608
array (),
569
609
$ current_etag ,
@@ -573,20 +613,48 @@ public function test_is_every_group_populated(): void {
573
613
);
574
614
$ this ->assertFalse ( $ group_collection ->is_every_group_populated () );
575
615
$ this ->assertFalse ( $ group_collection ->is_every_group_complete () );
576
- $ group_collection ->add_url_metric ( $ this ->get_sample_url_metric ( array ( 'viewport_width ' => 200 ) ) );
616
+ $ group_collection ->add_url_metric (
617
+ $ this ->get_sample_url_metric (
618
+ array (
619
+ 'viewport_width ' => 200 ,
620
+ 'etag ' => $ current_etag ,
621
+ )
622
+ )
623
+ );
577
624
$ this ->assertFalse ( $ group_collection ->is_every_group_populated () );
578
625
$ this ->assertFalse ( $ group_collection ->is_every_group_complete () );
579
- $ group_collection ->add_url_metric ( $ this ->get_sample_url_metric ( array ( 'viewport_width ' => 500 ) ) );
626
+ $ group_collection ->add_url_metric (
627
+ $ this ->get_sample_url_metric (
628
+ array (
629
+ 'viewport_width ' => 500 ,
630
+ 'etag ' => $ current_etag ,
631
+ )
632
+ )
633
+ );
580
634
$ this ->assertFalse ( $ group_collection ->is_every_group_populated () );
581
635
$ this ->assertFalse ( $ group_collection ->is_every_group_complete () );
582
- $ group_collection ->add_url_metric ( $ this ->get_sample_url_metric ( array ( 'viewport_width ' => 900 ) ) );
636
+ $ group_collection ->add_url_metric (
637
+ $ this ->get_sample_url_metric (
638
+ array (
639
+ 'viewport_width ' => 900 ,
640
+ 'etag ' => $ current_etag ,
641
+ )
642
+ )
643
+ );
583
644
$ this ->assertTrue ( $ group_collection ->is_every_group_populated () );
584
645
$ this ->assertFalse ( $ group_collection ->is_every_group_complete () );
585
646
586
647
// Now finish completing all the groups.
587
648
foreach ( array_merge ( $ breakpoints , array ( 1000 ) ) as $ viewport_width ) {
588
649
for ( $ i = 0 ; $ i < $ sample_size ; $ i ++ ) {
589
- $ group_collection ->add_url_metric ( $ this ->get_sample_url_metric ( array ( 'viewport_width ' => $ viewport_width ) ) );
650
+ $ group_collection ->add_url_metric (
651
+ $ this ->get_sample_url_metric (
652
+ array (
653
+ 'viewport_width ' => $ viewport_width ,
654
+ 'etag ' => $ current_etag ,
655
+ )
656
+ )
657
+ );
590
658
}
591
659
}
592
660
$ this ->assertTrue ( $ group_collection ->is_every_group_complete () );
0 commit comments