153 lines
4.5 KiB
PHP
153 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Api\V1;
|
|
|
|
use App\Models\Basket;
|
|
use App\Models\Campagne;
|
|
use App\Models\Coupon;
|
|
use App\Models\Customers;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
use Tests\TestCase;
|
|
|
|
class BasketTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_updateBasket_missingRequiredParameters_returnsError()
|
|
{
|
|
$enpoint = route('api.v1.basket.update');
|
|
|
|
$response = $this->json('GET', $enpoint, [
|
|
// missing required params intentionally
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson([
|
|
'Statut' => 'KO',
|
|
'Code' => 101,
|
|
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents',
|
|
]);
|
|
}
|
|
|
|
public function test_updateBasket_invalidCampaign_returnsError()
|
|
{
|
|
$enpoint = route('api.v1.basket.update');
|
|
|
|
$response = $this->json('GET', $enpoint, [
|
|
'customer_hashcode' => 'hash123',
|
|
'refcamp' => 999999, // non existent campaign id
|
|
'refsite' => 1,
|
|
'panier' => '1)1000',
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson([
|
|
'Statut' => 'KO',
|
|
'Code' => 103,
|
|
'Message' => "La campagne n'existe pas ou n'est pas un panier",
|
|
]);
|
|
}
|
|
|
|
public function test_updateBasket_successfulBasketCreation_returnsSuccess()
|
|
{
|
|
$enpoint = route('api.v1.basket.update');
|
|
|
|
// Create a fake campaign
|
|
$campaign = Campagne::factory()->create([
|
|
'camp_published' => 1,
|
|
'camp_type' => 1,
|
|
'camp_siteId' => 1,
|
|
]);
|
|
|
|
// Create a fake customer
|
|
$customer = Customers::factory()->create([
|
|
'hashcode' => 'hash123',
|
|
]);
|
|
|
|
// Create a coupon in the campaign
|
|
$coupon = Coupon::factory()->create();
|
|
$campaign->coupons()->attach($coupon);
|
|
|
|
$response = $this->json('GET', $enpoint, [
|
|
'customer_hashcode' => 'hash123',
|
|
'refcamp' => $campaign->camp_id,
|
|
'refsite' => $campaign->camp_siteId,
|
|
'panier' => sprintf("(%d)%s", 2, $coupon->coupon_code),
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson([
|
|
'Statut' => 'OK',
|
|
]);
|
|
|
|
// Assert basket created for the customer and campaign
|
|
$this->assertDatabaseHas('baskets', [
|
|
'campagne_id' => $campaign->camp_id,
|
|
'customer_id' => $customer->id,
|
|
]);
|
|
|
|
$basket = Basket::where(['campagne_id' => $campaign->camp_id, 'customer_id' => $customer->id])->first();
|
|
|
|
$this->assertDatabaseHas('basket_contents', [
|
|
'quantity' => 2,
|
|
'printed' => 0,
|
|
'basket_id' => $basket->id,
|
|
'coupon_id' => $coupon->coupon_id
|
|
]);
|
|
}
|
|
|
|
public function test_updateBasket_successfulBasketUpdate_returnsSuccess()
|
|
{
|
|
$enpoint = route('api.v1.basket.update');
|
|
|
|
// Create a fake campaign
|
|
$campaign = Campagne::factory()->create([
|
|
'camp_published' => 1,
|
|
'camp_type' => 1,
|
|
'camp_siteId' => 1,
|
|
]);
|
|
|
|
// Create a fake customer
|
|
$customer = Customers::factory()->create([
|
|
'hashcode' => 'hash123',
|
|
]);
|
|
|
|
// Create a coupon in the campaign
|
|
$coupon = Coupon::factory()->create();
|
|
$campaign->coupons()->attach($coupon);
|
|
|
|
// Create a basket
|
|
$basket = new Basket();
|
|
$basket->uuid = (string) Str::uuid();
|
|
$basket->campagne()->associate($campaign);
|
|
$basket->customer()->associate($customer);
|
|
$basket->save();
|
|
|
|
$response = $this->json('GET', $enpoint, [
|
|
'customer_hashcode' => 'hash123',
|
|
'refcamp' => $campaign->camp_id,
|
|
'refsite' => $campaign->camp_siteId,
|
|
'panier' => sprintf("(%d)%s", 2, $coupon->coupon_code),
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson([
|
|
'Statut' => 'OK',
|
|
]);
|
|
|
|
// Assert basket created for the customer and campaign
|
|
$this->assertDatabaseHas('baskets', [
|
|
'campagne_id' => $campaign->camp_id,
|
|
'customer_id' => $customer->id,
|
|
]);
|
|
|
|
$this->assertDatabaseHas('basket_contents', [
|
|
'quantity' => 2,
|
|
'printed' => 0,
|
|
'basket_id' => $basket->id,
|
|
'coupon_id' => $coupon->coupon_id
|
|
]);
|
|
}
|
|
}
|