Skip to content

Commit 034fe46

Browse files
committed
Add test for od_get_current_url_metrics_etag()
1 parent 4456563 commit 034fe46

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

plugins/optimization-detective/tests/storage/test-data.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,58 @@ public function test_od_get_url_metrics_slug(): void {
286286
}
287287
}
288288

289+
/**
290+
* Test od_get_current_url_metrics_etag().
291+
*
292+
* @covers ::od_get_current_url_metrics_etag
293+
*/
294+
public function test_od_get_current_url_metrics_etag(): void {
295+
remove_all_filters( 'od_current_url_metrics_etag_data' );
296+
$registry = new OD_Tag_Visitor_Registry();
297+
298+
$captured_etag_data = array();
299+
add_filter(
300+
'od_current_url_metrics_etag_data',
301+
static function ( array $data ) use ( &$captured_etag_data ) {
302+
$captured_etag_data[] = $data;
303+
return $data;
304+
},
305+
PHP_INT_MAX
306+
);
307+
$etag1 = od_get_current_url_metrics_etag( $registry );
308+
$this->assertMatchesRegularExpression( '/^[a-z0-9]{32}\z/', $etag1 );
309+
$etag2 = od_get_current_url_metrics_etag( $registry );
310+
$this->assertSame( $etag1, $etag2 );
311+
$this->assertCount( 2, $captured_etag_data );
312+
$this->assertSame( array( 'tag_visitors' => array() ), $captured_etag_data[0] );
313+
$this->assertSame( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );
314+
315+
$registry->register( 'foo', static function (): void {} );
316+
$registry->register( 'bar', static function (): void {} );
317+
$registry->register( 'baz', static function (): void {} );
318+
$etag3 = od_get_current_url_metrics_etag( $registry );
319+
$this->assertNotEquals( $etag2, $etag3 );
320+
$this->assertNotEquals( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );
321+
$this->assertSame( array( 'tag_visitors' => array( 'foo', 'bar', 'baz' ) ), $captured_etag_data[ count( $captured_etag_data ) - 1 ] );
322+
add_filter(
323+
'od_current_url_metrics_etag_data',
324+
static function ( $data ): array {
325+
$data['last_modified'] = '2024-03-02T01:00:00';
326+
return $data;
327+
}
328+
);
329+
$etag4 = od_get_current_url_metrics_etag( $registry );
330+
$this->assertNotEquals( $etag3, $etag4 );
331+
$this->assertNotEquals( $captured_etag_data[ count( $captured_etag_data ) - 2 ], $captured_etag_data[ count( $captured_etag_data ) - 1 ] );
332+
$this->assertSame(
333+
array(
334+
'tag_visitors' => array( 'foo', 'bar', 'baz' ),
335+
'last_modified' => '2024-03-02T01:00:00',
336+
),
337+
$captured_etag_data[ count( $captured_etag_data ) - 1 ]
338+
);
339+
}
340+
289341
/**
290342
* Data provider.
291343
*

0 commit comments

Comments
 (0)