2024-03-20 13:09:39 +03:00
|
|
|
<?php
|
|
|
|
|
2024-04-23 14:23:21 +03:00
|
|
|
namespace Tests\Feature\admin;
|
2024-03-20 13:09:39 +03:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|