Skip to content

Commit 3540d9e

Browse files
committed
Update test case test_get_common_lcp_element
Signed-off-by: Shyamsundar Gadde <[email protected]>
1 parent 25cb61c commit 3540d9e

File tree

1 file changed

+102
-22
lines changed

1 file changed

+102
-22
lines changed

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

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -721,45 +721,125 @@ public function test_get_groups_by_lcp_element(): void {
721721
$this->assertNull( $group_collection->get_common_lcp_element() );
722722
}
723723

724+
/**
725+
* Data provider.
726+
*
727+
* @return array<string, mixed>
728+
*/
729+
public function data_provider_test_get_common_lcp_element(): array {
730+
$xpath1 = '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]/*[1]';
731+
$xpath2 = '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]/*[2]';
732+
733+
$get_sample_url_metric = function ( int $viewport_width, string $lcp_element_xpath, bool $is_lcp ): OD_URL_Metric {
734+
return $this->get_sample_url_metric(
735+
array(
736+
'viewport_width' => $viewport_width,
737+
'element' => array(
738+
'isLCP' => $is_lcp,
739+
'xpath' => $lcp_element_xpath,
740+
),
741+
)
742+
);
743+
};
744+
745+
return array(
746+
'all_groups_have_common_lcp' => array(
747+
'url_metrics' => array(
748+
$get_sample_url_metric( 400, $xpath1, true ),
749+
$get_sample_url_metric( 600, $xpath1, true ),
750+
$get_sample_url_metric( 1000, $xpath1, true ),
751+
),
752+
'expected' => array(
753+
'type' => OD_Element::class,
754+
'xpath' => $xpath1,
755+
),
756+
),
757+
'no_url_metrics' => array(
758+
'url_metrics' => array(),
759+
'expected' => null,
760+
),
761+
'empty_first_group' => array(
762+
'url_metrics' => array(
763+
$get_sample_url_metric( 600, $xpath1, true ),
764+
$get_sample_url_metric( 1000, $xpath1, true ),
765+
),
766+
'expected' => null,
767+
),
768+
'empty_last_group' => array(
769+
'url_metrics' => array(
770+
$get_sample_url_metric( 400, $xpath1, true ),
771+
$get_sample_url_metric( 600, $xpath1, true ),
772+
),
773+
'expected' => null,
774+
),
775+
'first_and_last_common_lcp_others_empty' => array(
776+
'url_metrics' => array(
777+
$get_sample_url_metric( 400, $xpath1, true ),
778+
$get_sample_url_metric( 1000, $xpath1, true ),
779+
),
780+
'expected' => array(
781+
'type' => OD_Element::class,
782+
'xpath' => $xpath1,
783+
),
784+
),
785+
'intermediate_groups_conflict' => array(
786+
'url_metrics' => array(
787+
$get_sample_url_metric( 400, $xpath1, true ),
788+
$get_sample_url_metric( 600, $xpath2, true ),
789+
$get_sample_url_metric( 1000, $xpath1, true ),
790+
),
791+
'expected' => null,
792+
),
793+
'first_and_last_lcp_mismatch' => array(
794+
'url_metrics' => array(
795+
$get_sample_url_metric( 400, $xpath1, true ),
796+
$get_sample_url_metric( 600, $xpath1, true ),
797+
$get_sample_url_metric( 1000, $xpath2, true ),
798+
),
799+
'expected' => null,
800+
),
801+
'no_lcp_metrics' => array(
802+
'url_metrics' => array(
803+
$get_sample_url_metric( 400, $xpath1, false ),
804+
$get_sample_url_metric( 600, $xpath1, false ),
805+
$get_sample_url_metric( 1000, $xpath1, false ),
806+
),
807+
'expected' => null,
808+
),
809+
);
810+
}
811+
724812
/**
725813
* Test get_common_lcp_element().
726814
*
727815
* @covers ::get_common_lcp_element
816+
*
817+
* @dataProvider data_provider_test_get_common_lcp_element
818+
*
819+
* @param OD_URL_Metric[] $url_metrics URL Metrics.
820+
* @param mixed $expected Expected.
728821
*/
729-
public function test_get_common_lcp_element(): void {
822+
public function test_get_common_lcp_element( array $url_metrics, $expected ): void {
730823
$breakpoints = array( 480, 800 );
731824
$sample_size = 3;
732825
$current_etag = md5( '' );
733826
$group_collection = new OD_URL_Metric_Group_Collection(
734-
array(),
827+
$url_metrics,
735828
$current_etag,
736829
$breakpoints,
737830
$sample_size,
738831
HOUR_IN_SECONDS
739832
);
740833

741-
$lcp_element_xpath = '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]/*[1]';
742-
743-
foreach ( array_merge( $breakpoints, array( 1000 ) ) as $viewport_width ) {
744-
for ( $i = 0; $i < $sample_size; $i++ ) {
745-
$group_collection->add_url_metric(
746-
$this->get_sample_url_metric(
747-
array(
748-
'viewport_width' => $viewport_width,
749-
'element' => array(
750-
'isLCP' => true,
751-
'xpath' => $lcp_element_xpath,
752-
),
753-
)
754-
)
755-
);
756-
}
757-
}
758-
759834
$this->assertCount( 3, $group_collection );
835+
760836
$common_lcp_element = $group_collection->get_common_lcp_element();
761-
$this->assertInstanceOf( OD_Element::class, $common_lcp_element );
762-
$this->assertSame( $lcp_element_xpath, $common_lcp_element['xpath'] );
837+
if ( is_array( $expected ) ) {
838+
$this->assertInstanceOf( $expected['type'], $common_lcp_element );
839+
$this->assertSame( $expected['xpath'], $common_lcp_element->get_xpath() );
840+
} else {
841+
$this->assertNull( $common_lcp_element );
842+
}
763843
}
764844

765845
/**

0 commit comments

Comments
 (0)