Skip to content

Commit

Permalink
Rewrite the test for creating post
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Dec 13, 2022
1 parent b97d903 commit f4f6109
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 88 deletions.
7 changes: 3 additions & 4 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace App\Http\Controllers;

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

class PostController extends Controller
{
Expand All @@ -12,8 +11,8 @@ public function create()
return view('posts.create');
}

public function storeDataWithoutValidation()
public function store(PostRequest $request)
{
Post::create(\request()->all());
auth()->user()->posts()->create($request->validated());
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/PostRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'title' => '',
'description' => ''
];
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function posts()
{
return $this->hasMany(Post::class);
}
}
6 changes: 6 additions & 0 deletions database/migrations/2022_12_08_152740_create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->text('description');
$table->timestamps();

$table->foreign('user_id')
->references('id')
->on('users')
->cascadeOnDelete();
});
}

Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
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');
Route::post('/store', 'store')->name('posts.store');
});
});
21 changes: 0 additions & 21 deletions tests/Feature/ExampleTest.php

This file was deleted.

44 changes: 0 additions & 44 deletions tests/Feature/FormTest.php

This file was deleted.

25 changes: 25 additions & 0 deletions tests/Feature/PostModuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Feature;

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

class PostModuleTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function a_user_can_create_a_post()
{
$this->withoutExceptionHandling();

$this->actingAs(User::factory()->create());

$this->post(route('posts.store'), $attributes = Post::factory()->raw());

$this->assertDatabaseHas('posts', $attributes);
}
}
18 changes: 0 additions & 18 deletions tests/Unit/ExampleTest.php

This file was deleted.

21 changes: 21 additions & 0 deletions tests/Unit/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\Unit;

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

class UserTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function a_user_has_posts()
{
$user = User::factory()->create();

$this->assertInstanceOf(Collection::class, $user->posts);
}
}

0 comments on commit f4f6109

Please sign in to comment.