From 8c434c7862f00ffe68afefd9943ab20de491f57c Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 21 Dec 2023 17:02:44 -0800 Subject: [PATCH] Implement and scaffold tests --- app/Models/ReportTemplate.php | 15 ++++++++++----- tests/Unit/ReportTemplateTest.php | 32 ++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.php index 8c0fa6091b..2eeafa57ac 100644 --- a/app/Models/ReportTemplate.php +++ b/app/Models/ReportTemplate.php @@ -31,8 +31,8 @@ class ReportTemplate extends Model 'options' => 'array', ]; - //we will need a bit to catch and store the name of the report. - //for now the blip above is creating the name, but can be confusing if multiple are made at once + // we will need a bit to catch and store the name of the report. + // for now the blip above is creating the name, but can be confusing if multiple are made at once public function checkmarkValue(string $property): string { @@ -84,9 +84,9 @@ class ReportTemplate extends Model // @todo: I think this was added to support the null object pattern // @todo: Check if this is still needed and if so, add a test for it (testParsingSelectValues()). -// if ($this->options[$property] === [null]) { -// return null; -// } + // if ($this->options[$property] === [null]) { + // return null; + // } // If a model is provided then we should ensure we only return // the ids of models that exist and are not deleted. @@ -94,6 +94,11 @@ class ReportTemplate extends Model return $model::findMany($this->options[$property])->pluck('id'); } + // Wrap the value in an array if needed. + if (!is_array($this->options[$property])) { + return [$this->options[$property]]; + } + return $this->options[$property]; } diff --git a/tests/Unit/ReportTemplateTest.php b/tests/Unit/ReportTemplateTest.php index 5d930484d6..0a49120eb9 100644 --- a/tests/Unit/ReportTemplateTest.php +++ b/tests/Unit/ReportTemplateTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit; +use App\Models\Department; use App\Models\Location; use App\Models\ReportTemplate; use Tests\TestCase; @@ -81,7 +82,7 @@ class ReportTemplateTest extends TestCase $template = ReportTemplate::factory()->create([ 'options' => [ 'an_array' => ['2', '3', '4'], - 'an_empty_array'=> [], + 'an_empty_array' => [], 'an_array_containing_null' => [null], ], ]); @@ -147,12 +148,37 @@ class ReportTemplateTest extends TestCase public function testGracefullyHandlesSingleSelectBecomingMultiSelect() { - $this->markTestIncomplete(); + $department = Department::factory()->create(); + + // Given a report template saved with a property that is a string value + $templateWithValue = ReportTemplate::factory()->create([ + 'options' => ['single_value' => 'a string'], + ]); + + $templateWithModelId = ReportTemplate::factory()->create([ + 'options' => ['by_dept_id' => $department->id], + ]); + + $this->assertEquals(['a string'], $templateWithValue->selectValues('single_value')); + $this->assertContains($department->id, $templateWithModelId->selectValues('by_dept_id', Department::class)); } public function testGracefullyHandlesMultiSelectBecomingSingleSelect() { - $this->markTestIncomplete(); + // $this->markTestIncomplete(); + + [$departmentA, $departmentB] = Department::factory()->count(2)->create(); + + // Given a report template saved with a property that is an array of values + $templateWithValuesInArray = ReportTemplate::factory()->create([ + 'options' => ['array_of_values' => [1, 'a string']], + ]); + + $templateWithModelIdsInArray = ReportTemplate::factory()->create([ + 'options' => ['model_ids' => [$departmentA->id, $departmentB->id]], + ]); + + // @todo: Determine behavior...shoudl we return the first value? } public function testDeletedCustomFieldsDoNotCauseAnIssue()