Skip to content

Commit b97d903

Browse files
committed
Test simple form submission
1 parent 83d36ee commit b97d903

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

app/Models/Post.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

database/factories/PostFactory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
};

routes/web.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use App\Http\Controllers\HomeController;
4+
use App\Http\Controllers\PostController;
35
use Illuminate\Support\Facades\Route;
46

57
/*
@@ -19,4 +21,12 @@
1921

2022
Auth::routes();
2123

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+
});

tests/Feature/FormTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

0 commit comments

Comments
 (0)