File tree Expand file tree Collapse file tree 6 files changed +150
-1
lines changed Expand file tree Collapse file tree 6 files changed +150
-1
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \Http \Controllers ;
4
+
5
+ use App \Models \Post ;
6
+ use Illuminate \Http \Request ;
7
+
8
+ class PostController extends Controller
9
+ {
10
+ public function create ()
11
+ {
12
+ return view ('posts.create ' );
13
+ }
14
+
15
+ public function storeDataWithoutValidation ()
16
+ {
17
+ Post::create (\request ()->all ());
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \Models ;
4
+
5
+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
6
+ use Illuminate \Database \Eloquent \Model ;
7
+
8
+ class Post extends Model
9
+ {
10
+ use HasFactory;
11
+
12
+ /**
13
+ * The attributes that aren't mass assignable.
14
+ *
15
+ * Empty array means all are mass assignable in this case
16
+ */
17
+ protected $ guarded = [];
18
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Database \Factories ;
4
+
5
+ use Illuminate \Database \Eloquent \Factories \Factory ;
6
+ use function Spatie \Ignition \ErrorPage \title ;
7
+
8
+ /**
9
+ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
10
+ */
11
+ class PostFactory extends Factory
12
+ {
13
+ /**
14
+ * Define the model's default state.
15
+ *
16
+ * @return array<string, mixed>
17
+ */
18
+ public function definition ()
19
+ {
20
+ return [
21
+ 'title ' => $ this ->faker ->sentence ,
22
+ 'description ' => $ this ->faker ->paragraph ,
23
+ ];
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Illuminate \Database \Migrations \Migration ;
4
+ use Illuminate \Database \Schema \Blueprint ;
5
+ use Illuminate \Support \Facades \Schema ;
6
+
7
+ return new class extends Migration
8
+ {
9
+ /**
10
+ * Run the migrations.
11
+ *
12
+ * @return void
13
+ */
14
+ public function up ()
15
+ {
16
+ Schema::create ('posts ' , function (Blueprint $ table ) {
17
+ $ table ->id ();
18
+ $ table ->string ('title ' );
19
+ $ table ->text ('description ' );
20
+ $ table ->timestamps ();
21
+ });
22
+ }
23
+
24
+ /**
25
+ * Reverse the migrations.
26
+ *
27
+ * @return void
28
+ */
29
+ public function down ()
30
+ {
31
+ Schema::dropIfExists ('posts ' );
32
+ }
33
+ };
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
+ use App \Http \Controllers \HomeController ;
4
+ use App \Http \Controllers \PostController ;
3
5
use Illuminate \Support \Facades \Route ;
4
6
5
7
/*
19
21
20
22
Auth::routes ();
21
23
22
- Route::get ('/home ' , [App \Http \Controllers \HomeController::class, 'index ' ])->name ('home ' );
24
+ Route::middleware (['auth ' ])->group (function () {
25
+ Route::get ('/home ' , [HomeController::class, 'index ' ])->name ('home ' );
26
+
27
+ Route::prefix ('posts ' )->controller (PostController::class)->group (function () {
28
+ Route::get ('/create ' , 'create ' )->name ('posts.create ' );
29
+
30
+ Route::post ('/store-simple-form-data ' , 'storeDataWithoutValidation ' )->name ('posts.store.data.without.validation ' );
31
+ });
32
+ });
Original file line number Diff line number Diff line change
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 FormTest extends TestCase
11
+ {
12
+ use RefreshDatabase;
13
+
14
+ /**
15
+ * Test that guest cannot manage posts
16
+ *
17
+ * - Accessing to the form redirects to login page
18
+ */
19
+ public function test_guest_cannot_manage_posts ()
20
+ {
21
+ $ this ->get (route ('posts.create ' ))->assertRedirect (route ('login ' ));
22
+ }
23
+
24
+ /**
25
+ * Simple form test
26
+ *
27
+ * Test creating a post without validation
28
+ *
29
+ * - Log in as a user
30
+ * - Make a post request with data created using factory
31
+ * - Assert database has those data
32
+ */
33
+ public function test_a_user_can_create_a_post ()
34
+ {
35
+ $ this ->actingAs (User::factory ()->create ());
36
+
37
+ $ this ->post (
38
+ route ('posts.store.data.without.validation ' ),
39
+ $ attributes = Post::factory ()->raw ()
40
+ );
41
+
42
+ $ this ->assertDatabaseHas ('posts ' , $ attributes );
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments