Alright so I'm pretty new to both unit testing, Fakers and laravel. I'm trying to unit test my resource controller, but I'm stuck at the update function.
Here's my controller:
public function store(Request $request)
{
DB::beginTransaction();
try {
$newOrder = new Order;
$newOrder->fill($request->all());
$newOrder->user_id = $request->user()->id;
if (!$request->doAssigned) {
$newOrder->status = ($request->doValidate) ? 'pending' : 'draft';
}
$newOrder->save();
DB::commit();
return $newOrder;
} catch (\Exception $e) {
DB::rollback();
abort(400, $e);
}
}
public function update(Request $request, $id)
{
DB::beginTransaction();
try {
$oOrder = Order::with(['contact', 'contact.company'])->find($id);
$bChangeStatus = $oOrder->status !== $request->status;
$oOrder->fill($request->all());
$oOrder->save();
// update the status
if ($bChangeStatus && $request->status === 'canceled') {
Transport_order::where('order_id', $id)
->whereNotIn('status', ['billed', 'delivered'])
->update(['status' => 'canceled']);
}
// ....... the same if conditions for other status
if ($request->merchandise) {
foreach ($request->merchandise as $oNewMerchandise) {
$oMerchandise = Merchandise::find($oNewMerchandise->id);
$oMerchandise->fill($oNewMerchandise);
$oMerchandise->save();
}
}
$oOrder->equipments()->sync($request->equipment_ids);
DB::commit();
return $oOrder;
} catch (\Exception $e) {
DB::rollback();
abort(400, $e);
}
}
I have already made a test of the store method but I'm stuck with the update methode I can't handle it .
public function testStore_draft()
{
Sanctum::actingAs($this->user, ['*']);
$orderStore = array (
'external_ref' => '44' ,
'contact_id'=> '19',
'comment' => 'oioio',
'user_id'=> '3',
'doAssigned' => false,
'doValidate' => false,
);
$response = $this->json('POST', 'api/order',$orderStore, $this->headers)
->assertStatus(201)
->assertJsonFragment( ['status'=>'draft']);
}
Any help is welcome ! Thank you very much !
Aucun commentaire:
Enregistrer un commentaire