65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Article;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class CommentControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_store_creates_comment()
|
|
{
|
|
$article = Article::factory()->create();
|
|
|
|
$payload = [
|
|
'author_name' => 'John Doe',
|
|
'content' => 'This is a test comment',
|
|
];
|
|
|
|
$response = $this->postJson(route('comments.store', $article), $payload);
|
|
|
|
$response->assertCreated()
|
|
->assertJsonStructure([
|
|
'data' => ['id', 'author_name', 'content', 'created_at']
|
|
])
|
|
->assertJsonFragment([
|
|
'author_name' => 'John Doe',
|
|
'content' => 'This is a test comment',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('comments', [
|
|
'author_name' => 'John Doe',
|
|
'content' => 'This is a test comment',
|
|
'article_id' => $article->id,
|
|
]);
|
|
}
|
|
|
|
public function test_store_requires_author_name_and_content()
|
|
{
|
|
$article = Article::factory()->create();
|
|
|
|
$response = $this->postJson(route('comments.store', $article));
|
|
|
|
$response->assertStatus(422)
|
|
->assertJsonValidationErrors(['author_name', 'content']);
|
|
}
|
|
|
|
public function test_store_requires_author_name_and_content_and_returns_custom_messages()
|
|
{
|
|
$article = Article::factory()->create();
|
|
|
|
$response = $this->postJson(route('comments.store', $article));
|
|
|
|
$response->assertStatus(422)
|
|
->assertJsonValidationErrors(['author_name', 'content']);
|
|
|
|
$errors = $response->json('errors');
|
|
|
|
$this->assertSame('Имя автора обязательно.', $errors['author_name'][0]);
|
|
$this->assertSame('Содержание комментария обязательно.', $errors['content'][0]);
|
|
}
|
|
}
|