Add unit tests for the different ways of providing field options
This commit is contained in:
parent
92fda9ce2d
commit
9dca130fae
|
|
@ -6,7 +6,28 @@ use October\Tests\Fixtures\Backend\Models\UserFixture;
|
|||
|
||||
class FormTestModel extends Model
|
||||
{
|
||||
public function modelCustomOptionsMethod()
|
||||
{
|
||||
return ['model', 'custom', 'options'];
|
||||
}
|
||||
|
||||
public function getFieldNameOnModelOptionsMethodOptions()
|
||||
{
|
||||
return ['model', 'field name', 'options method'];
|
||||
}
|
||||
|
||||
public function getDropdownOptions()
|
||||
{
|
||||
return ['dropdown', 'options'];
|
||||
}
|
||||
}
|
||||
|
||||
class FormHelper
|
||||
{
|
||||
public static function staticMethodOptions()
|
||||
{
|
||||
return ['static', 'method'];
|
||||
}
|
||||
}
|
||||
|
||||
class FormTest extends PluginTestCase
|
||||
|
|
@ -131,6 +152,61 @@ class FormTest extends PluginTestCase
|
|||
$this->assertEquals('[name="array[trigger][]"]', array_get($attributes, 'data-trigger'));
|
||||
}
|
||||
|
||||
public function testOptionsGeneration()
|
||||
{
|
||||
$form = new Form(null, [
|
||||
'model' => new FormTestModel,
|
||||
'arrayName' => 'array',
|
||||
'fields' => [
|
||||
'static_method_options' => [
|
||||
'type' => 'dropdown',
|
||||
'options' => '\FormHelper::staticMethodOptions',
|
||||
'expect' => ['static', 'method'],
|
||||
],
|
||||
'callable_options' => [
|
||||
'type' => 'dropdown',
|
||||
'options' => [\FormHelper::class, 'staticMethodOptions'],
|
||||
'expect' => ['static', 'method'],
|
||||
],
|
||||
'model_method_options' => [
|
||||
'type' => 'dropdown',
|
||||
'options' => 'modelCustomOptionsMethod',
|
||||
'expect' => ['model', 'custom', 'options'],
|
||||
],
|
||||
'defined_options' => [
|
||||
'type' => 'dropdown',
|
||||
'options' => ['value1', 'value2'],
|
||||
'expect' => ['value1', 'value2'],
|
||||
],
|
||||
'defined_options_key_value' => [
|
||||
'type' => 'dropdown',
|
||||
'options' => [
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
],
|
||||
'expect' => [
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
],
|
||||
],
|
||||
'field_name_on_model_options_method' => [
|
||||
'type' => 'dropdown',
|
||||
'expect' => ['model', 'field name', 'options method'],
|
||||
],
|
||||
'get_dropdown_options_method' => [
|
||||
'type' => 'dropdown',
|
||||
'expect' => ['dropdown', 'options'],
|
||||
],
|
||||
]
|
||||
]);
|
||||
|
||||
$form->render();
|
||||
|
||||
foreach ($form->getFields() as $name => $field) {
|
||||
$this->assertEquals($field->options(), $field->config['expect']);
|
||||
}
|
||||
}
|
||||
|
||||
protected function restrictedFormFixture(bool $singlePermission = false)
|
||||
{
|
||||
return new Form(null, [
|
||||
|
|
|
|||
Loading…
Reference in New Issue