user = User::factory()->create(['role' => UserRole::SuperAdmin]); $this->actingAs($this->user, 'backend'); $this->range = new Range(); $this->range->name = 'Gamme ' . uniqid(); $this->range->status = true; $this->range->order = random_int(1, 9999); $this->range->picture_alt = 'alt'; $this->range->picture_title = 'title'; $this->range->save(); }); $createProduct = function (string $name, Range $range): Product { $product = new Product([ 'range_id' => $range->id, 'name' => $name . ' ' . uniqid(), 'brand' => ProductBrand::loue, 'invoicing_unit' => InvoicingUnit::PCE, 'sell_unit' => SellUnit::PCE, 'order_unit' => OrderUnit::PCE, 'tax_rate' => TaxRate::TVA_20, 'description' => 'Description test', 'status' => true, ]); $product->order = random_int(1, 999999); $product->price = Money::EUR(1000); $product->short_description = 'Court'; $product->picture_alt = 'alt'; $product->picture_title = 'title'; $product->picture_large_alt = 'alt'; $product->picture_large_title = 'title'; $product->save(); return $product; }; it('can access index', function (): void { /** @var \Tests\TestCase $this */ // GIVEN // WHEN $response = $this->get('/bo/discounts'); // THEN expect($response->status())->toBe(200); $response->assertViewIs('bo.discounts.index'); }); it('can access create', function (): void { /** @var \Tests\TestCase $this */ // GIVEN // WHEN $response = $this->get('/bo/discounts/create'); // THEN expect($response->status())->toBe(200); $response->assertViewIs('bo.discounts.create'); }); it('can store a discount with associated products', function () use ($createProduct): void { /** @var \Tests\TestCase $this */ // GIVEN $product1 = $createProduct('Produit 1', $this->range); $product2 = $createProduct('Produit 2', $this->range); $data = [ 'type' => DiscountType::PERCENT->value, 'value' => 15, 'is_old_price_displayed' => true, 'date_start' => null, 'date_end' => null, 'client_timezone' => 'UTC', 'associated_products' => [(string) $product1->id, (string) $product2->id], ]; // WHEN $response = $this->post('/bo/discounts', $data); // THEN $discount = Discount::query()->first(); expect($discount)->not->toBeNull() ->and($discount?->type)->toBe(DiscountType::PERCENT) ->and($discount?->value->getPercentage())->toBe(15); $freshProduct1 = Product::query()->find($product1->id); $freshProduct2 = Product::query()->find($product2->id); expect($freshProduct1?->discount_id)->toBe($discount?->id) ->and($freshProduct2?->discount_id)->toBe($discount?->id); $response->assertRedirect("/bo/discounts/{$discount?->id}/edit"); }); it('can access edit', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $discount = new Discount(); $discount->type = DiscountType::PERCENT; $discount->value = DiscountValue::fromPercentage(10); $discount->is_old_price_displayed = true; $discount->save(); // WHEN $response = $this->get("/bo/discounts/{$discount->id}/edit"); // THEN expect($response->status())->toBe(200); $response->assertViewIs('bo.discounts.edit'); }); it('can update a discount and sync products', function () use ($createProduct): void { /** @var \Tests\TestCase $this */ // GIVEN $product1 = $createProduct('P1', $this->range); $product2 = $createProduct('P2', $this->range); $discount = new Discount(); $discount->type = DiscountType::PERCENT; $discount->value = DiscountValue::fromPercentage(10); $discount->is_old_price_displayed = true; $discount->save(); Product::query()->where('id', $product1->id)->update(['discount_id' => $discount->id]); $data = [ 'type' => DiscountType::PERCENT->value, 'value' => 20, 'is_old_price_displayed' => false, 'date_start' => null, 'date_end' => null, 'client_timezone' => 'UTC', 'associated_products' => [(string) $product2->id], ]; // WHEN $response = $this->put("/bo/discounts/{$discount->id}", $data); // THEN $discount->refresh(); expect($discount->is_old_price_displayed)->toBeFalse() ->and($discount->value->getPercentage())->toBe(20); $freshP1 = Product::query()->find($product1->id); $freshP2 = Product::query()->find($product2->id); expect($freshP1?->discount_id)->toBeNull() ->and($freshP2?->discount_id)->toBe($discount->id); $response->assertRedirect("/bo/discounts/{$discount->id}/edit"); }); it('can delete a discount', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $discount = new Discount(); $discount->type = DiscountType::PERCENT; $discount->value = DiscountValue::fromPercentage(10); $discount->is_old_price_displayed = true; $discount->save(); // WHEN $response = $this->delete("/bo/discounts/{$discount->id}"); // THEN expect(Discount::query()->find($discount->id))->toBeNull(); $response->assertRedirect('/bo/discounts'); }); describe('authorization rejections', function (): void { it('redirects unauthenticated guest to login', function (): void { /** @var \Tests\TestCase $this */ // GIVEN auth('backend')->logout(); // WHEN $response = $this->get('/bo/discounts'); // THEN $response->assertRedirect('/bo/login'); }); it('redirects frontend customer trying to access bo to login', function (): void { /** @var \Tests\TestCase $this */ // GIVEN auth('backend')->logout(); $customer = new Customer(); $this->actingAs($customer, 'frontend'); // WHEN $response = $this->get('/bo/discounts'); // THEN $response->assertRedirect('/bo/login'); }); }); });