Skip to content

Commit

Permalink
Test showing post
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Dec 13, 2022
1 parent f4f6109 commit cc24203
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 5 deletions.
10 changes: 9 additions & 1 deletion app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

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

class PostController extends Controller
{
Expand All @@ -13,6 +14,13 @@ public function create()

public function store(PostRequest $request)
{
auth()->user()->posts()->create($request->validated());
$post = auth()->user()->posts()->create($request->validated());

return redirect($post->path());
}

public function show(Post $post)
{
return view('posts.show', compact('post'));
}
}
10 changes: 10 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ class Post extends Model
* Empty array means all are mass assignable in this case
*/
protected $guarded = [];

public function path()
{
return route('posts.show', $this->id);
}

public function user()
{
return $this->belongsTo(User::class);
}
}
2 changes: 2 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

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

Expand All @@ -18,6 +19,7 @@ class PostFactory extends Factory
public function definition()
{
return [
'user_id' => User::factory(),
'title' => $this->faker->sentence,
'description' => $this->faker->paragraph,
];
Expand Down
36 changes: 36 additions & 0 deletions resources/views/posts/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Create Post') }}</div>

<div class="card-body">
<form action="{{ route('posts.store') }}" method="POST">
@csrf

<div class="mb-3">
<label for="title" class="form-label">{{ __('Title') }}</label>
<input type="text" class="form-control" id="title">
</div>

<div class="mb-3">
<label for="description" class="form-label">{{ __('Description') }}</label>
<textarea class="form-control" id="description" rows="3"></textarea>
</div>

<div class="mb-0">
<button type="submit" class="btn btn-primary">
{{ __('Submit') }}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection

18 changes: 18 additions & 0 deletions resources/views/posts/show.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Post') }}</div>

<div class="card-body">
<h5 class="card-title">{{ $post->title }}</h5>
<p class="card-text">{{ $post->description }}</p>
</div>
</div>
</div>
</div>
</div>
@endsection
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
Route::prefix('posts')->controller(PostController::class)->group(function() {
Route::get('/create', 'create')->name('posts.create');

Route::get('/{post}', 'show')->name('posts.show');

Route::post('/store', 'store')->name('posts.store');
});
});
19 changes: 15 additions & 4 deletions tests/Feature/PostModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@ class PostModuleTest extends TestCase
/** @test */
public function a_user_can_create_a_post()
{
$this->withoutExceptionHandling();

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

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

$this->assertDatabaseHas('posts', $attributes);
}

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

$post = Post::factory()->create();

$this->actingAs($post->user)
->get($post->path())
->assertSee($post->title)
->assertSee($post->description);
}
}
29 changes: 29 additions & 0 deletions tests/Unit/PostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Tests\Unit;

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

class PostTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function a_post_has_path()
{
$post = Post::factory()->create();

$this->assertEquals(route('posts.show', $post->id), $post->path());
}

/** @test */
public function a_post_has_an_owner()
{
$post = Post::factory()->create();

$this->assertInstanceOf(User::class, $post->user);
}
}

0 comments on commit cc24203

Please sign in to comment.