Skip to content

Commit be2086d

Browse files
committed
Improve test coverage of OD_URL_Metric and OD_Tag_Visitor_Registry
1 parent af9182b commit be2086d

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

plugins/optimization-detective/tests/test-class-od-tag-visitor-registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function test(): void {
2424

2525
// Add img visitor.
2626
$this->assertFalse( $registry->is_registered( 'img' ) );
27+
$this->assertNull( $registry->get_registered( 'img' ) );
2728
$img_visitor = static function ( OD_Tag_Visitor_Context $context ) {
2829
return $context->processor->get_tag() === 'IMG';
2930
};

plugins/optimization-detective/tests/test-class-od-url-metric.php

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,13 @@ public function data_provider_to_test_constructor_with_extended_schema(): array
368368
'boundingClientRect' => $this->get_sample_dom_rect(),
369369
);
370370

371+
$data = array(
372+
'url' => home_url( '/' ),
373+
'viewport' => $viewport,
374+
'timestamp' => microtime( true ),
375+
'elements' => array( $valid_element ),
376+
);
377+
371378
return array(
372379
'added_valid_root_property_populated' => array(
373380
'set_up' => static function (): void {
@@ -550,6 +557,112 @@ static function ( array $properties ): array {
550557
'assert' => static function (): void {},
551558
'error' => 'OD_URL_Metric[elements][0][isColorful] is not of type boolean.',
552559
),
560+
561+
'added_immutable_element_property' => array(
562+
'set_up' => static function (): void {
563+
add_filter(
564+
'od_url_metric_schema_element_item_additional_properties',
565+
static function ( array $properties ): array {
566+
$properties['isLCP'] = array(
567+
'type' => 'string',
568+
);
569+
return $properties;
570+
}
571+
);
572+
},
573+
'data' => $data,
574+
'assert' => static function (): void {},
575+
'error' => '',
576+
'wrong' => 'Filter: 'od_url_metric_schema_element_item_additional_properties'',
577+
),
578+
579+
'added_element_property_without_type' => array(
580+
'set_up' => static function (): void {
581+
add_filter(
582+
'od_url_metric_schema_element_item_additional_properties',
583+
static function ( array $properties ): array {
584+
$properties['foo'] = array(
585+
'minimum' => 1,
586+
);
587+
return $properties;
588+
}
589+
);
590+
},
591+
'data' => $data,
592+
'assert' => function (): void {
593+
$this->assertArrayHasKey( 'isLCP', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties'] );
594+
$this->assertArrayNotHasKey( 'foo', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties'] );
595+
},
596+
'error' => '',
597+
'wrong' => 'Filter: 'od_url_metric_schema_element_item_additional_properties'',
598+
),
599+
600+
'added_element_property_with_invalid_type' => array(
601+
'set_up' => static function (): void {
602+
add_filter(
603+
'od_url_metric_schema_element_item_additional_properties',
604+
static function ( array $properties ): array {
605+
$properties['foo'] = array(
606+
'type' => null,
607+
);
608+
return $properties;
609+
}
610+
);
611+
},
612+
'data' => $data,
613+
'assert' => function (): void {
614+
$this->assertArrayHasKey( 'isLCP', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties'] );
615+
$this->assertArrayNotHasKey( 'foo', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties'] );
616+
},
617+
'error' => '',
618+
'wrong' => 'Filter: 'od_url_metric_schema_element_item_additional_properties'',
619+
),
620+
621+
'added_element_property_with_required' => array(
622+
'set_up' => static function (): void {
623+
add_filter(
624+
'od_url_metric_schema_element_item_additional_properties',
625+
static function ( array $properties ): array {
626+
$properties['foo'] = array(
627+
'type' => 'string',
628+
'required' => true,
629+
);
630+
return $properties;
631+
}
632+
);
633+
},
634+
'data' => $data,
635+
'assert' => function (): void {
636+
$this->assertArrayHasKey( 'isLCP', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties'] );
637+
$this->assertArrayHasKey( 'foo', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties'] );
638+
$this->assertFalse( OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties']['foo']['required'] );
639+
},
640+
'error' => '',
641+
'wrong' => 'Filter: 'od_url_metric_schema_element_item_additional_properties'',
642+
),
643+
644+
'added_element_property_invalid_default' => array(
645+
'set_up' => static function (): void {
646+
add_filter(
647+
'od_url_metric_schema_element_item_additional_properties',
648+
static function ( array $properties ): array {
649+
$properties['foo'] = array(
650+
'type' => 'string',
651+
'default' => 'bard',
652+
);
653+
return $properties;
654+
}
655+
);
656+
},
657+
'data' => $data,
658+
'assert' => function (): void {
659+
$this->assertArrayHasKey( 'isLCP', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties'] );
660+
$this->assertArrayHasKey( 'foo', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties'] );
661+
$this->assertArrayNotHasKey( 'default', OD_URL_Metric::get_json_schema()['properties']['elements']['items']['properties']['foo'] );
662+
},
663+
'error' => '',
664+
'wrong' => 'Filter: 'od_url_metric_schema_element_item_additional_properties'',
665+
),
553666
);
554667
}
555668

@@ -565,12 +678,16 @@ static function ( array $properties ): array {
565678
* @param array<string, mixed> $data Data.
566679
* @param Closure $assert Assert.
567680
* @param string $error Error.
681+
* @param string $wrong Expected doing it wrong.
568682
*/
569-
public function test_constructor_with_extended_schema( Closure $set_up, array $data, Closure $assert, string $error = '' ): void {
683+
public function test_constructor_with_extended_schema( Closure $set_up, array $data, Closure $assert, string $error = '', string $wrong = '' ): void {
570684
if ( '' !== $error ) {
571685
$this->expectException( OD_Data_Validation_Exception::class );
572686
$this->expectExceptionMessage( $error );
573687
}
688+
if ( '' !== $wrong ) {
689+
$this->setExpectedIncorrectUsage( $wrong );
690+
}
574691
$url_metric_sans_extended_schema = new OD_URL_Metric( $data );
575692
$set_up();
576693
$url_metric_with_extended_schema = new OD_URL_Metric( $data );

0 commit comments

Comments
 (0)