diff --git a/src/DataView/DataView.php b/src/DataView/DataView.php index 0ea5ef2..e418e62 100644 --- a/src/DataView/DataView.php +++ b/src/DataView/DataView.php @@ -70,8 +70,8 @@ class DataView { * - ui: array - WordPress admin UI configuration * - capability: string - required capability (default: 'manage_options') */ - public function __construct( array $config ) { - $this->registry = new FieldTypeRegistry(); + public function __construct( array $config, ?FieldTypeRegistry $registry = null ) { + $this->registry = $registry ?? new FieldTypeRegistry(); $this->label_generator = new LabelGenerator(); $this->schema_generator = new SchemaGenerator( $this->registry ); diff --git a/tests/phpunit/data-view.php b/tests/phpunit/data-view.php index 4926629..ff79edf 100644 --- a/tests/phpunit/data-view.php +++ b/tests/phpunit/data-view.php @@ -54,6 +54,78 @@ public function test_field_type_registry_has_default_types(): void { $this->assertTrue( $registry->has_type( 'datetime' ) ); } + public function test_field_type_registry_has_custom_types(): void { + $registry_1 = new FieldTypeRegistry(); + $registry_2 = new FieldTypeRegistry(); + + $registry_1->register_type( 'custom_type_1', [ + 'dataset' => DataSet::TYPE_STRING, + 'sanitizer' => fn( $value ) => ( $value ), + 'schema' => [], + 'input' => 'hidden', + ] ); + + $registry_2->register_type( 'custom_type_2', [ + 'dataset' => DataSet::TYPE_INTEGER, + 'sanitizer' => fn( $value ) => ( $value ), + 'schema' => [], + 'input' => 'hidden', + ] ); + + $this->assertTrue( $registry_1->has_type( 'custom_type_1' ) ); + $this->assertTrue( $registry_2->has_type( 'custom_type_2' ) ); + + $this->assertFalse( $registry_1->has_type( 'custom_type_2' ) ); + $this->assertFalse( $registry_2->has_type( 'custom_type_1' ) ); + + foreach( [ + $registry_1, + $registry_2 + ] as $registry ) { + // Check defaults are not erased by custom + $this->assertTrue( $registry->has_type( 'string' ) ); + $this->assertTrue( $registry->has_type( 'text' ) ); + $this->assertTrue( $registry->has_type( 'email' ) ); + $this->assertTrue( $registry->has_type( 'url' ) ); + $this->assertTrue( $registry->has_type( 'integer' ) ); + $this->assertTrue( $registry->has_type( 'boolean' ) ); + $this->assertTrue( $registry->has_type( 'date' ) ); + $this->assertTrue( $registry->has_type( 'datetime' ) ); + } + } + + public function test_dataview_can_use_field_registery_with_custom_types(): void { + $registry = new FieldTypeRegistry(); + $registry->register_type( 'custom_type', [ + 'dataset' => DataSet::TYPE_STRING, + 'sanitizer' => fn( $value ) => ( $value ), + 'schema' => [], + 'input' => 'hidden', + ] ); + + $config = [ + 'slug' => 'dv_test_custom_field_type', + 'label' => 'Item', + 'fields' => [ + 'name' => 'custom_type', + 'title' => 'string', + ] + ]; + + try { + $view_default_registry = new DataView( $config ); + $this->fail( 'Expected InvalidArgumentException was not thrown' ); + } catch ( \InvalidArgumentException $e ) { + $this->assertStringStartsWith( + 'Unknown field type "custom_type"', + $e->getMessage() + ); + } + + $view_custom_registry = new DataView( $config, $registry ); + $this->assertInstanceOf( DataView::class, $view_custom_registry ); + } + public function test_field_type_registry_maps_to_dataset_types(): void { $registry = new FieldTypeRegistry();