feedback: on a functioning orders test
good afternoon ~
Have been working on this test for quite some time. I think I have it whipped! But want to see if anyone sees any flaws.
I found a few basic orders product_order product type posts and restructured my tables based on these.
https://dba.stackexchange.com/questions/135054/mysql-what-to-store-in-orders-or-product-order
https://laracasts.com/discuss/channels/eloquent/eloquent-relationship-for-order-order-items-and-products?page=1
In this case I have a orders bidreserves (like product_order) and jobs tables.
One issue I had creating a bid reserve was inserting the order_id which is necessarily created afterwards. Today, I used my first DB::transaction. I used a (order_id)->nullable(); column to allow for this. (this is one area I'd like to know if this is the best way to do it)
The other question: is the store method the right place to have a conditional for BidReserveExists()? I thought a custom @can blade directive might be a way to handle that. But am unsure if it's necessary to test for that in addition to a blade directive, or if one test for that condition is adequate?
Thank you ~
public function store($jobId)
{
$job = Job::incomplete()->findOrFail($jobId);
// dd(auth()->user()->BidReserveExists($jobId));
// if (auth()->user()->BidReserveExists($jobId)) {
// return "You have already placed a bid reserve for this job.";
// }
$this->validate(request(), [
'bidreserve' => ['required', 'integer', 'min:1'],
'payment_token' => ['required'],
]);
// Charging the bidder
try {
$bidReserve = $job->bidReserves()->create([
'bidreserve' => 500,
'job_id' => $jobId,
]);
$order = $bidReserve->complete($this->paymentGateway, request('payment_token'), auth()->user()->id);
DB::transaction(function () use($order) {
DB::table('bidreserves')->update(['order_id' => $order->id]);
// DB::table('posts')->delete();
});
return response()->json($order, 201);
} catch (PaymentFailedException $e) {
return response()->json([], 422);
}
}
class CreateBidReservesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bidreserves', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('job_id');
$table->unsignedBigInteger('order_id')->nullable();
$table->foreign('job_id')->references('id')->on('jobs');
$table->bigInteger('bidreserve');
$table->timestamps();
});
}
/** @test */
public function an_authenticated_bidder_can_pay_bidreserve_fee()
{
$this->withoutExceptionHandling();
$user = factory(User::class)->create();
$job = factory(Job::class)->create();
$this->actingAs($user);
$this->payBidReserve($job, [
'bidreserve' => 500,
'payment_token' => $this->paymentGateway->getValidTestToken()
]);
$this->response->assertStatus(201);
$this->assertCount(1, $job->bidReserves);
// dd($job->orders);
// Make sure customer was charged correct amount
$this->assertEquals(500, $this->paymentGateway->totalCharges());
// Make sure order exists for customer
$this->assertTrue($job->hasOrderFor($user->id));
}
here's a dd($job);
0 => App\BidReserve^ {#2798
-bidReserve: null
-email: null
+table: "bidreserves"
#attributes: array:6 [
"id" => "1"
"job_id" => "1"
"order_id" => "1"
"bidreserve" => "500"
"created_at" => "2019-08-24 18:51:01"
"updated_at" => "2019-08-24 18:51:01"
]
0 => App\Order^ {#2764
#connection: "sqlite_testing"
#table: "orders"
#attributes: array:7 [
"id" => "1"
"user_id" => "1"
"confirmation_number" => "KVS3PGCW9CKBN58PAZGR45D6"
"amount" => "500"
"card_last_four" => "4242"
"created_at" => "2019-08-24 18:51:01"
"updated_at" => "2019-08-24 18:51:01"
Please or to participate in this conversation.