A free open source IT asset/license management system
Implement some tests
Marcus Moore691e81d827f0
diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.phpindex 0ec2f854c4..3cc90d3c7e 100644--- a/app/Models/ReportTemplate.php+++ b/app/Models/ReportTemplate.php@@ -5,6 +5,7 @@ namespace App\Models;use Illuminate\Database\Eloquent\Builder;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;+use Illuminate\Database\Eloquent\Relations\BelongsTo;use Watson\Validating\ValidatingTrait;class ReportTemplate extends Model@@ -42,6 +43,11 @@ class ReportTemplate extends Model});}+ public function user(): BelongsTo+ {+ return $this->belongsTo(User::class);+ }+public function checkmarkValue(string $property): string{// Assuming we're using the null object pattern,diff --git a/database/factories/ReportTemplateFactory.php b/database/factories/ReportTemplateFactory.phpindex bb5cac5963..86f861f0ce 100644--- a/database/factories/ReportTemplateFactory.php+++ b/database/factories/ReportTemplateFactory.php@@ -15,7 +15,9 @@ class ReportTemplateFactory extends Factory{return ['name' => $this->faker->word(),- 'options' => [],+ 'options' => [+ 'id' => '1',+ ],];}}diff --git a/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php b/tests/Feature/ReportTemplates/DeleteReportTemplateTest.phpindex 5ff0194915..c015e74450 100644--- a/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php+++ b/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php@@ -1,7 +1,9 @@<?php-namespace Feature\ReportTemplates;+namespace Tests\Feature\ReportTemplates;+use App\Models\ReportTemplate;+use App\Models\User;use Tests\Support\InteractsWithSettings;use Tests\TestCase;@@ -11,11 +13,20 @@ class DeleteReportTemplateTest extends TestCasepublic function testDeletingReportTemplateRequiresCorrectPermission(){- $this->markTestIncomplete();+ $this->actingAs(User::factory()->create())+ ->post(route('report-templates.destroy', 1))+ ->assertForbidden();}public function testCanDeleteAReportTemplate(){- $this->markTestIncomplete();+ $user = User::factory()->canViewReports()->create();+ $reportTemplate = ReportTemplate::factory()->for($user)->create();++ $this->actingAs($user)+ ->delete(route('report-templates.destroy', $reportTemplate))+ ->assertRedirect(route('reports/custom'));++ $this->assertFalse($reportTemplate->exists());}}diff --git a/tests/Feature/ReportTemplates/ShowReportTemplateTest.php b/tests/Feature/ReportTemplates/ShowReportTemplateTest.phpindex 213bc32c88..02036d7b9a 100644--- a/tests/Feature/ReportTemplates/ShowReportTemplateTest.php+++ b/tests/Feature/ReportTemplates/ShowReportTemplateTest.php@@ -1,6 +1,6 @@<?php-namespace Feature\ReportTemplates;+namespace Tests\Feature\ReportTemplates;use App\Models\ReportTemplate;use App\Models\User;diff --git a/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php b/tests/Feature/ReportTemplates/UpdateReportTemplateTest.phpindex f8ef208f07..f979263f19 100644--- a/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php+++ b/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php@@ -1,6 +1,6 @@<?php-namespace Feature\ReportTemplates;+namespace Tests\Feature\ReportTemplates;use App\Models\ReportTemplate;use App\Models\User;@@ -13,12 +13,29 @@ class UpdateReportTemplateTest extends TestCasepublic function testUpdatingReportTemplateRequiresCorrectPermission(){- $this->markTestIncomplete();+ $this->actingAs(User::factory()->create())+ ->post(route('report-templates.update', 1))+ ->assertForbidden();+ }++ public function testCanLoadEditReportTemplatePage()+ {+ $user = User::factory()->canViewReports()->create();+ $reportTemplate = ReportTemplate::factory()->for($user)->create();++ $this->actingAs($user)+ ->get(route('report-templates.edit', $reportTemplate))+ ->assertOk();}public function testUpdatingReportTemplateRequiresValidFields(){$this->markTestIncomplete();++ $this->actingAs(User::factory()->canViewReports()->create())+ ->post(route('report-templates.update', ReportTemplate::factory()->create()))+ // @todo: name isn't being passed in this case+ ->assertSessionHasErrors('name');}public function testCanUpdateAReportTemplate()@@ -27,12 +44,23 @@ class UpdateReportTemplateTest extends TestCase$user = User::factory()->canViewReports()->create();- $reportTemplate = ReportTemplate::factory()->for($user)->create();+ $reportTemplate = ReportTemplate::factory()->for($user)->create([+ 'options' => [+ 'id' => 1,+ 'company' => 1,+ 'by_company_id' => [1, 2],+ ],+ ]);$this->actingAs($user)- ->get(route('report-templates.edit', $reportTemplate))+ ->post(route('report-templates.update', $reportTemplate), [+ 'id' => 1,+ 'by_company_id' => [3],+ ])->assertOk();// @todo:+ $reportTemplate->fresh();+ dd($reportTemplate->options);}}