Skip to content

Commit

Permalink
Test simple form submission
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Dec 9, 2022
1 parent 83d36ee commit b97d903
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
public function create()
{
return view('posts.create');
}

public function storeDataWithoutValidation()
{
Post::create(\request()->all());
}
}
18 changes: 18 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasFactory;

/**
* The attributes that aren't mass assignable.
*
* Empty array means all are mass assignable in this case
*/
protected $guarded = [];
}
25 changes: 25 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use function Spatie\Ignition\ErrorPage\title;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
*/
class PostFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => $this->faker->sentence,
'description' => $this->faker->paragraph,
];
}
}
33 changes: 33 additions & 0 deletions database/migrations/2022_12_08_152740_create_posts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
12 changes: 11 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use App\Http\Controllers\HomeController;
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -19,4 +21,12 @@

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->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');
});
});
44 changes: 44 additions & 0 deletions tests/Feature/FormTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Feature;

use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class FormTest extends TestCase
{
use RefreshDatabase;

/**
* Test that guest cannot manage posts
*
* - Accessing to the form redirects to login page
*/
public function test_guest_cannot_manage_posts()
{
$this->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);
}
}

0 comments on commit b97d903

Please sign in to comment.