Roman_applicant-site/tests/Feature/admin/DocumentTest.php

135 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\admin;
use App\Models\Admission;
use App\Models\Document;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class DocumentTest extends TestCase
{
private User $user;
private array $data;
private Document $document;
private Admission $admission;
protected function setUp(): void
{
parent::setUp();
$this->admission = Admission::factory()->create();
$this->document = Document::factory()->create();
$this->data = Document::factory()->make()->only([
'position',
'name',
'description',
'admission_id',
]);
$this->user = User::factory()->create([
'name' => 'admin',
'email' => 'test@example.com',
'password' => 123456
]);
}
public function testIndexDocumentsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('documents.index'));
$response->assertOk();
}
public function testCreateDocumentPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('documents.create'));
$response->assertOk();
}
public function testStoreDocument(): void
{
Storage::fake('fake');
$file = UploadedFile::fake()->create('fake.pdf', 100, 'application/pdf');
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->post(route('documents.store'), [...$this->data, 'document' => $file]);
$response->assertRedirect(route('documents.index'));
$this->assertDatabaseHas('documents', $this->data);
}
public function testShowDocumentPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('documents.show', $this->document));
$response->assertOk();
}
public function testEditDocumentPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('documents.edit', $this->document));
$response->assertOk();
}
public function testUpdateDocument(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->patch(route('documents.update', $this->document), $this->data);
$response->assertRedirect(route('documents.index'));
$this->assertDatabaseHas('documents', $this->data);
}
public function testDestroyDocument(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->delete(route('documents.destroy', $this->document));
$response->assertRedirect(route('documents.index'));
$this->assertDatabaseMissing('documents', $this->document->toArray());
}
public function testCreateDocumentFromAdmissionPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('document_create_from_admission', $this->admission));
$response->assertOk();
}
public function testStoreFromAdmissionDocument(): void
{
Storage::fake('fake');
$file = UploadedFile::fake()->create('fake.pdf', 100, 'application/pdf');
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->post(route('document_store_from_admission'), [...$this->data, 'document' => $file]);
$response->assertRedirect(route('admissions.index'));
$this->assertDatabaseHas('documents', $this->data);
}
}