Skip to content

Commit 8a026c9

Browse files
Field Type Registry: Share custom types across instances (#34)
* Field Type Registry: Share custom types across instances * Field Type Registry: Revert shared custom types and give possibility to set custom FieldTypeRegistry in DataView * Tests: Field Type Registry - Update tests according to last changes * Field Type Registry: Remove uneeded condition in constructor (added for previous approach)
1 parent 51c473e commit 8a026c9

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

src/DataView/DataView.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class DataView {
7070
* - ui: array - WordPress admin UI configuration
7171
* - capability: string - required capability (default: 'manage_options')
7272
*/
73-
public function __construct( array $config ) {
74-
$this->registry = new FieldTypeRegistry();
73+
public function __construct( array $config, ?FieldTypeRegistry $registry = null ) {
74+
$this->registry = $registry ?? new FieldTypeRegistry();
7575
$this->label_generator = new LabelGenerator();
7676
$this->schema_generator = new SchemaGenerator( $this->registry );
7777

tests/phpunit/data-view.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)