|
| 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 Illuminate\Support\Carbon; |
| 13 | +use Illuminate\Support\Str; |
| 14 | +use Laravel\Lumen\Testing\DatabaseMigrations; |
| 15 | +use Tests\TestCase; |
| 16 | + |
| 17 | +class QuestionsUpdateTest extends TestCase |
| 18 | +{ |
| 19 | + use DatabaseMigrations; |
| 20 | + |
| 21 | + private Generator $faker; |
| 22 | + private User $user; |
| 23 | + private Question $question; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->faker = Factory::create(); |
| 30 | + $this->user = UserFactory::new()->create(); |
| 31 | + $this->question = QuestionFactory::new(['author_id' => $this->user->id])->create(); |
| 32 | + } |
| 33 | + |
| 34 | + /** @test */ |
| 35 | + public function it_updates_questions(): void |
| 36 | + { |
| 37 | + Carbon::setTestNow(); |
| 38 | + |
| 39 | + $payload = [ |
| 40 | + 'title' => $this->faker->title, |
| 41 | + 'description' => $this->faker->paragraph, |
| 42 | + ]; |
| 43 | + |
| 44 | + $response = $this->actingAs($this->user) |
| 45 | + ->call( |
| 46 | + 'PATCH', |
| 47 | + route('discussions.questions.update', ['questionId' => $this->question->id]), |
| 48 | + $payload |
| 49 | + ); |
| 50 | + |
| 51 | + $this->assertResponseStatus(Response::HTTP_NO_CONTENT); |
| 52 | + |
| 53 | + self::assertTrue($response->isEmpty()); |
| 54 | + |
| 55 | + $this->seeInDatabase('questions', [ |
| 56 | + 'id' => $this->question->id, |
| 57 | + 'author_id' => $this->user->id, |
| 58 | + 'title' => $payload['title'], |
| 59 | + 'slug' => Str::slug($payload['title']), |
| 60 | + 'description' => $payload['description'], |
| 61 | + 'updated_at' => Carbon::now(), |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + /** @test */ |
| 66 | + public function it_fails_to_update_if_title_is_missing(): void |
| 67 | + { |
| 68 | + $this->actingAs($this->user) |
| 69 | + ->patch(route('discussions.questions.update', ['questionId' => $this->question->id])) |
| 70 | + ->seeJsonStructure([ |
| 71 | + 'title', |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + /** @test */ |
| 76 | + public function it_keeps_previous_description_if_none_is_sent(): void |
| 77 | + { |
| 78 | + $response = $this->actingAs($this->user) |
| 79 | + ->call( |
| 80 | + 'PATCH', |
| 81 | + route('discussions.questions.update', ['questionId' => $this->question->id]), |
| 82 | + [ |
| 83 | + 'title' => $this->faker->title, |
| 84 | + ] |
| 85 | + ); |
| 86 | + |
| 87 | + self::assertTrue($response->isEmpty()); |
| 88 | + self::assertEquals($this->question->description, $this->question->refresh()->description); |
| 89 | + } |
| 90 | + |
| 91 | + /** @test */ |
| 92 | + public function it_forbids_non_owner_to_update_questions(): void |
| 93 | + { |
| 94 | + $this->actingAs(UserFactory::new()->make()) |
| 95 | + ->patch( |
| 96 | + route('discussions.questions.update', ['questionId' => $this->question->id]), |
| 97 | + [ |
| 98 | + 'title' => $this->faker->title, |
| 99 | + ] |
| 100 | + ) |
| 101 | + ->assertResponseStatus(Response::HTTP_FORBIDDEN); |
| 102 | + } |
| 103 | +} |
0 commit comments