Permalink
Browse files

Episode 91

  • Loading branch information...
JeffreyWay committed Oct 30, 2017
1 parent 1a5afa1 commit e7f53188cdfc58fb2d3a0e244088f0d45dd69ec9
Showing with 77 additions and 0 deletions.
  1. +18 −0 app/Http/Controllers/ThreadsController.php
  2. +1 −0 routes/web.php
  3. +58 −0 tests/Feature/UpdateThreadsTest.php
@@ -103,6 +103,24 @@ public function show($channel, Thread $thread, Trending $trending)
return view('threads.show', compact('thread'));
}
/**
* Update the given thread.
*
* @param string $channel
* @param Thread $thread
*/
public function update($channel, Thread $thread)
{
$this->authorize('update', $thread);
$thread->update(request()->validate([
'title' => 'required',
'body' => 'required'
]));
return $thread;
}
/**
* Delete the given thread.
*
@@ -22,6 +22,7 @@
Route::get('threads', 'ThreadsController@index')->name('threads');
Route::get('threads/create', 'ThreadsController@create');
Route::get('threads/{channel}/{thread}', 'ThreadsController@show');
Route::patch('threads/{channel}/{thread}', 'ThreadsController@update');
Route::delete('threads/{channel}/{thread}', 'ThreadsController@destroy');
Route::post('threads', 'ThreadsController@store')->middleware('must-be-confirmed');
Route::get('threads/{channel}', 'ThreadsController@index');
@@ -0,0 +1,58 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UpdateThreadsTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
$this->withExceptionHandling();
$this->signIn();
}
/** @test */
function unauthorized_users_may_not_update_threads()
{
$thread = create('App\Thread', ['user_id' => create('App\User')->id]);
$this->patch($thread->path(), [])->assertStatus(403);
}
/** @test */
function a_thread_requires_a_title_and_body_to_be_updated()
{
$thread = create('App\Thread', ['user_id' => auth()->id()]);
$this->patch($thread->path(), [
'title' => 'Changed'
])->assertSessionHasErrors('body');
$this->patch($thread->path(), [
'body' => 'Changed'
])->assertSessionHasErrors('title');
}
/** @test */
function a_thread_can_be_updated_by_its_creator()
{
$thread = create('App\Thread', ['user_id' => auth()->id()]);
$this->patch($thread->path(), [
'title' => 'Changed',
'body' => 'Changed body.'
]);
tap($thread->fresh(), function ($thread) {
$this->assertEquals('Changed', $thread->title);
$this->assertEquals('Changed body.', $thread->body);
});
}
}

0 comments on commit e7f5318

Please sign in to comment.