@@ -54,6 +54,78 @@ public function test_field_type_registry_has_default_types(): void {
5454 $ this ->assertTrue ( $ registry ->has_type ( 'datetime ' ) );
5555 }
5656
57+ public function test_field_type_registry_has_custom_types (): void {
58+ $ registry_1 = new FieldTypeRegistry ();
59+ $ registry_2 = new FieldTypeRegistry ();
60+
61+ $ registry_1 ->register_type ( 'custom_type_1 ' , [
62+ 'dataset ' => DataSet::TYPE_STRING ,
63+ 'sanitizer ' => fn ( $ value ) => ( $ value ),
64+ 'schema ' => [],
65+ 'input ' => 'hidden ' ,
66+ ] );
67+
68+ $ registry_2 ->register_type ( 'custom_type_2 ' , [
69+ 'dataset ' => DataSet::TYPE_INTEGER ,
70+ 'sanitizer ' => fn ( $ value ) => ( $ value ),
71+ 'schema ' => [],
72+ 'input ' => 'hidden ' ,
73+ ] );
74+
75+ $ this ->assertTrue ( $ registry_1 ->has_type ( 'custom_type_1 ' ) );
76+ $ this ->assertTrue ( $ registry_2 ->has_type ( 'custom_type_2 ' ) );
77+
78+ $ this ->assertFalse ( $ registry_1 ->has_type ( 'custom_type_2 ' ) );
79+ $ this ->assertFalse ( $ registry_2 ->has_type ( 'custom_type_1 ' ) );
80+
81+ foreach ( [
82+ $ registry_1 ,
83+ $ registry_2
84+ ] as $ registry ) {
85+ // Check defaults are not erased by custom
86+ $ this ->assertTrue ( $ registry ->has_type ( 'string ' ) );
87+ $ this ->assertTrue ( $ registry ->has_type ( 'text ' ) );
88+ $ this ->assertTrue ( $ registry ->has_type ( 'email ' ) );
89+ $ this ->assertTrue ( $ registry ->has_type ( 'url ' ) );
90+ $ this ->assertTrue ( $ registry ->has_type ( 'integer ' ) );
91+ $ this ->assertTrue ( $ registry ->has_type ( 'boolean ' ) );
92+ $ this ->assertTrue ( $ registry ->has_type ( 'date ' ) );
93+ $ this ->assertTrue ( $ registry ->has_type ( 'datetime ' ) );
94+ }
95+ }
96+
97+ public function test_dataview_can_use_field_registery_with_custom_types (): void {
98+ $ registry = new FieldTypeRegistry ();
99+ $ registry ->register_type ( 'custom_type ' , [
100+ 'dataset ' => DataSet::TYPE_STRING ,
101+ 'sanitizer ' => fn ( $ value ) => ( $ value ),
102+ 'schema ' => [],
103+ 'input ' => 'hidden ' ,
104+ ] );
105+
106+ $ config = [
107+ 'slug ' => 'dv_test_custom_field_type ' ,
108+ 'label ' => 'Item ' ,
109+ 'fields ' => [
110+ 'name ' => 'custom_type ' ,
111+ 'title ' => 'string ' ,
112+ ]
113+ ];
114+
115+ try {
116+ $ view_default_registry = new DataView ( $ config );
117+ $ this ->fail ( 'Expected InvalidArgumentException was not thrown ' );
118+ } catch ( \InvalidArgumentException $ e ) {
119+ $ this ->assertStringStartsWith (
120+ 'Unknown field type "custom_type" ' ,
121+ $ e ->getMessage ()
122+ );
123+ }
124+
125+ $ view_custom_registry = new DataView ( $ config , $ registry );
126+ $ this ->assertInstanceOf ( DataView::class, $ view_custom_registry );
127+ }
128+
57129 public function test_field_type_registry_maps_to_dataset_types (): void {
58130 $ registry = new FieldTypeRegistry ();
59131
0 commit comments