Organize tests

This commit is contained in:
Marcus Moore 2024-10-28 14:24:47 -07:00
parent c881727747
commit d4cf392387
No known key found for this signature in database
6 changed files with 64 additions and 28 deletions

View file

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

View file

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

View file

@ -12,7 +12,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
->get(route('report-templates.edit', ReportTemplate::factory()->create()))
->get($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden();
}
@ -22,7 +22,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->create();
$this->actingAs($user)
->get(route('report-templates.edit', $reportTemplate))
->get($this->getRoute($reportTemplate))
->assertSessionHas('error')
->assertRedirect(route('reports/custom'));
}
@ -33,7 +33,12 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
$this->actingAs($user)
->get(route('report-templates.edit', $reportTemplate))
->get($this->getRoute($reportTemplate))
->assertOk();
}
private function getRoute(ReportTemplate $reportTemplate): string
{
return route('report-templates.edit', $reportTemplate);
}
}

View file

@ -12,21 +12,10 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
->get(route('reports/custom'))
->get($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden();
}
public function testCanLoadCustomReportPage()
{
$this->actingAs(User::factory()->canViewReports()->create())
->get(route('reports/custom'))
->assertOk()
->assertViewHas(['template' => function (ReportTemplate $template) {
// the view should have an empty report by default
return $template->exists() === false;
}]);
}
public function testCanLoadASavedReportTemplate()
{
$user = User::factory()->canViewReports()->create();
@ -34,7 +23,7 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
$user->reportTemplates()->save($reportTemplate);
$this->actingAs($user)
->get(route('report-templates.show', $reportTemplate))
->get($this->getRoute($reportTemplate))
->assertOk()
->assertViewHas(['template' => function (ReportTemplate $templatePassedToView) use ($reportTemplate) {
return $templatePassedToView->is($reportTemplate);
@ -46,8 +35,13 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
$reportTemplate = ReportTemplate::factory()->create();
$this->actingAs(User::factory()->canViewReports()->create())
->get(route('report-templates.show', $reportTemplate))
->get($this->getRoute($reportTemplate))
->assertSessionHas('error')
->assertRedirect(route('reports/custom'));
}
private function getRoute(ReportTemplate $reportTemplate): string
{
return route('report-templates.show', $reportTemplate);
}
}

View file

@ -12,14 +12,14 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
->post(route('report-templates.update', ReportTemplate::factory()->create()))
->post($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden();
}
public function testCannotUpdateAnotherUsersReportTemplate()
{
$this->actingAs(User::factory()->canViewReports()->create())
->post(route('report-templates.update', ReportTemplate::factory()->create()))
->post($this->getRoute(ReportTemplate::factory()->create()))
->assertSessionHas('error')
->assertRedirect(route('reports/custom'));
}
@ -39,7 +39,7 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
]);
$this->actingAs($user)
->post(route('report-templates.update', $reportTemplate), [
->post($this->getRoute($reportTemplate), [
'id' => 1,
'company' => 1,
'by_company_id' => [3],
@ -52,4 +52,9 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
$this->assertEquals(1, $reportTemplate->checkmarkValue('company'));
$this->assertEquals([3], $reportTemplate->selectValues('by_company_id'));
}
private function getRoute(ReportTemplate $reportTemplate): string
{
return route('report-templates.update', $reportTemplate);
}
}

View file

@ -4,13 +4,15 @@ namespace Tests\Feature\Reporting;
use App\Models\Asset;
use App\Models\Company;
use App\Models\ReportTemplate;
use App\Models\User;
use Illuminate\Testing\TestResponse;
use League\Csv\Reader;
use PHPUnit\Framework\Assert;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;
class CustomReportTest extends TestCase
class CustomReportTest extends TestCase implements TestsPermissionsRequirement
{
protected function setUp(): void
{
@ -43,6 +45,26 @@ class CustomReportTest extends TestCase
);
}
public function testRequiresPermission()
{
$this->actingAs(User::factory()->create())
->get(route('reports/custom'))
->assertForbidden();
}
public function testCanLoadCustomReportPage()
{
$this->actingAs(User::factory()->canViewReports()->create())
->get(route('reports/custom'))
->assertOk()
->assertViewHas([
'template' => function (ReportTemplate $template) {
// the view should have an empty report by default
return $template->exists() === false;
}
]);
}
public function testCustomAssetReport()
{
Asset::factory()->create(['name' => 'Asset A']);