From e5fb888d677f414f9e70a1c7dcad80ff285ce574 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 11 Jan 2024 13:34:20 -0800 Subject: [PATCH] Implement test --- app/Models/ReportTemplate.php | 11 +++++++++-- tests/Unit/ReportTemplateTest.php | 16 +++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.php index 73909d045..b1da5d8d1 100644 --- a/app/Models/ReportTemplate.php +++ b/app/Models/ReportTemplate.php @@ -68,14 +68,21 @@ class ReportTemplate extends Model 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 // the value if the model still exists. if ($model) { - $foundModel = $model::find($this->options[$property]); + $foundModel = $model::find($value); return $foundModel ? $foundModel->id : null; } - return $this->options[$property] ?? null; + + return $value; } public function selectValues(string $property, string $model = null): iterable diff --git a/tests/Unit/ReportTemplateTest.php b/tests/Unit/ReportTemplateTest.php index 216e2d3fb..ab4573f9d 100644 --- a/tests/Unit/ReportTemplateTest.php +++ b/tests/Unit/ReportTemplateTest.php @@ -173,22 +173,24 @@ class ReportTemplateTest extends TestCase $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(); - // 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([ - 'options' => ['array_of_values' => [1, 'a string']], + 'options' => ['array_of_values' => [3, 'a string']], ]); $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()