Implement and scaffold tests

This commit is contained in:
Marcus Moore 2023-12-21 17:02:44 -08:00
parent 7f153b32e4
commit 8c434c7862
No known key found for this signature in database
2 changed files with 39 additions and 8 deletions

View file

@ -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];
}

View file

@ -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()