findOrFail($jobId); returns 404 but test creates $job

Posted 1 week ago by trevorpan

error: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST http://bidbird.test/jobs//bidreserve

As you can see above, the $job->id is not found.

class BidReservesController extends Controller
{
    private $paymentGateway;

    public function __construct(PaymentGateway $paymentGateway)
    {
        $this->middleware('auth');
        $this->paymentGateway = $paymentGateway;
    }

....

  /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store($jobId)
    {
        // dd($job);

        $job = Job::incomplete()->findOrFail($jobId);

        dd($job);

        // dd($this->request);
        $this->validate(request(), [
            'bidreserve' => 'required|integer' 
        ]);

        $bidreserve = BidReserve::create([
            'bidreserve' => 500,
            'job_id' => request($job->id),
            'user_id' => auth()->user()->id
            ]);
        
        // Charging the customer
        $this->paymentGateway->charge(request('bidreserve'), request('payment_token'));

        // dd($job);

        return response()->json([], 201);

        return view('jobs.show', compact('bidreserve', 'job'));
    }
<?php

namespace Tests\Feature;

use App\BidReserve;
use App\Job;
use App\User;
use App\Billing\FakePaymentGateway;
use App\Billing\PaymentGateway;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;


class PayBidReservesTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->paymentGateway = new FakePaymentGateway;
        $this->app->instance(PaymentGateway::class, $this->paymentGateway);
        Event::fake();
    }

    private function payBidReserve($job, $params)
    {
        $this->response = $this->json('POST', "/jobs/{$job->id}/bidreserve", $params);
    }


    /**
     * A basic feature test example.
     *
     * @return void
     */

    /** @test */
    public function a_bidder_can_pay_reserve_fee()
    {
        $this->withoutExceptionHandling();
        Event::fake();

        $job = factory(Job::class)->make();
        $user = factory(User::class)->make();
        $bidreserve = factory(BidReserve::class)->create();

        // dd($job);

        // dd($this->paymentGateway);
        // $this->actingAs($user);

        // dd($user);

        $this->actingAs($user)->payBidReserve($job, [
            'bidreserve' => 500,
            'user_id' => $user->id,
            'job_id' => $job->id,
            'payment_token' => $this->paymentGateway->getValidTestToken()
        ]);

     
        $this->response->assertStatus(201);
    }
}
<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\BidReserve;
use App\Job;
use App\User;
use Faker\Generator as Faker;

$factory->define(BidReserve::class, function (Faker $faker) {
    return [
        'user_id' => function() {
                return factory(User::class)->create()->id;
            },
        'job_id' => function() {
            return factory(Job::class)->create()->id;
        },
        'bidreserve' => 500,
        // 'job_id' => $this->faker->numberBetween(1, 10),
        // 'user_id' => $this->faker->numberBetween(1, 100),
    
    ];
});

Ok, when I dd($job); at the test it gives:

#attributes: array:11 [
    "jobtitle" => "Sequi veniam quia explicabo omnis quidem."
    "body" => "Qui veritatis quia sunt id distinctio soluta fugit. Temporibus ut aliquid veniam alias. Debitis optio maxime non a quos. Velit voluptatem dicta placeat quia possimus. Ex delectus debitis voluptas aperiam."
    "deadline" => "2019-09-10 01:16:36"
    "user_id" => 1
    "projectaddress" => "354 Rolfson Skyway Suite 563"
    "city" => "New Heavenchester"
    "state" => "Indiana"
    "zipcode" => "78306"
    "biddertype" => "consectetur"
    "job" => "eveniet"
    "subjob" => "cumque"
  ]

as expected.

when dd($bidreserve); it gives:

#attributes: array:6 [
    "user_id" => 2
    "job_id" => 1
    "bidreserve" => 500
    "updated_at" => "2019-08-13 01:17:32"
    "created_at" => "2019-08-13 01:17:32"
    "id" => 1
  ]
  #original: array:6 [
    "user_id" => 2
    "job_id" => 1
    "bidreserve" => 500
    "updated_at" => "2019-08-13 01:17:32"
    "created_at" => "2019-08-13 01:17:32"
    "id" => 1
  ]
//routes.web
Route::post('/jobs/{job}/bidreserve', '[email protected]')->name('bidreserve');

I have a few questions: if you create job_id and user_id in the BidReserveFactory is it proper to use make() in the test vs. create()?

Is that why there are two users?

But still perplexed why the $job->id is not available when it hits the controller.... thank you ~

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