Roman_applicant-site/tests/Feature/admin/catalog/NewsTest.php

105 lines
2.7 KiB
PHP

<?php
namespace Tests\Feature\admin\catalog;
use App\Models\Department;
use App\Models\EducationalInstitution;
use App\Models\Faculty;
use App\Models\News;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Tests\TestCase;
class NewsTest extends TestCase
{
private User $user;
private News $news;
private array $data;
protected function setUp(): void
{
parent::setUp();
$this->news = News::factory()->create();
$this->data = News::factory()->make()->only([
'name',
'text',
]);
$this->user = User::factory()->create([
'name' => 'admin',
'email' => 'test@example.com',
'password' => 123456
]);
}
public function testIndexNewsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('news.index'));
$response->assertOk();
}
public function testCreateNewsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('news.create'));
$response->assertOk();
}
public function testStoreNews(): void
{
$file = UploadedFile::fake()->create('fake.jpg', 100);
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->post(route('news.store'), [...$this->data, 'photo' => $file]);
$response->assertRedirect(route('news.index'));
$this->assertDatabaseHas('news', $this->data);
}
public function testShowNewsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('news.show', $this->news));
$response->assertOk();
}
public function testEditNewsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('news.edit', $this->news));
$response->assertOk();
}
public function testUpdateNews(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->patch(route('news.update', $this->news), $this->data);
$response->assertRedirect(route('news.index'));
$this->assertDatabaseHas('news', $this->data);
}
public function testDestroyNews(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->delete(route('news.destroy', $this->news));
$response->assertRedirect(route('news.index'));
$this->assertDatabaseMissing('news', $this->news->toArray());
}
}