Skip to content

Commit

Permalink
Update test case test_get_common_lcp_element
Browse files Browse the repository at this point in the history
Signed-off-by: Shyamsundar Gadde <[email protected]>
  • Loading branch information
ShyamGadde committed Dec 7, 2024
1 parent 25cb61c commit 3540d9e
Showing 1 changed file with 102 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -721,45 +721,125 @@ public function test_get_groups_by_lcp_element(): void {
$this->assertNull( $group_collection->get_common_lcp_element() );
}

/**
* Data provider.
*
* @return array<string, mixed>
*/
public function data_provider_test_get_common_lcp_element(): array {
$xpath1 = '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]/*[1]';
$xpath2 = '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]/*[2]';

$get_sample_url_metric = function ( int $viewport_width, string $lcp_element_xpath, bool $is_lcp ): OD_URL_Metric {
return $this->get_sample_url_metric(
array(
'viewport_width' => $viewport_width,
'element' => array(
'isLCP' => $is_lcp,
'xpath' => $lcp_element_xpath,
),
)
);
};

return array(
'all_groups_have_common_lcp' => array(
'url_metrics' => array(
$get_sample_url_metric( 400, $xpath1, true ),
$get_sample_url_metric( 600, $xpath1, true ),
$get_sample_url_metric( 1000, $xpath1, true ),
),
'expected' => array(
'type' => OD_Element::class,
'xpath' => $xpath1,
),
),
'no_url_metrics' => array(
'url_metrics' => array(),
'expected' => null,
),
'empty_first_group' => array(
'url_metrics' => array(
$get_sample_url_metric( 600, $xpath1, true ),
$get_sample_url_metric( 1000, $xpath1, true ),
),
'expected' => null,
),
'empty_last_group' => array(
'url_metrics' => array(
$get_sample_url_metric( 400, $xpath1, true ),
$get_sample_url_metric( 600, $xpath1, true ),
),
'expected' => null,
),
'first_and_last_common_lcp_others_empty' => array(
'url_metrics' => array(
$get_sample_url_metric( 400, $xpath1, true ),
$get_sample_url_metric( 1000, $xpath1, true ),
),
'expected' => array(
'type' => OD_Element::class,
'xpath' => $xpath1,
),
),
'intermediate_groups_conflict' => array(
'url_metrics' => array(
$get_sample_url_metric( 400, $xpath1, true ),
$get_sample_url_metric( 600, $xpath2, true ),
$get_sample_url_metric( 1000, $xpath1, true ),
),
'expected' => null,
),
'first_and_last_lcp_mismatch' => array(
'url_metrics' => array(
$get_sample_url_metric( 400, $xpath1, true ),
$get_sample_url_metric( 600, $xpath1, true ),
$get_sample_url_metric( 1000, $xpath2, true ),
),
'expected' => null,
),
'no_lcp_metrics' => array(
'url_metrics' => array(
$get_sample_url_metric( 400, $xpath1, false ),
$get_sample_url_metric( 600, $xpath1, false ),
$get_sample_url_metric( 1000, $xpath1, false ),
),
'expected' => null,
),
);
}

/**
* Test get_common_lcp_element().
*
* @covers ::get_common_lcp_element
*
* @dataProvider data_provider_test_get_common_lcp_element
*
* @param OD_URL_Metric[] $url_metrics URL Metrics.
* @param mixed $expected Expected.
*/
public function test_get_common_lcp_element(): void {
public function test_get_common_lcp_element( array $url_metrics, $expected ): void {
$breakpoints = array( 480, 800 );
$sample_size = 3;
$current_etag = md5( '' );
$group_collection = new OD_URL_Metric_Group_Collection(
array(),
$url_metrics,
$current_etag,
$breakpoints,
$sample_size,
HOUR_IN_SECONDS
);

$lcp_element_xpath = '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]/*[1]';

foreach ( array_merge( $breakpoints, array( 1000 ) ) as $viewport_width ) {
for ( $i = 0; $i < $sample_size; $i++ ) {
$group_collection->add_url_metric(
$this->get_sample_url_metric(
array(
'viewport_width' => $viewport_width,
'element' => array(
'isLCP' => true,
'xpath' => $lcp_element_xpath,
),
)
)
);
}
}

$this->assertCount( 3, $group_collection );

$common_lcp_element = $group_collection->get_common_lcp_element();
$this->assertInstanceOf( OD_Element::class, $common_lcp_element );
$this->assertSame( $lcp_element_xpath, $common_lcp_element['xpath'] );
if ( is_array( $expected ) ) {
$this->assertInstanceOf( $expected['type'], $common_lcp_element );
$this->assertSame( $expected['xpath'], $common_lcp_element->get_xpath() );
} else {
$this->assertNull( $common_lcp_element );
}
}

/**
Expand Down

0 comments on commit 3540d9e

Please sign in to comment.