Let's Build A Forum Series - Handler.php

Published 1 year ago by afonte15

Hi,

I am currently going though the "Let's Build a Forum" but I am having trouble with the code from episode 4. When I run the phpunit test I get the error below.

I made the change just as Jeff did on Handler.php. Any help would be helpful as I cannot continue working on the project and would like to. I am currently using Laravel 5.5.


1) Tests\Feature\ParticipateInForumTest::an_authenticated_user_may_participate_in_forum_threads
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: 

/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:255
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:242
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:176
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:599
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:588
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:577
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
/home/vagrant/Code/forum/vendor/fideloper/proxy/src/TrustProxies.php:56
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
/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:149
/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:149
/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:149
/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:149
/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:151
/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:294
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:144
/home/vagrant/Code/forum/tests/Feature/ParticipateInForumTest.php:34


There was 1 failure:

1) Tests\Feature\ParticipateInForumTest::unauthenticated_users_may_not_add_replies
Failed asserting that exception of type "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException" matches expected exception "Illuminate\Auth\AuthenticationException". Message was: "" at
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:255
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:242
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:176
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:599
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:588
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Router.php:577
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
/home/vagrant/Code/forum/vendor/fideloper/proxy/src/TrustProxies.php:56
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
/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:149
/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:149
/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:149
/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:149
/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:151
/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:294
/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:144
/home/vagrant/Code/forum/tests/Feature/ParticipateInForumTest.php:20


Best Answer (As Selected By afonte15)
sutherland

What does your test look like? My next guess is that you are using something like $this->thread = factory('App\Thread'); in your setUp() method instead of $this->thread = factory('App\Thread')->create();

sutherland

Did you register the route using the appropriate method? You must be calling POST on route defined as GET.

afonte15

That is my Route for my Replies Controller just as Jeff has it. Unless I am making a typo error somewhere I am not sure what is wrong with my code.

Route::get('/threads/{thread}/replies', '[email protected]');

<?php

namespace App\Http\Controllers;

use App\Thread;
use Illuminate\Http\Request;

class RepliesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    
    public function store(Thread $thread)
    {
        $thread->addReply([
            'body' => request('body'),
            'user_id' => auth()->id()
        ]);
        
         return back();
    }
}

sutherland

That's not what his route uses, it needs to be registered as POST like so:

Route::post('/threads/{thread}/replies', '[email protected]');
afonte15

Thank you @sutherland you are correct I was writting that incorrectly!

Now I receive this error when I run phpunit. I have copied and pasted from Jeff's github to my code. I am not sure why it is not recognizing addReply as a method.

[email protected]:~/Code/forum$ phpunit --filter ThreadTest
PHPUnit 6.4.1 by Sebastian Bergmann and contributors.

EEE                                                                 3 / 3 (100%)

Time: 622 ms, Memory: 8.00MB

There were 3 errors:

1) Tests\Unit\ThreadTest::a_thread_has_replies
ErrorException: Undefined property: Illuminate\Database\Eloquent\FactoryBuilder::$replies

/home/vagrant/Code/forum/tests/Unit/ThreadTest.php:28

2) Tests\Unit\ThreadTest::a_thread_has_a_creator
ErrorException: Undefined property: Illuminate\Database\Eloquent\FactoryBuilder::$creator

/home/vagrant/Code/forum/tests/Unit/ThreadTest.php:35

3) Tests\Unit\ThreadTest::a_thread_can_add_a_reply
BadMethodCallException: Method addReply does not exist.

/home/vagrant/Code/forum/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php:97
/home/vagrant/Code/forum/tests/Unit/ThreadTest.php:41

ERRORS!
Tests: 3, Assertions: 0, Errors: 3.
sutherland

Without seeing more of your code, I can't really help you. Just keep digging through his source code on Github to find your mistakes. It sounds like you may not have created the addReply() method or the correct relationship methods on the thread model.

https://github.com/laracasts/Lets-Build-a-Forum-in-Laravel/blob/76a05a038ae9a846df9b11ab1fcff48376ce74cf/app/Thread.php

afonte15

It is there.... I even copied and pasted the code to make sure

thread.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;


class Thread extends Model
{
    /**
     * Don't auto-apply mass assignment protection.
     *
     * @var array
     */

    protected $guarded = [];
    /**
     * Get a string path for the thread.
     *
     * @return string
     */

    public function path()
    {
        return '/threads/' . $this->id;
    }

    /**
     * A thread may have many replies.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */

    public function replies()
    {
        return $this->hasMany(Reply::class);
    }

    /**
     * A thread belongs to a creator.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo

     */

    public function creator()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * Add a reply to the thread.
     *
     * @param $reply
     */

    public function addReply($reply)
    {
        $this->replies()->create($reply);
    }
}

Reply.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Reply extends Model
{
    protected $guarded = [];
    
    public function owner()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
afonte15

@sutherland I found my mistake! Thank you for your help and your patience...

The mistake was on my ThreadTest.php

 public function  setUp()
    {
        parent::setUp();
        
        $this->thread = factory('App\Thread')->create();

//I was missing the ->create(); from deleting and adding things on other pages when I was trying to figure out what was wrong which originally was the post method on my routes that I forgot to add that line back correctly.

    }
sutherland

What does your test look like? My next guess is that you are using something like $this->thread = factory('App\Thread'); in your setUp() method instead of $this->thread = factory('App\Thread')->create();

afonte15

Yes, I posted above how I had the code written incorrectly!

@sutherland thank you again!

On another note if you have time to take a look at this thread....

https://laracasts.com/discuss/channels/eloquent/dropping-foreign-keys

sutherland

Don't forget to mark as solved

Please sign in or create an account to participate in this conversation.