From b97d903e491156a4cdb0cefd379639310ff6a22f Mon Sep 17 00:00:00 2001 From: unclexo Date: Fri, 9 Dec 2022 20:53:05 +0600 Subject: [PATCH] Test simple form submission --- app/Http/Controllers/PostController.php | 19 ++++++++ app/Models/Post.php | 18 ++++++++ database/factories/PostFactory.php | 25 +++++++++++ .../2022_12_08_152740_create_posts_table.php | 33 ++++++++++++++ routes/web.php | 12 ++++- tests/Feature/FormTest.php | 44 +++++++++++++++++++ 6 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/PostController.php create mode 100644 app/Models/Post.php create mode 100644 database/factories/PostFactory.php create mode 100644 database/migrations/2022_12_08_152740_create_posts_table.php create mode 100644 tests/Feature/FormTest.php diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php new file mode 100644 index 0000000..b19408d --- /dev/null +++ b/app/Http/Controllers/PostController.php @@ -0,0 +1,19 @@ +all()); + } +} diff --git a/app/Models/Post.php b/app/Models/Post.php new file mode 100644 index 0000000..791db38 --- /dev/null +++ b/app/Models/Post.php @@ -0,0 +1,18 @@ + + */ +class PostFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'title' => $this->faker->sentence, + 'description' => $this->faker->paragraph, + ]; + } +} diff --git a/database/migrations/2022_12_08_152740_create_posts_table.php b/database/migrations/2022_12_08_152740_create_posts_table.php new file mode 100644 index 0000000..5b74819 --- /dev/null +++ b/database/migrations/2022_12_08_152740_create_posts_table.php @@ -0,0 +1,33 @@ +id(); + $table->string('title'); + $table->text('description'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('posts'); + } +}; diff --git a/routes/web.php b/routes/web.php index bf53cd0..148c506 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,7 @@ name('home'); +Route::middleware(['auth'])->group(function () { + Route::get('/home', [HomeController::class, 'index'])->name('home'); + + Route::prefix('posts')->controller(PostController::class)->group(function() { + Route::get('/create', 'create')->name('posts.create'); + + Route::post('/store-simple-form-data', 'storeDataWithoutValidation')->name('posts.store.data.without.validation'); + }); +}); diff --git a/tests/Feature/FormTest.php b/tests/Feature/FormTest.php new file mode 100644 index 0000000..7ee5180 --- /dev/null +++ b/tests/Feature/FormTest.php @@ -0,0 +1,44 @@ +get(route('posts.create'))->assertRedirect(route('login')); + } + + /** + * Simple form test + * + * Test creating a post without validation + * + * - Log in as a user + * - Make a post request with data created using factory + * - Assert database has those data + */ + public function test_a_user_can_create_a_post() + { + $this->actingAs(User::factory()->create()); + + $this->post( + route('posts.store.data.without.validation'), + $attributes = Post::factory()->raw() + ); + + $this->assertDatabaseHas('posts', $attributes); + } +}