Führe Rabattcodes, Nutzerkreis der Lizenzoption und Pflichtangaben der Kasse ein

Domäne: Rabattcodes mit Geltungsbereich (Warenkorb oder gewählte Ziele) und
Einlösungsberechtigung (allgemein oder freigeschaltete Konten) unabhängig
voneinander; Prüfung bei Eingabe, in jeder Übersicht und unter Sperre vor
Bestellabschluss (Prüfsumme der Übersicht enthält den Rabatt). Rabatt je
Position, Zwischensumme und Zahlbetrag in der Bestellung; Stripe erhält den
Preis nach Rabatt. Nutzerkreis (Einzel-/Mehrfachlizenz, Trainerzahl) an der
Angebotsfassung, kopiert in Bestellposition und Erwerb, Teil der Prüfung
"Bereits erworben". OfferService::activate lehnt Angebote ohne veröffentlichten
Artikelstand ab. Kasse: Zustimmungen als Pflichtangabe mit Nachweis,
"Zahlungspflichtig bestellen", Rechnungsweg für Organisationen, Erstattung über
Stripe, Widerruf erklären. Produktivfreigabe SHOP_LIVE_RELEASE standardmäßig aus.

Bestandstests geändert (fachlich überholt, Assertions nicht abgeschwächt): die
Schaltfläche heißt "Zahlungspflichtig bestellen" (§ 312j BGB), die Summe heißt
"Zu zahlen", der Vereinshinweis steht als Rollenliste; Bestellungen über die
Kasse senden jetzt die Zustimmungen.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-22 19:42:02 +01:00
1 parent cb80835c92
commit 0154182f3b
40 files changed
+2151 -92

No files matched your search

+4 -4
View File
@@ -146,7 +146,7 @@ public function test_a_change_after_the_overview_was_shown_creates_no_order_and_
$this->reprice($offer, 3500);
$this->actingAs($tina)
->post(route('checkout.place'), ['digest' => $shown])
->post(route('checkout.place'), ['digest' => $shown, ...$this->consentFields()])
->assertRedirect(route('checkout'))
->assertSessionHasErrors('checkout');
@@ -172,7 +172,7 @@ public function test_a_withdrawn_position_stays_visible_blocks_the_order_and_lea
->assertSee('Nicht mehr erhältlich')
->assertSee('Konflikte im Training')
->assertSee('Entferne die Positionen, die nicht bestellt werden können');
$this->post(route('checkout.place'), ['digest' => $review->digest()])->assertSessionHasErrors('checkout');
$this->post(route('checkout.place'), ['digest' => $review->digest(), ...$this->consentFields()])->assertSessionHasErrors('checkout');
$this->assertSame(0, Order::query()->count());
$cart->remove($withdrawn);
@@ -217,7 +217,7 @@ public function test_when_everything_is_already_owned_the_checkout_stays_blocked
$this->assertTrue($review->empty());
$this->assertFalse($review->canOrder());
$this->actingAs($tina)->get(route('cart'))->assertSee('Es gibt nichts zu bestellen');
$this->actingAs($tina)->post(route('checkout.place'), ['digest' => $review->digest()])->assertSessionHasErrors('checkout');
$this->actingAs($tina)->post(route('checkout.place'), ['digest' => $review->digest(), ...$this->consentFields()])->assertSessionHasErrors('checkout');
$this->assertSame(1, Order::query()->count(), 'Nur die Vorbestellung existiert.');
}
@@ -333,7 +333,7 @@ public function test_the_cart_survives_login_and_the_checkout_is_reached_in_the_
$this->post('/anmelden', ['email' => 'tina.trainerin@example.test', 'password' => ShopDemoSeeder::PASSWORD])
->assertRedirect(route('checkout'));
$this->get(route('checkout'))->assertOk()->assertSee('Konflikte im Training')->assertSee('Bestellung anlegen');
$this->get(route('checkout'))->assertOk()->assertSee('Konflikte im Training')->assertSee('Zahlungspflichtig bestellen');
}
public function test_the_cart_survives_registration_and_email_confirmation(): void
+366
View File
@@ -0,0 +1,366 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\Bundle;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Commerce\Cart;
use App\Domain\Commerce\Checkout;
use App\Domain\Commerce\Discount\DiscountCode;
use App\Domain\Commerce\Discount\DiscountRelease;
use App\Domain\Commerce\Discount\DiscountResult;
use App\Domain\Commerce\Discount\DiscountService;
use App\Domain\Commerce\Order;
use App\Domain\Commerce\StaleOverview;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Support\BuildsShop;
use Tests\TestCase;
/**
* Rabattcodes (PAY-008, FACH-14): Geltungsbereich und Einlösungsberechtigung sind unabhängig, die Prüfung läuft
* serverseitig bei der Einlösung und erneut vor Bestellabschluss, der Rabatt bleibt in der Bestellung nachvollziehbar.
*/
class DiscountCodeTest extends TestCase
{
use BuildsShop;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seedShop();
$this->travelTo('2026-11-24 10:00:00');
}
/**
* @param array<string, mixed> $data
* @param list<array{type: string, id: int}> $targets
*/
private function code(string $code, array $data = [], array $targets = []): DiscountCode
{
return app(DiscountService::class)->save([
'code' => $code,
'label' => 'Test '.$code,
'kind' => DiscountCode::PERCENT,
'percent' => 10,
'scope' => DiscountCode::SCOPE_CART,
'eligibility' => DiscountCode::OPEN,
'active' => true,
...$data,
], $targets);
}
private function release(DiscountCode $code, User $member): DiscountRelease
{
return app(DiscountService::class)->release($code, $member, $this->vera(), null, 'Zertifikat geprüft');
}
private function cartFor(User $user, array $offers, ?string $code = null): Cart
{
$cart = app(Cart::class);
$cart->clear();
foreach ($offers as $offer) {
$cart->add($offer);
}
if ($code !== null) {
$cart->applyCode($code, $user);
}
return $cart;
}
public function test_a_general_code_reduces_the_whole_cart(): void
{
$this->code('SOMMER10');
$tina = $this->tina();
$cart = $this->cartFor($tina, [$this->privateKonflikte(), $this->privateGrenzen()], 'sommer10');
$review = $cart->review($tina);
$this->assertSame(7800, $review->subtotalCents());
$this->assertSame(780, $review->discountCents());
$this->assertSame(7020, $review->totalCents());
$order = app(Checkout::class)->placeFromCart($tina, $cart, null, $review->digest());
$this->assertSame(7800, $order->subtotal_cents);
$this->assertSame(780, $order->discount_cents);
$this->assertSame(7020, $order->total_cents);
$this->assertSame('SOMMER10', $order->discount_snapshot['code']);
$this->assertSame([490, 290], $order->lines->pluck('discount_cents')->all());
$this->assertSame([4900, 2900], $order->lines->pluck('price_cents')->all(), 'Die Listenpreise bleiben unverändert gespeichert.');
$this->assertSame(0, app(Cart::class)->count());
$this->assertNull(session(Cart::DISCOUNT_KEY), 'Der Code gehört zur Bestellung und ist danach aus dem Warenkorb entfernt.');
}
public function test_an_item_specific_code_reduces_only_the_selected_position_in_a_mixed_cart(): void
{
$konflikte = $this->privateKonflikte();
$this->code('NURKONFLIKT', targets: [['type' => DiscountCode::TARGET_OFFER, 'id' => $konflikte->id]], data: ['scope' => DiscountCode::SCOPE_SELECTED]);
$tina = $this->tina();
$cart = $this->cartFor($tina, [$konflikte, $this->privateGrenzen()], 'NURKONFLIKT');
$review = $cart->review($tina);
$this->assertSame(490, $review->discountCents());
$this->assertSame(490, $review->discountFor($review->lines[0]));
$this->assertSame(0, $review->discountFor($review->lines[1]), 'Die nicht gewählte Position bleibt zum Listenpreis.');
$this->assertSame(7310, $review->totalCents());
}
public function test_a_license_article_target_does_not_make_a_bundle_containing_it_discountable_and_vice_versa(): void
{
$article = $this->article('shop-konflikte');
$bundle = Bundle::query()->where('name', 'Starterpaket')->firstOrFail();
$tina = $this->tina();
$this->code('ARTIKEL', ['scope' => DiscountCode::SCOPE_SELECTED], [['type' => DiscountCode::TARGET_ARTICLE, 'id' => $article->id]]);
$cart = $this->cartFor($tina, [$this->privateKonflikte(), $this->starter()], 'ARTIKEL');
$review = $cart->review($tina);
$this->assertSame(490, $review->discountCents(), 'Zehn Prozent nur auf das Angebot, dessen Position der Artikel selbst ist.');
$this->assertSame(0, $review->discountFor($review->lines[1]), 'Das Starterpaket enthält den Artikel, ist aber nicht als Ziel gewählt.');
$this->code('PAKET', ['scope' => DiscountCode::SCOPE_SELECTED], [['type' => DiscountCode::TARGET_BUNDLE, 'id' => $bundle->id]]);
$cart = $this->cartFor($tina, [$this->privateKonflikte(), $this->starter()], 'PAKET');
$review = $cart->review($tina);
$this->assertSame(0, $review->discountFor($review->lines[0]), 'Der enthaltene Artikel wird durch ein Paketziel nicht rabattfähig.');
$this->assertSame(590, $review->discountFor($review->lines[1]));
}
public function test_a_restricted_code_works_repeatedly_for_a_released_account_only(): void
{
$x = $this->code('ZERT', ['eligibility' => DiscountCode::RESTRICTED, 'percent' => 20]);
$this->release($x, $this->tina());
$first = $this->orderWithCode($this->tina(), 'ZERT');
$second = $this->orderWithCode($this->tina(), 'ZERT');
$this->assertSame(980, $first->discount_cents);
$this->assertSame(980, $second->discount_cents, 'Das freigeschaltete Konto löst den Code bei späteren Bestellungen erneut ein.');
$result = $this->cartFor($this->max(), [$this->privateKonflikte()])->applyCode('ZERT', $this->max());
$this->assertSame(DiscountResult::NO_RELEASE, $result->state);
$this->assertStringContainsString('nicht freigeschaltet', (string) $result->message);
$this->assertSame(0, app(Cart::class)->review($this->max())->discountCents(), 'Wer den Code kennt, aber keine Freigabe hat, erhält keinen Rabatt.');
}
public function test_a_release_for_one_code_does_not_entitle_to_another_code(): void
{
$x = $this->code('CODEX', ['eligibility' => DiscountCode::RESTRICTED]);
$y = $this->code('CODEY', ['eligibility' => DiscountCode::RESTRICTED]);
$tina = $this->tina();
$this->release($x, $tina);
$this->cartFor($tina, [$this->privateKonflikte()]);
$this->assertTrue(app(Cart::class)->applyCode('CODEX', $tina)->applied());
$blocked = app(Cart::class)->applyCode('CODEY', $tina);
$this->assertSame(DiscountResult::NO_RELEASE, $blocked->state);
$this->assertSame($x->id, app(Cart::class)->discountCode()?->id, 'Der abgelehnte zweite Code ändert den bisherigen nicht.');
$this->release($y, $tina);
$this->assertTrue(app(Cart::class)->applyCode('CODEY', $tina)->applied());
$this->assertSame($y->id, app(Cart::class)->discountCode()?->id, 'Pro Bestellung gilt höchstens ein Code: der zweite ersetzt den ersten.');
}
public function test_a_revoked_release_stops_the_discount_and_keeps_the_history(): void
{
$code = $this->code('ZERT', ['eligibility' => DiscountCode::RESTRICTED]);
$release = $this->release($code, $this->tina());
$this->assertTrue($this->cartFor($this->tina(), [$this->privateKonflikte()])->applyCode('ZERT', $this->tina())->applied());
app(DiscountService::class)->revoke($release, $this->vera(), 'Zertifikat abgelaufen');
$review = app(Cart::class)->review($this->tina());
$this->assertSame(DiscountResult::NO_RELEASE, $review->discount?->state);
$this->assertSame(0, $review->discountCents());
$this->assertTrue($review->discountBlocks());
$this->assertFalse($review->canOrder(), 'Ein vorgemerkter Code, der nicht mehr gilt, hält die Bestellung an.');
$this->assertNotNull($release->fresh()->revoked_at);
$this->assertSame($this->vera()->id, $release->fresh()->revoked_by);
$this->assertSame(1, DiscountRelease::query()->count(), 'Freigaben werden nie gelöscht.');
$this->assertSame(DiscountResult::NO_RELEASE, app(Cart::class)->applyCode('ZERT', $this->tina())->state);
}
public function test_a_release_can_have_an_end_date_and_cannot_be_granted_twice_or_for_an_open_code(): void
{
$code = $this->code('BEFRISTET', ['eligibility' => DiscountCode::RESTRICTED]);
$service = app(DiscountService::class);
$service->release($code, $this->tina(), $this->vera(), now()->addDays(2));
$this->assertTrue($this->cartFor($this->tina(), [$this->privateKonflikte()])->applyCode('BEFRISTET', $this->tina())->applied());
$this->travelTo(now()->addDays(3));
$this->assertSame(DiscountResult::NO_RELEASE, app(Cart::class)->review($this->tina())->discount?->state);
$this->expectException(DomainRuleException::class);
$service->release($this->code('OFFEN'), $this->tina(), $this->vera());
}
public function test_an_expired_deactivated_or_not_yet_valid_code_explains_itself(): void
{
$tina = $this->tina();
$this->code('ALT', ['valid_until' => '2026-11-20']);
$this->code('AUS', ['active' => false]);
$this->code('SPAETER', ['valid_from' => '2026-12-01']);
$cart = $this->cartFor($tina, [$this->privateKonflikte()]);
$expired = $cart->applyCode('ALT', $tina);
$this->assertSame(DiscountResult::EXPIRED, $expired->state);
$this->assertStringContainsString('abgelaufen', (string) $expired->message);
$this->assertStringContainsString('20.11.2026', (string) $expired->message);
$this->assertSame(DiscountResult::INACTIVE, $cart->applyCode('AUS', $tina)->state);
$this->assertStringContainsString('01.12.2026', (string) $cart->applyCode('SPAETER', $tina)->message);
$this->assertSame(DiscountResult::UNKNOWN, $cart->applyCode('GIBTESNICHT', $tina)->state);
$this->assertNull($cart->discountCode());
}
public function test_a_code_that_matches_no_position_says_so(): void
{
$this->code('NURGRENZEN', ['scope' => DiscountCode::SCOPE_SELECTED], [['type' => DiscountCode::TARGET_OFFER, 'id' => $this->privateGrenzen()->id]]);
$tina = $this->tina();
$cart = $this->cartFor($tina, [$this->privateKonflikte()]);
$result = $cart->applyCode('NURGRENZEN', $tina);
$this->assertSame(DiscountResult::NO_MATCH, $result->state);
$this->assertStringContainsString('keines davon liegt in deiner Bestellung', (string) $result->message);
}
public function test_an_owned_position_is_not_discounted_and_does_not_count_as_a_match(): void
{
$tina = $this->tina();
$order = $this->orderOf($tina, [$this->privateKonflikte()]);
app(Checkout::class)->confirmTestPayment($order, 'test-own');
$this->code('NURKONFLIKT', ['scope' => DiscountCode::SCOPE_SELECTED], [['type' => DiscountCode::TARGET_OFFER, 'id' => $this->privateKonflikte()->id]]);
$result = $this->cartFor($tina, [$this->privateKonflikte(), $this->privateGrenzen()])->applyCode('NURKONFLIKT', $tina);
$this->assertSame(DiscountResult::NO_MATCH, $result->state, 'Die bereits erworbene Position geht nicht in die Bestellung ein.');
}
public function test_a_revoked_release_between_overview_and_order_creates_no_order(): void
{
$code = $this->code('ZERT', ['eligibility' => DiscountCode::RESTRICTED]);
$release = $this->release($code, $this->tina());
$tina = $this->tina();
$cart = $this->cartFor($tina, [$this->privateKonflikte()], 'ZERT');
$shown = $cart->review($tina)->digest();
app(DiscountService::class)->revoke($release, $this->vera());
try {
app(Checkout::class)->placeFromCart($tina, $cart, null, $shown);
$this->fail('Die Bestellung zum rabattierten Preis darf nicht entstehen.');
} catch (StaleOverview) {
$this->assertSame(0, Order::query()->count());
}
}
public function test_a_code_deactivated_between_overview_and_order_creates_no_order(): void
{
$code = $this->code('SOMMER10');
$tina = $this->tina();
$cart = $this->cartFor($tina, [$this->privateKonflikte()], 'SOMMER10');
$shown = $cart->review($tina)->digest();
app(DiscountService::class)->setActive($code, false);
$this->expectException(StaleOverview::class);
app(Checkout::class)->placeFromCart($tina, $cart, null, $shown);
}
public function test_a_blocked_code_must_be_removed_before_ordering_at_full_price(): void
{
$code = $this->code('SOMMER10');
$tina = $this->tina();
$cart = $this->cartFor($tina, [$this->privateKonflikte()], 'SOMMER10');
app(DiscountService::class)->setActive($code, false);
$review = $cart->review($tina);
$this->assertFalse($review->canOrder());
$this->expectException(DomainRuleException::class);
app(Checkout::class)->placeFromCart($tina, $cart, null, $review->digest());
}
public function test_the_discount_may_not_push_the_payable_amount_below_the_payment_minimum(): void
{
$this->code('GRATIS', ['percent' => 100]);
$tina = $this->tina();
$result = $this->cartFor($tina, [$this->privateGrenzen()])->applyCode('GRATIS', $tina);
$this->assertSame(DiscountResult::TOO_HIGH, $result->state);
$this->assertStringContainsString('0,50 €', (string) $result->message);
}
public function test_the_checkout_page_applies_and_removes_a_code_and_shows_the_discount_and_payable_amount(): void
{
$this->code('SOMMER10');
$tina = $this->tina();
$this->actingAs($tina)->post(route('cart.add', $this->privateKonflikte()));
$this->post(route('checkout.code'), ['code' => 'sommer10'])->assertRedirect(route('checkout'));
$this->get(route('checkout'))
->assertOk()
->assertSee('SOMMER10')
->assertSee('Zwischensumme')
->assertSee('−4,90 €')
->assertSee('44,10 €')
->assertSee('Code entfernen');
$this->post(route('checkout.code.remove'))->assertRedirect(route('checkout'));
$this->get(route('checkout'))->assertOk()->assertDontSee('Zwischensumme')->assertSee('49,00 €');
$this->post(route('checkout.code'), ['code' => 'GIBTESNICHT'])->assertSessionHasErrors('code');
$this->post(route('checkout.code'), ['code' => ''])->assertSessionHasErrors('code');
}
public function test_the_checkout_page_explains_a_missing_release_in_plain_words(): void
{
$this->code('ZERT', ['eligibility' => DiscountCode::RESTRICTED]);
$this->actingAs($this->max())->post(route('cart.add', $this->privateKonflikte()));
$response = $this->post(route('checkout.code'), ['code' => 'ZERT'])->assertSessionHasErrors('code');
$this->assertStringContainsString('nicht freigeschaltet', $response->getSession()->get('errors')->first('code'));
$this->get(route('checkout'))->assertOk()->assertSee('49,00 €')->assertDontSee('Zwischensumme');
}
public function test_codes_are_validated_when_saved(): void
{
$service = app(DiscountService::class);
$base = ['label' => 'x', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'allgemein'];
foreach ([
['code' => 'a b'],
['code' => 'OK1', 'label' => ''],
['code' => 'OK1', 'percent' => 0],
['code' => 'OK1', 'percent' => 101],
['code' => 'OK1', 'kind' => 'betrag', 'amount_euro' => '0'],
['code' => 'OK1', 'scope' => 'auswahl'],
['code' => 'OK1', 'valid_from' => '2026-12-01', 'valid_until' => '2026-11-01'],
] as $override) {
try {
$service->save([...$base, ...$override], []);
$this->fail('Ungültige Angaben müssen abgewiesen werden: '.json_encode($override));
} catch (DomainRuleException) {
$this->assertSame(0, DiscountCode::query()->count());
}
}
$service->save([...$base, 'code' => 'ok1'], []);
$this->expectException(DomainRuleException::class);
$service->save([...$base, 'code' => 'OK1'], []);
}
private function orderWithCode(User $user, string $code): Order
{
$cart = $this->cartFor($user, [$this->privateKonflikte()], $code);
return app(Checkout::class)->placeFromCart($user, $cart, null, $cart->review($user)->digest());
}
}
+2 -2
View File
@@ -273,11 +273,11 @@ public function test_the_checkout_page_places_the_order_through_the_http_flow():
$this->actingAs($tina)->post(route('cart.add', $this->privateKonflikte()));
$this->post(route('cart.add', $this->privateGrenzen()));
$page = $this->get(route('checkout'))->assertOk()->assertSee('Summe')->assertSee('78,00 €');
$page = $this->get(route('checkout'))->assertOk()->assertSee('Zu zahlen')->assertSee('78,00 €');
preg_match('/name="digest" value="([a-f0-9]{64})"/', $page->getContent(), $match);
$this->assertNotEmpty($match);
$this->post(route('checkout.place'), ['digest' => $match[1]])->assertRedirect(route('member.order', Order::query()->latest('id')->first() ?? 1));
$this->post(route('checkout.place'), ['digest' => $match[1], ...$this->consentFields()])->assertRedirect(route('member.order', Order::query()->latest('id')->first() ?? 1));
$order = Order::query()->latest('id')->firstOrFail();
$this->post(route('member.order.confirm', $order))->assertRedirect(route('member.library'));
+2 -1
View File
@@ -232,7 +232,8 @@ public function test_the_checkout_offers_the_choice_between_person_and_club_and_
->assertOk()
->assertSee('Testverein Musterstadt e. V.')
->assertSee('Verein oder Organisation')
->assertSee('Der Verein ist Lizenzinhaber')
->assertSee('Lizenzinhaber')
->assertSee('Nur dieses Konto, nicht weitere Mitglieder des Vereins.')
->assertSee('149,00 €');
$this->get(route('checkout', ['bestellen_als' => $vera->party_id]))
->assertOk()
+6
View File
@@ -27,6 +27,12 @@ protected function seedShop(): void
$this->seed(ShopDemoSeeder::class);
}
/** Die Zustimmungen der Kasse (Pflichtangabe), wie das Formular sie sendet. */
protected function consentFields(): array
{
return ['zustimmung' => ['agb' => '1', 'sofort' => '1']];
}
protected function offerNamed(string $name): Offer
{
return Offer::query()->where('name', $name)->firstOrFail();
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace Tests\Unit;
use App\Domain\Commerce\Discount\DiscountCalculator;
use App\Domain\Commerce\Discount\DiscountCode;
use PHPUnit\Framework\TestCase;
/**
* Rechenregel des Rabatts je Position (ganze Cent). Die Rundung ist eine Entscheidung zur Bestätigung durch Lars;
* die Tests halten fest, was heute gilt.
*/
class DiscountCalculatorTest extends TestCase
{
public function test_a_percentage_is_rounded_per_line_half_up(): void
{
// Drei Positionen zu je 0,99 €, 50 %: je Position 49,5 Cent, aufgerundet auf 50 Cent.
$discounts = DiscountCalculator::allocate(DiscountCode::PERCENT, 50, null, [1 => 99, 2 => 99, 3 => 99]);
$this->assertSame([1 => 50, 2 => 50, 3 => 50], $discounts);
$this->assertSame(150, array_sum($discounts), 'Auf der Warenkorbsumme gerundet wären es 149 Cent.');
}
public function test_a_percentage_never_exceeds_the_line_price(): void
{
$this->assertSame([1 => 4900], DiscountCalculator::allocate(DiscountCode::PERCENT, 100, null, [1 => 4900]));
$this->assertSame([1 => 4900], DiscountCalculator::allocate(DiscountCode::PERCENT, 250, null, [1 => 4900]));
$this->assertSame([1 => 0], DiscountCalculator::allocate(DiscountCode::PERCENT, 0, null, [1 => 4900]));
}
public function test_a_fixed_amount_is_distributed_in_proportion_to_the_prices(): void
{
$this->assertSame([1 => 600, 2 => 400], DiscountCalculator::allocate(DiscountCode::AMOUNT, null, 1000, [1 => 3000, 2 => 2000]));
}
public function test_the_remaining_cents_go_to_the_largest_remainder_and_the_sum_is_exact(): void
{
$discounts = DiscountCalculator::allocate(DiscountCode::AMOUNT, null, 100, [1 => 3333, 2 => 3333, 3 => 3334]);
$this->assertSame([1 => 33, 2 => 33, 3 => 34], $discounts);
$this->assertSame(100, array_sum($discounts));
}
public function test_a_fixed_amount_is_capped_at_the_affected_total(): void
{
$discounts = DiscountCalculator::allocate(DiscountCode::AMOUNT, null, 99999, [1 => 2900, 2 => 4900]);
$this->assertSame([1 => 2900, 2 => 4900], $discounts);
}
public function test_nothing_to_discount_yields_nothing(): void
{
$this->assertSame([], DiscountCalculator::allocate(DiscountCode::PERCENT, 10, null, []));
$this->assertSame([1 => 0], DiscountCalculator::allocate(DiscountCode::AMOUNT, null, 0, [1 => 100]));
}
public function test_codes_are_normalised_without_spaces_and_in_capitals(): void
{
$this->assertSame('ZERT-2026', DiscountCode::normalise(' zert-2026 '));
$this->assertSame('SOMMER10', DiscountCode::normalise('sommer 10'));
}
}