Inline route helpers in tests

This commit is contained in:
Marcus Moore 2024-11-07 16:54:55 -08:00
parent b8265d54bb
commit dc0b8c7572
No known key found for this signature in database
5 changed files with 17 additions and 42 deletions

View file

@ -16,7 +16,7 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
$reportTemplate = ReportTemplate::factory()->create(); $reportTemplate = ReportTemplate::factory()->create();
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->post($this->getRoute($reportTemplate)) ->post(route('report-templates.destroy', $reportTemplate->id))
->assertNotFound(); ->assertNotFound();
$this->assertModelExists($reportTemplate); $this->assertModelExists($reportTemplate);
@ -27,7 +27,7 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
$reportTemplate = ReportTemplate::factory()->create(); $reportTemplate = ReportTemplate::factory()->create();
$this->actingAs(User::factory()->canViewReports()->create()) $this->actingAs(User::factory()->canViewReports()->create())
->delete($this->getRoute($reportTemplate)) ->delete(route('report-templates.destroy', $reportTemplate->id))
->assertNotFound(); ->assertNotFound();
$this->assertModelExists($reportTemplate); $this->assertModelExists($reportTemplate);
@ -39,14 +39,9 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
$reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create(); $reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
$this->actingAs($user) $this->actingAs($user)
->delete($this->getRoute($reportTemplate)) ->delete(route('report-templates.destroy', $reportTemplate->id))
->assertRedirect(route('reports/custom')); ->assertRedirect(route('reports/custom'));
$this->assertSoftDeleted($reportTemplate); $this->assertSoftDeleted($reportTemplate);
} }
private function getRoute(ReportTemplate $reportTemplate): string
{
return route('report-templates.destroy', $reportTemplate->id);
}
} }

View file

@ -14,7 +14,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
public function testRequiresPermission() public function testRequiresPermission()
{ {
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->get($this->getRoute(ReportTemplate::factory()->create())) ->get(route('report-templates.edit', ReportTemplate::factory()->create()))
->assertNotFound(); ->assertNotFound();
} }
@ -24,7 +24,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->create(); $reportTemplate = ReportTemplate::factory()->create();
$this->actingAs($user) $this->actingAs($user)
->get($this->getRoute($reportTemplate)) ->get(route('report-templates.edit', $reportTemplate))
->assertNotFound(); ->assertNotFound();
} }
@ -34,12 +34,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create(); $reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
$this->actingAs($user) $this->actingAs($user)
->get($this->getRoute($reportTemplate)) ->get(route('report-templates.edit', $reportTemplate))
->assertOk(); ->assertOk();
} }
private function getRoute(ReportTemplate $reportTemplate): string
{
return route('report-templates.edit', $reportTemplate);
}
} }

View file

@ -14,7 +14,7 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
public function testRequiresPermission() public function testRequiresPermission()
{ {
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->get($this->getRoute(ReportTemplate::factory()->create())) ->get(route('report-templates.show', ReportTemplate::factory()->create()))
->assertNotFound(); ->assertNotFound();
} }
@ -25,7 +25,7 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
$user->reportTemplates()->save($reportTemplate); $user->reportTemplates()->save($reportTemplate);
$this->actingAs($user) $this->actingAs($user)
->get($this->getRoute($reportTemplate)) ->get(route('report-templates.show', $reportTemplate))
->assertOk() ->assertOk()
->assertViewHas(['template' => function (ReportTemplate $templatePassedToView) use ($reportTemplate) { ->assertViewHas(['template' => function (ReportTemplate $templatePassedToView) use ($reportTemplate) {
return $templatePassedToView->is($reportTemplate); return $templatePassedToView->is($reportTemplate);
@ -37,12 +37,7 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->create(); $reportTemplate = ReportTemplate::factory()->create();
$this->actingAs(User::factory()->canViewReports()->create()) $this->actingAs(User::factory()->canViewReports()->create())
->get($this->getRoute($reportTemplate)) ->get(route('report-templates.show', $reportTemplate))
->assertNotFound(); ->assertNotFound();
} }
private function getRoute(ReportTemplate $reportTemplate): string
{
return route('report-templates.show', $reportTemplate);
}
} }

View file

@ -14,14 +14,14 @@ class StoreReportTemplateTest extends TestCase implements TestsPermissionsRequir
public function testRequiresPermission() public function testRequiresPermission()
{ {
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->post($this->getRoute()) ->post(route('report-templates.store'))
->assertForbidden(); ->assertForbidden();
} }
public function testSavingReportTemplateRequiresValidFields() public function testSavingReportTemplateRequiresValidFields()
{ {
$this->actingAs(User::factory()->canViewReports()->create()) $this->actingAs(User::factory()->canViewReports()->create())
->post($this->getRoute(), [ ->post(route('report-templates.store'), [
'name' => '', 'name' => '',
]) ])
->assertSessionHasErrors('name'); ->assertSessionHasErrors('name');
@ -33,7 +33,7 @@ class StoreReportTemplateTest extends TestCase implements TestsPermissionsRequir
// start on the custom report page // start on the custom report page
->from(route('reports/custom')) ->from(route('reports/custom'))
->followingRedirects() ->followingRedirects()
->post($this->getRoute(), [ ->post(route('report-templates.store'), [
'name' => '', 'name' => '',
// set some values to ensure they are still present // set some values to ensure they are still present
// when returning to the custom report page. // when returning to the custom report page.
@ -48,7 +48,7 @@ class StoreReportTemplateTest extends TestCase implements TestsPermissionsRequir
$user = User::factory()->canViewReports()->create(); $user = User::factory()->canViewReports()->create();
$this->actingAs($user) $this->actingAs($user)
->post($this->getRoute(), [ ->post(route('report-templates.store'), [
'name' => 'My Awesome Template', 'name' => 'My Awesome Template',
'company' => '1', 'company' => '1',
'by_company_id' => ['1', '2'], 'by_company_id' => ['1', '2'],
@ -63,9 +63,4 @@ class StoreReportTemplateTest extends TestCase implements TestsPermissionsRequir
$this->assertEquals('1', $template->options['company']); $this->assertEquals('1', $template->options['company']);
$this->assertEquals(['1', '2'], $template->options['by_company_id']); $this->assertEquals(['1', '2'], $template->options['by_company_id']);
} }
private function getRoute(): string
{
return route('report-templates.store');
}
} }

View file

@ -14,14 +14,14 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
public function testRequiresPermission() public function testRequiresPermission()
{ {
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->post($this->getRoute(ReportTemplate::factory()->create())) ->post(route('report-templates.update', ReportTemplate::factory()->create()))
->assertNotFound(); ->assertNotFound();
} }
public function testCannotUpdateAnotherUsersReportTemplate() public function testCannotUpdateAnotherUsersReportTemplate()
{ {
$this->actingAs(User::factory()->canViewReports()->create()) $this->actingAs(User::factory()->canViewReports()->create())
->post($this->getRoute(ReportTemplate::factory()->create())) ->post(route('report-templates.update', ReportTemplate::factory()->create()))
->assertNotFound(); ->assertNotFound();
} }
@ -32,7 +32,7 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
$reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create(); $reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
$this->actingAs($user) $this->actingAs($user)
->post($this->getRoute($reportTemplate), [ ->post(route('report-templates.update', $reportTemplate), [
// //
]) ])
->assertSessionHasErrors([ ->assertSessionHasErrors([
@ -56,7 +56,7 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
]); ]);
$this->actingAs($user) $this->actingAs($user)
->post($this->getRoute($reportTemplate), [ ->post(route('report-templates.update', $reportTemplate), [
'name' => 'Updated Name', 'name' => 'Updated Name',
'id' => 1, 'id' => 1,
'company' => 1, 'company' => 1,
@ -71,9 +71,4 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
$this->assertEquals(1, $reportTemplate->checkmarkValue('company')); $this->assertEquals(1, $reportTemplate->checkmarkValue('company'));
$this->assertEquals([3], $reportTemplate->selectValues('by_company_id')); $this->assertEquals([3], $reportTemplate->selectValues('by_company_id'));
} }
private function getRoute(ReportTemplate $reportTemplate): string
{
return route('report-templates.update', $reportTemplate);
}
} }