|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Models\Posts; |
| 6 | +use App\Models\User; |
| 7 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 8 | +use Illuminate\Foundation\Testing\WithFaker; |
| 9 | +use Illuminate\Support\Facades\Auth; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class PostTest extends TestCase |
| 13 | +{ |
| 14 | + use RefreshDatabase; |
| 15 | + |
| 16 | + public function test_home_screen_can_be_rendered() |
| 17 | + { |
| 18 | + $response = $this->get('/'); |
| 19 | + $response->assertViewHas('posts'); |
| 20 | + $response->assertStatus(200); |
| 21 | + } |
| 22 | + |
| 23 | + public function test_home_screen_can_be_rendered_with_posts() |
| 24 | + { |
| 25 | + $post = Posts::factory(5)->create(); |
| 26 | + $response = $this->get('/'); |
| 27 | + $response->assertViewHas('posts'); |
| 28 | + $response->assertSeeTextInOrder([ |
| 29 | + date('j F Y', strtotime($post[0]->created_at)), |
| 30 | + $post[0]->title, |
| 31 | + strlen($post[0]->content) > 100 ? substr(strip_tags($post[0]->content), 0, 100) . '...' : strip_tags($post->content), |
| 32 | + 'Read more', |
| 33 | + $post[0]->user->name |
| 34 | + ]); |
| 35 | + $response->assertStatus(200); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_create_post_screen_can_be_rendered() |
| 39 | + { |
| 40 | + $user = User::factory()->create(); |
| 41 | + |
| 42 | + $response = $this->actingAs($user)->get('/posts/create'); |
| 43 | + |
| 44 | + $response->assertStatus(200); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_user_can_create_post() |
| 48 | + { |
| 49 | + $user = User::factory()->create(); |
| 50 | + |
| 51 | + $response = $this->actingAs($user)->post('/posts', [ |
| 52 | + 'title' => 'Test title', |
| 53 | + 'content' => '<h1>Testing this out</h1>' |
| 54 | + ]); |
| 55 | + |
| 56 | + $response->assertRedirect('/posts/6'); |
| 57 | + |
| 58 | + $this->assertDatabaseHas('posts', [ |
| 59 | + 'title' => 'Test title', |
| 60 | + 'content' => '<h1>Testing this out</h1>' |
| 61 | + ]); |
| 62 | + } |
| 63 | + |
| 64 | + public function test_create_post_title_validation() |
| 65 | + { |
| 66 | + $user = User::factory()->create(); |
| 67 | + |
| 68 | + $response = $this->actingAs($user)->post('/posts', [ |
| 69 | + 'title' => null, |
| 70 | + 'content' => '<h1>Testing this out</h1>' |
| 71 | + ]); |
| 72 | + |
| 73 | + $response->assertSessionHasErrors(['title']); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_create_post_content_validation() |
| 77 | + { |
| 78 | + $user = User::factory()->create(); |
| 79 | + |
| 80 | + $response = $this->actingAs($user)->post('/posts', [ |
| 81 | + 'title' => 'Test title', |
| 82 | + 'content' => null |
| 83 | + ]); |
| 84 | + |
| 85 | + $response->assertSessionHasErrors(['content']); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_edit_post_screen_can_be_rendered() |
| 89 | + { |
| 90 | + $post = Posts::factory()->create(); |
| 91 | + $user = User::factory()->create(); |
| 92 | + |
| 93 | + $response = $this->actingAs($user)->get('/posts/' . $post->id . '/edit'); |
| 94 | + |
| 95 | + $response->assertStatus(200); |
| 96 | + } |
| 97 | + |
| 98 | + public function test_user_can_edit_post() |
| 99 | + { |
| 100 | + $post = Posts::factory()->create(); |
| 101 | + $user = User::factory()->create(); |
| 102 | + |
| 103 | + $response = $this->actingAs($user)->put('/posts/' . $post->id, [ |
| 104 | + 'title' => 'Changing Post', |
| 105 | + 'content' => '<h1>Changing the content for post</h1>' |
| 106 | + ]); |
| 107 | + |
| 108 | + $response->assertRedirect('/posts/' . $post->id); |
| 109 | + |
| 110 | + $this->assertDatabaseHas('posts', [ |
| 111 | + 'title' => 'Changing Post', |
| 112 | + 'content' => '<h1>Changing the content for post</h1>' |
| 113 | + ]); |
| 114 | + } |
| 115 | + |
| 116 | + public function test_edit_post_title_validation() |
| 117 | + { |
| 118 | + $post = Posts::factory()->create(); |
| 119 | + $user = User::factory()->create(); |
| 120 | + |
| 121 | + $response = $this->actingAs($user)->put('/posts/' . $post->id, [ |
| 122 | + 'title' => null, |
| 123 | + 'content' => '<h1>Testing this out</h1>' |
| 124 | + ]); |
| 125 | + |
| 126 | + $response->assertSessionHasErrors(['title']); |
| 127 | + } |
| 128 | + |
| 129 | + public function test_edit_post_content_validation() |
| 130 | + { |
| 131 | + $post = Posts::factory()->create(); |
| 132 | + $user = User::factory()->create(); |
| 133 | + |
| 134 | + $response = $this->actingAs($user)->put('/posts/' . $post->id, [ |
| 135 | + 'title' => 'Test title', |
| 136 | + 'content' => null |
| 137 | + ]); |
| 138 | + |
| 139 | + $response->assertSessionHasErrors(['content']); |
| 140 | + } |
| 141 | +} |
0 commit comments