|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Domains\Discussions\Tests\Feature; |
| 4 | + |
| 5 | +use Domains\Accounts\Database\Factories\UserFactory; |
| 6 | +use Domains\Accounts\Models\User; |
| 7 | +use Domains\Discussions\Database\Factories\QuestionFactory; |
| 8 | +use Domains\Discussions\Models\Question; |
| 9 | +use Faker\Factory; |
| 10 | +use Faker\Generator; |
| 11 | +use Illuminate\Http\Response; |
| 12 | +use Laravel\Lumen\Testing\DatabaseMigrations; |
| 13 | +use Tests\TestCase; |
| 14 | + |
| 15 | +class AnswersStoreTest extends TestCase |
| 16 | +{ |
| 17 | + use DatabaseMigrations; |
| 18 | + |
| 19 | + private Generator $faker; |
| 20 | + private User $user; |
| 21 | + private Question $question; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + $this->faker = Factory::create(); |
| 28 | + $this->user = UserFactory::new()->create(); |
| 29 | + $this->question = QuestionFactory::new(['author_id' => $this->user->id])->create(); |
| 30 | + } |
| 31 | + |
| 32 | + /** @test */ |
| 33 | + public function it_stores_answer(): void |
| 34 | + { |
| 35 | + $payload = [ |
| 36 | + 'content' => $this->faker->paragraph, |
| 37 | + ]; |
| 38 | + |
| 39 | + $response = $this->actingAs($this->user) |
| 40 | + ->call('POST', route('discussions.questions.answers', ['questionId' => $this->question->id]), $payload) |
| 41 | + ->assertStatus(Response::HTTP_NO_CONTENT); |
| 42 | + |
| 43 | + self::assertTrue($response->isEmpty()); |
| 44 | + |
| 45 | + $this->seeInDatabase('question_answers', [ |
| 46 | + 'author_id' => $this->user->id, |
| 47 | + 'question_id' => $this->question->id, |
| 48 | + 'content' => $payload['content'] |
| 49 | + ]); |
| 50 | + } |
| 51 | + |
| 52 | + /** @test */ |
| 53 | + public function it_forbids_guests_to_store_answer(): void |
| 54 | + { |
| 55 | + $this->post(route('discussions.questions.answers', ['questionId' => $this->question->id])) |
| 56 | + ->assertResponseStatus(Response::HTTP_UNAUTHORIZED); |
| 57 | + } |
| 58 | + |
| 59 | + /** @test */ |
| 60 | + public function it_fails_to_store_answer_on_validation_errors(): void |
| 61 | + { |
| 62 | + $this->actingAs($this->user) |
| 63 | + ->post(route('discussions.questions.answers', ['questionId' => $this->question->id])) |
| 64 | + ->seeJsonStructure([ |
| 65 | + 'content', |
| 66 | + ]); |
| 67 | + } |
| 68 | + |
| 69 | + /** @test */ |
| 70 | + public function it_fails_to_store_answer_on_invalid_question(): void |
| 71 | + { |
| 72 | + $payload = [ |
| 73 | + 'content' => $this->faker->paragraph, |
| 74 | + ]; |
| 75 | + |
| 76 | + $this->actingAs($this->user) |
| 77 | + ->post(route('discussions.questions.answers', ['questionId' => 1000]), $payload) |
| 78 | + ->assertResponseStatus(Response::HTTP_NOT_FOUND); |
| 79 | + } |
| 80 | +} |
0 commit comments