So I was following a Laracasts video where I got to lesson 10. I have followed the video closely any can't get my validation to pass. When Irun PHPUnit, I get the following error:
:
1) CreateThreadsTest::a_thread_requires_a_title Illuminate\Validation\ValidationException: The given data failed to pass validation.
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php:105 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php:55 /home/vagrant/Code/forum/app/Http/Controllers/ThreadController.php:44 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:55 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:44 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Route.php:203 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Route.php:160 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:559 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:43 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:65 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:64 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:59 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:561 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:520 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:498 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:174 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:46 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:149 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:234 /home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:84 /home/vagrant/Code/forum/tests/Feature/CreateThreadsTest.php:68 /home/vagrant/Code/forum/tests/Feature/CreateThreadsTest.php:37
Here is my ThreadController.php file:
<?php
namespace App\Http\Controllers;
use App\Thread;
use Illuminate\Http\Request;
class ThreadController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth')->except(['index', 'show']);
}
public function index()
{ $threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('threads.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required'
]);
$thread = Thread::create([
'user_id' => auth()->id(),
'title' => request('title'),
'channel_id' => request('channel_id'),
'body' => request('body'),
]);
return redirect($thread->path());
}
/**
* Display the specified resource.
*
* @param \App\Thead $thead
* @return \Illuminate\Http\Response
*/
public function show($channelId, Thread $thread)
{
return view('threads.show', compact('thread'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Thead $thead
* @return \Illuminate\Http\Response
*/
public function edit(Thead $thead)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Thead $thead
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Thead $thead)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Thead $thead
* @return \Illuminate\Http\Response
*/
public function destroy(Thead $thead)
{
//
}
}'
Here is my TestCase.php file:
<?php
namespace Tests;
use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function setUp()
{
parent::setUp();
$this->disableExceptionHandling();
}
protected function signIn($user = null)
{
$user = $user ?: create('App\User');
$this->actingAs($user);
return $this;
}
// Hat tip, @adamwathan.
protected function disableExceptionHandling()
{
$this->oldExceptionHandler = $this->app->make(ExceptionHandler::class);
$this->app->instance(ExceptionHandler::class, new class extends Handler {
public function __construct() {}
public function report(\Exception $e) {}
public function render($request, \Exception $e) {
throw $e;
}
});
}
protected function withExceptionHandling()
{
$this->app->instance(ExceptionHandler::class, $this->oldExceptionHandler);
return $this;
}
}
and here is my CreateThreadsTest.php file
<?php
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class CreateThreadsTest extends TestCase
{
use DatabaseMigrations;
/** @test */
function guests_may_not_create_threads()
{
$this->expectException('Illuminate\Auth\AuthenticationException');
$this->get('/threads/create')
->assertRedirect('/login');
$this->post('/threads', [])
->assertRedirect('/login');
}
/** @test */
function an_authenticated_user_can_create_new_forum_threads()
{
$this->withExceptionHandling()->signIn();
$thread = make('App\Thread');
$response = $this->post('/threads', $thread->toArray());
dd($response->headers->get('Location'));
$this->get($response->headers->get('Location'))
->assertSee($thread->title)
->assertSee($thread->body);
}
/** @test */
function a_thread_requires_a_title()
{
$this->publishThread(['title' => null])->assertSessionHasErrors('title');
// $this->withExceptionHandling()->signIn();
//
// $thread = make('App\Thread', ['title' => "A"]);
//
//
//
// $this->post('/threads', $thread->toArray());
}
/** @test */
function a_thread_requires_a_body()
{
$this->publishThread(['body' => null])
->assertSessionHasErrors('body');
}
/** @test */
function a_thread_requires_a_valid_channel()
{
factory('App\Channel', 2)->create();
$this->publishThread(['channel_id' => null])
->assertSessionHasErrors('channel_id');
$this->publishThread(['channel_id' => 999])
->assertSessionHasErrors('channel_id');
}
protected function publishThread($overrides = [])
{
$this->withExceptionHandling()->signIn();
$thread = make('App\Thread', $overrides);
return $this->post('/threads', $thread->toArray());
}
}
As you can see this is the code I copied from the Laracasts videos. As you can see, the validation fails on the threads controller even though I enable ExceptionHandling for the title. This is weird indeed. How do I properly enable exception handling for the title? Thanks?