Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DataView/DataView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
72 changes: 72 additions & 0 deletions tests/phpunit/data-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down