Implement test

This commit is contained in:
Marcus Moore 2024-01-11 13:34:20 -08:00
parent 82df7a66ec
commit e5fb888d67
No known key found for this signature in database
2 changed files with 18 additions and 9 deletions

View file

@ -68,14 +68,21 @@ class ReportTemplate extends Model
return null; return null;
} }
$value = $this->options[$property];
if (is_array($value)) {
$value = $value[0];
}
// If a model is provided then we should ensure we only return // If a model is provided then we should ensure we only return
// the value if the model still exists. // the value if the model still exists.
if ($model) { if ($model) {
$foundModel = $model::find($this->options[$property]); $foundModel = $model::find($value);
return $foundModel ? $foundModel->id : null; return $foundModel ? $foundModel->id : null;
} }
return $this->options[$property] ?? null;
return $value;
} }
public function selectValues(string $property, string $model = null): iterable public function selectValues(string $property, string $model = null): iterable

View file

@ -173,22 +173,24 @@ class ReportTemplateTest extends TestCase
$this->assertContains($department->id, $templateWithModelId->selectValues('by_dept_id', Department::class)); $this->assertContains($department->id, $templateWithModelId->selectValues('by_dept_id', Department::class));
} }
public function testGracefullyHandlesMultiSelectBecomingSingleSelect() public function testGracefullyHandlesMultiSelectBecomingSingleSelectBySelectingTheFirstValue()
{ {
$this->markTestIncomplete();
[$departmentA, $departmentB] = Department::factory()->count(2)->create(); [$departmentA, $departmentB] = Department::factory()->count(2)->create();
// Given a report template saved with a property that is an array of values // Given report templates saved with a property that is an array of values
$templateWithValuesInArray = ReportTemplate::factory()->create([ $templateWithValuesInArray = ReportTemplate::factory()->create([
'options' => ['array_of_values' => [1, 'a string']], 'options' => ['array_of_values' => [3, 'a string']],
]); ]);
$templateWithModelIdsInArray = ReportTemplate::factory()->create([ $templateWithModelIdsInArray = ReportTemplate::factory()->create([
'options' => ['model_ids' => [$departmentA->id, $departmentB->id]], 'options' => ['array_of_model_ids' => [$departmentA->id, $departmentB->id]],
]); ]);
// @todo: Determine behavior...shoudl we return the first value? $this->assertEquals(3, $templateWithValuesInArray->selectValue('array_of_values'));
$this->assertEquals(
$departmentA->id,
$templateWithModelIdsInArray->selectValue('array_of_model_ids', Department::class)
);
} }
public function testOldValuesStillWorkAfterTheseChanges() public function testOldValuesStillWorkAfterTheseChanges()