Skip to content

Commit f4f6109

Browse files
committed
Rewrite the test for creating post
1 parent b97d903 commit f4f6109

File tree

10 files changed

+92
-88
lines changed

10 files changed

+92
-88
lines changed

app/Http/Controllers/PostController.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5-
use App\Models\Post;
6-
use Illuminate\Http\Request;
5+
use App\Http\Requests\PostRequest;
76

87
class PostController extends Controller
98
{
@@ -12,8 +11,8 @@ public function create()
1211
return view('posts.create');
1312
}
1413

15-
public function storeDataWithoutValidation()
14+
public function store(PostRequest $request)
1615
{
17-
Post::create(\request()->all());
16+
auth()->user()->posts()->create($request->validated());
1817
}
1918
}

app/Http/Requests/PostRequest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class PostRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array<string, mixed>
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'title' => '',
28+
'description' => ''
29+
];
30+
}
31+
}

app/Models/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ class User extends Authenticatable
4141
protected $casts = [
4242
'email_verified_at' => 'datetime',
4343
];
44+
45+
public function posts()
46+
{
47+
return $this->hasMany(Post::class);
48+
}
4449
}

database/migrations/2022_12_08_152740_create_posts_table.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ public function up()
1515
{
1616
Schema::create('posts', function (Blueprint $table) {
1717
$table->id();
18+
$table->unsignedBigInteger('user_id');
1819
$table->string('title');
1920
$table->text('description');
2021
$table->timestamps();
22+
23+
$table->foreign('user_id')
24+
->references('id')
25+
->on('users')
26+
->cascadeOnDelete();
2127
});
2228
}
2329

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
Route::prefix('posts')->controller(PostController::class)->group(function() {
2828
Route::get('/create', 'create')->name('posts.create');
2929

30-
Route::post('/store-simple-form-data', 'storeDataWithoutValidation')->name('posts.store.data.without.validation');
30+
Route::post('/store', 'store')->name('posts.store');
3131
});
3232
});

tests/Feature/ExampleTest.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/Feature/FormTest.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/Feature/PostModuleTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Models\Post;
6+
use App\Models\User;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Tests\TestCase;
9+
10+
class PostModuleTest extends TestCase
11+
{
12+
use RefreshDatabase;
13+
14+
/** @test */
15+
public function a_user_can_create_a_post()
16+
{
17+
$this->withoutExceptionHandling();
18+
19+
$this->actingAs(User::factory()->create());
20+
21+
$this->post(route('posts.store'), $attributes = Post::factory()->raw());
22+
23+
$this->assertDatabaseHas('posts', $attributes);
24+
}
25+
}

tests/Unit/ExampleTest.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/Unit/UserTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use App\Models\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Support\Collection;
8+
use Tests\TestCase;
9+
10+
class UserTest extends TestCase
11+
{
12+
use RefreshDatabase;
13+
14+
/** @test */
15+
public function a_user_has_posts()
16+
{
17+
$user = User::factory()->create();
18+
19+
$this->assertInstanceOf(Collection::class, $user->posts);
20+
}
21+
}

0 commit comments

Comments
 (0)