Skip to content

Commit 54183af

Browse files
committed
Eliminate constructing etag via OD_Tag_Visitor_Registry() unless side effects are needed
1 parent 02e9759 commit 54183af

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

plugins/optimization-detective/tests/test-class-od-url-metrics-group-collection.php

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Test_OD_URL_Metric_Group_Collection extends WP_UnitTestCase {
1919
* @return array<string, mixed> Data.
2020
*/
2121
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( '' );
2323

2424
return array(
2525
'no_breakpoints_ok' => array(
@@ -402,16 +402,22 @@ static function ( OD_URL_Metric $url_metric ): int {
402402
*/
403403
public function data_provider_test_get_group_for_viewport_width(): array {
404404
$current_time = microtime( true );
405+
$current_etag = md5( '' );
405406

406407
$none_needed_data = array(
407-
'url_metrics' => ( function () use ( $current_time ): array {
408+
'url_metrics' => ( function () use ( $current_time, $current_etag ): array {
408409
return array_merge(
409410
array_fill(
410411
0,
411412
3,
412413
new OD_URL_Metric(
413414
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(),
415421
array( 'timestamp' => $current_time )
416422
)
417423
)
@@ -421,14 +427,20 @@ public function data_provider_test_get_group_for_viewport_width(): array {
421427
3,
422428
new OD_URL_Metric(
423429
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(),
425436
array( 'timestamp' => $current_time )
426437
)
427438
)
428439
)
429440
);
430441
} )(),
431442
'current_time' => $current_time,
443+
'current_etag' => $current_etag,
432444
'breakpoints' => array( 480 ),
433445
'sample_size' => 3,
434446
'freshness_ttl' => HOUR_IN_SECONDS,
@@ -508,6 +520,34 @@ public function data_provider_test_get_group_for_viewport_width(): array {
508520
),
509521
)
510522
),
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+
),
511551
);
512552
}
513553

@@ -523,14 +563,14 @@ public function data_provider_test_get_group_for_viewport_width(): array {
523563
*
524564
* @param OD_URL_Metric[] $url_metrics URL Metrics.
525565
* @param float $current_time Current time.
566+
* @param non-empty-string $current_etag Current ETag.
526567
* @param int[] $breakpoints Breakpoints.
527568
* @param int $sample_size Sample size.
528569
* @param int $freshness_ttl Freshness TTL.
529570
* @param array<int, mixed> $expected_return Expected return.
530571
* @param array<int, bool> $expected_is_group_complete Expected is group complete.
531572
*/
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 {
534574
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, $sample_size, $freshness_ttl );
535575
$this->assertSame(
536576
$expected_return,
@@ -563,7 +603,7 @@ static function ( OD_URL_Metric_Group $group ): array {
563603
public function test_is_every_group_populated(): void {
564604
$breakpoints = array( 480, 800 );
565605
$sample_size = 3;
566-
$current_etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() );
606+
$current_etag = md5( '' );
567607
$group_collection = new OD_URL_Metric_Group_Collection(
568608
array(),
569609
$current_etag,
@@ -573,20 +613,48 @@ public function test_is_every_group_populated(): void {
573613
);
574614
$this->assertFalse( $group_collection->is_every_group_populated() );
575615
$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+
);
577624
$this->assertFalse( $group_collection->is_every_group_populated() );
578625
$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+
);
580634
$this->assertFalse( $group_collection->is_every_group_populated() );
581635
$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+
);
583644
$this->assertTrue( $group_collection->is_every_group_populated() );
584645
$this->assertFalse( $group_collection->is_every_group_complete() );
585646

586647
// Now finish completing all the groups.
587648
foreach ( array_merge( $breakpoints, array( 1000 ) ) as $viewport_width ) {
588649
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+
);
590658
}
591659
}
592660
$this->assertTrue( $group_collection->is_every_group_complete() );

tests/class-optimization-detective-test-helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ trait Optimization_Detective_Test_Helpers {
2727
*/
2828
public function populate_url_metrics( array $elements, bool $complete = true ): void {
2929
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
30-
$etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() );
30+
$etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() ); // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
3131
$sample_size = $complete ? od_get_url_metrics_breakpoint_sample_size() : 1;
3232
foreach ( array_merge( od_get_breakpoint_max_widths(), array( 1000 ) ) as $viewport_width ) {
3333
for ( $i = 0; $i < $sample_size; $i++ ) {
@@ -80,7 +80,7 @@ public function get_sample_dom_rect(): array {
8080
public function get_sample_url_metric( array $params ): OD_URL_Metric {
8181
$params = array_merge(
8282
array(
83-
'etag' => od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() ),
83+
'etag' => od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() ), // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
8484
'url' => home_url( '/' ),
8585
'viewport_width' => 480,
8686
'elements' => array(),

0 commit comments

Comments
 (0)