@@ -19,66 +19,68 @@ 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_compute_current_etag ( new OD_Tag_Visitor_Registry () );
23
+
22
24
return array (
23
25
'no_breakpoints_ok ' => array (
24
26
'url_metrics ' => array (),
25
- 'current_etag ' => '' ,
27
+ 'current_etag ' => $ current_etag ,
26
28
'breakpoints ' => array (),
27
29
'sample_size ' => 3 ,
28
30
'freshness_ttl ' => HOUR_IN_SECONDS ,
29
31
'exception ' => '' ,
30
32
),
31
33
'negative_breakpoint_bad ' => array (
32
34
'url_metrics ' => array (),
33
- 'current_etag ' => '' ,
35
+ 'current_etag ' => $ current_etag ,
34
36
'breakpoints ' => array ( -1 ),
35
37
'sample_size ' => 3 ,
36
38
'freshness_ttl ' => HOUR_IN_SECONDS ,
37
39
'exception ' => InvalidArgumentException::class,
38
40
),
39
41
'zero_breakpoint_bad ' => array (
40
42
'url_metrics ' => array (),
41
- 'current_etag ' => '' ,
43
+ 'current_etag ' => $ current_etag ,
42
44
'breakpoints ' => array ( 0 ),
43
45
'sample_size ' => 3 ,
44
46
'freshness_ttl ' => HOUR_IN_SECONDS ,
45
47
'exception ' => InvalidArgumentException::class,
46
48
),
47
49
'max_breakpoint_bad ' => array (
48
50
'url_metrics ' => array (),
49
- 'current_etag ' => '' ,
51
+ 'current_etag ' => $ current_etag ,
50
52
'breakpoints ' => array ( PHP_INT_MAX ),
51
53
'sample_size ' => 3 ,
52
54
'freshness_ttl ' => HOUR_IN_SECONDS ,
53
55
'exception ' => InvalidArgumentException::class,
54
56
),
55
57
'string_breakpoint_bad ' => array (
56
58
'url_metrics ' => array (),
57
- 'current_etag ' => '' ,
59
+ 'current_etag ' => $ current_etag ,
58
60
'breakpoints ' => array ( 'narrow ' ),
59
61
'sample_size ' => 3 ,
60
62
'freshness_ttl ' => HOUR_IN_SECONDS ,
61
63
'exception ' => InvalidArgumentException::class,
62
64
),
63
65
'negative_sample_size_bad ' => array (
64
66
'url_metrics ' => array (),
65
- 'current_etag ' => '' ,
67
+ 'current_etag ' => $ current_etag ,
66
68
'breakpoints ' => array ( 400 ),
67
69
'sample_size ' => -3 ,
68
70
'freshness_ttl ' => HOUR_IN_SECONDS ,
69
71
'exception ' => InvalidArgumentException::class,
70
72
),
71
73
'negative_freshness_tll_bad ' => array (
72
74
'url_metrics ' => array (),
73
- 'current_etag ' => '' ,
75
+ 'current_etag ' => $ current_etag ,
74
76
'breakpoints ' => array ( 400 ),
75
77
'sample_size ' => 3 ,
76
78
'freshness_ttl ' => -HOUR_IN_SECONDS ,
77
79
'exception ' => InvalidArgumentException::class,
78
80
),
79
81
'invalid_url_metrics_bad ' => array (
80
82
'url_metrics ' => array ( 'bad ' ),
81
- 'current_etag ' => '' ,
83
+ 'current_etag ' => $ current_etag ,
82
84
'breakpoints ' => array ( 400 ),
83
85
'sample_size ' => 3 ,
84
86
'freshness_ttl ' => HOUR_IN_SECONDS ,
@@ -89,7 +91,7 @@ public function data_provider_test_construction(): array {
89
91
$ this ->get_sample_url_metric ( array ( 'viewport_width ' => 200 ) ),
90
92
$ this ->get_sample_url_metric ( array ( 'viewport_width ' => 400 ) ),
91
93
),
92
- 'current_etag ' => '' ,
94
+ 'current_etag ' => $ current_etag ,
93
95
'breakpoints ' => array ( 400 ),
94
96
'sample_size ' => 3 ,
95
97
'freshness_ttl ' => HOUR_IN_SECONDS ,
@@ -194,7 +196,8 @@ public function data_provider_sample_size_and_breakpoints(): array {
194
196
* @dataProvider data_provider_sample_size_and_breakpoints
195
197
*/
196
198
public function test_add_url_metric ( int $ sample_size , array $ breakpoints , array $ viewport_widths , array $ expected_counts ): void {
197
- $ group_collection = new OD_URL_Metric_Group_Collection ( array (), '' , $ breakpoints , $ sample_size , HOUR_IN_SECONDS );
199
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
200
+ $ group_collection = new OD_URL_Metric_Group_Collection ( array (), $ current_etag , $ breakpoints , $ sample_size , HOUR_IN_SECONDS );
198
201
199
202
// Over-populate the sample size for the breakpoints by a dozen.
200
203
foreach ( $ viewport_widths as $ viewport_width => $ count ) {
@@ -222,9 +225,10 @@ public function test_add_url_metric( int $sample_size, array $breakpoints, array
222
225
* @covers ::add_url_metric
223
226
*/
224
227
public function test_adding_pushes_out_old_metrics (): void {
228
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
225
229
$ sample_size = 3 ;
226
230
$ breakpoints = array ( 400 , 600 );
227
- $ group_collection = new OD_URL_Metric_Group_Collection ( array (), '' , $ breakpoints , $ sample_size , HOUR_IN_SECONDS );
231
+ $ group_collection = new OD_URL_Metric_Group_Collection ( array (), $ current_etag , $ breakpoints , $ sample_size , HOUR_IN_SECONDS );
228
232
229
233
// Populate the groups with stale URL Metrics.
230
234
$ viewport_widths = array ( 300 , 500 , 700 );
@@ -345,7 +349,8 @@ function ( $viewport_width ) {
345
349
$ viewport_widths
346
350
);
347
351
348
- $ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , '' , $ breakpoints , 3 , HOUR_IN_SECONDS );
352
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
353
+ $ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , $ current_etag , $ breakpoints , 3 , HOUR_IN_SECONDS );
349
354
350
355
$ this ->assertCount (
351
356
count ( $ breakpoints ) + 1 ,
@@ -509,7 +514,8 @@ public function data_provider_test_get_group_for_viewport_width(): array {
509
514
* @param array<int, bool> $expected_is_group_complete Expected is group complete.
510
515
*/
511
516
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 {
512
- $ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , '' , $ breakpoints , $ sample_size , $ freshness_ttl );
517
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
518
+ $ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , $ current_etag , $ breakpoints , $ sample_size , $ freshness_ttl );
513
519
$ this ->assertSame (
514
520
$ expected_return ,
515
521
array_map (
@@ -541,9 +547,10 @@ static function ( OD_URL_Metric_Group $group ): array {
541
547
public function test_is_every_group_populated (): void {
542
548
$ breakpoints = array ( 480 , 800 );
543
549
$ sample_size = 3 ;
550
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
544
551
$ group_collection = new OD_URL_Metric_Group_Collection (
545
552
array (),
546
- '' ,
553
+ $ current_etag ,
547
554
$ breakpoints ,
548
555
$ sample_size ,
549
556
HOUR_IN_SECONDS
@@ -595,6 +602,7 @@ public function test_get_groups_by_lcp_element(): void {
595
602
596
603
$ breakpoints = array ( 480 , 800 );
597
604
$ sample_size = 3 ;
605
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
598
606
$ group_collection = new OD_URL_Metric_Group_Collection (
599
607
array (
600
608
// Group 1: 0-480 viewport widths.
@@ -607,7 +615,7 @@ public function test_get_groups_by_lcp_element(): void {
607
615
$ get_url_metric_with_one_lcp_element ( 820 , $ first_child_image_xpath ),
608
616
$ get_url_metric_with_one_lcp_element ( 900 , $ first_child_image_xpath ),
609
617
),
610
- '' ,
618
+ $ current_etag ,
611
619
$ breakpoints ,
612
620
$ sample_size ,
613
621
HOUR_IN_SECONDS
@@ -637,9 +645,10 @@ public function test_get_groups_by_lcp_element(): void {
637
645
public function test_get_common_lcp_element (): void {
638
646
$ breakpoints = array ( 480 , 800 );
639
647
$ sample_size = 3 ;
648
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
640
649
$ group_collection = new OD_URL_Metric_Group_Collection (
641
650
array (),
642
- '' ,
651
+ $ current_etag ,
643
652
$ breakpoints ,
644
653
$ sample_size ,
645
654
HOUR_IN_SECONDS
@@ -750,9 +759,10 @@ public function data_provider_element_max_intersection_ratios(): array {
750
759
* @param array<string, float> $expected Expected.
751
760
*/
752
761
public function test_get_all_element_max_intersection_ratios ( array $ url_metrics , array $ expected ): void {
762
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
753
763
$ breakpoints = array ( 480 , 600 , 782 );
754
764
$ sample_size = 3 ;
755
- $ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , '' , $ breakpoints , $ sample_size , 0 );
765
+ $ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , $ current_etag , $ breakpoints , $ sample_size , 0 );
756
766
$ actual = $ group_collection ->get_all_element_max_intersection_ratios ();
757
767
$ this ->assertSame ( $ actual , $ group_collection ->get_all_element_max_intersection_ratios (), 'Cached result is identical. ' );
758
768
$ this ->assertSame ( $ expected , $ actual );
@@ -927,9 +937,10 @@ public function data_provider_get_all_elements_positioned_in_any_initial_viewpor
927
937
* @param array<string, bool> $expected Expected.
928
938
*/
929
939
public function test_get_all_elements_positioned_in_any_initial_viewport ( array $ url_metrics , array $ expected ): void {
940
+ $ current_etag = od_compute_current_etag ( new OD_Tag_Visitor_Registry () );
930
941
$ breakpoints = array ( 480 , 600 , 782 );
931
942
$ sample_size = 3 ;
932
- $ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , '' , $ breakpoints , $ sample_size , 0 );
943
+ $ group_collection = new OD_URL_Metric_Group_Collection ( $ url_metrics , $ current_etag , $ breakpoints , $ sample_size , 0 );
933
944
$ actual = $ group_collection ->get_all_elements_positioned_in_any_initial_viewport ();
934
945
$ this ->assertSame ( $ actual , $ group_collection ->get_all_elements_positioned_in_any_initial_viewport (), 'Cached result is identical. ' );
935
946
$ this ->assertSame ( $ expected , $ actual );
@@ -953,7 +964,7 @@ public function test_get_flattened_url_metrics(): void {
953
964
954
965
$ group_collection = new OD_URL_Metric_Group_Collection (
955
966
$ url_metrics ,
956
- '' ,
967
+ od_compute_current_etag ( new OD_Tag_Visitor_Registry () ) ,
957
968
array ( 500 , 700 ),
958
969
3 ,
959
970
HOUR_IN_SECONDS
@@ -981,7 +992,7 @@ public function test_json_serialize(): void {
981
992
982
993
$ group_collection = new OD_URL_Metric_Group_Collection (
983
994
$ url_metrics ,
984
- '' ,
995
+ od_compute_current_etag ( new OD_Tag_Visitor_Registry () ) ,
985
996
array ( 500 , 700 ),
986
997
3 ,
987
998
HOUR_IN_SECONDS
0 commit comments