@@ -368,6 +368,13 @@ public function data_provider_to_test_constructor_with_extended_schema(): array
368
368
'boundingClientRect ' => $ this ->get_sample_dom_rect (),
369
369
);
370
370
371
+ $ data = array (
372
+ 'url ' => home_url ( '/ ' ),
373
+ 'viewport ' => $ viewport ,
374
+ 'timestamp ' => microtime ( true ),
375
+ 'elements ' => array ( $ valid_element ),
376
+ );
377
+
371
378
return array (
372
379
'added_valid_root_property_populated ' => array (
373
380
'set_up ' => static function (): void {
@@ -550,6 +557,112 @@ static function ( array $properties ): array {
550
557
'assert ' => static function (): void {},
551
558
'error ' => 'OD_URL_Metric[elements][0][isColorful] is not of type boolean. ' ,
552
559
),
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
+ ),
553
666
);
554
667
}
555
668
@@ -565,12 +678,16 @@ static function ( array $properties ): array {
565
678
* @param array<string, mixed> $data Data.
566
679
* @param Closure $assert Assert.
567
680
* @param string $error Error.
681
+ * @param string $wrong Expected doing it wrong.
568
682
*/
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 {
570
684
if ( '' !== $ error ) {
571
685
$ this ->expectException ( OD_Data_Validation_Exception::class );
572
686
$ this ->expectExceptionMessage ( $ error );
573
687
}
688
+ if ( '' !== $ wrong ) {
689
+ $ this ->setExpectedIncorrectUsage ( $ wrong );
690
+ }
574
691
$ url_metric_sans_extended_schema = new OD_URL_Metric ( $ data );
575
692
$ set_up ();
576
693
$ url_metric_with_extended_schema = new OD_URL_Metric ( $ data );
0 commit comments