mivita/tests/Feature/AboRepositoryDateChangeTest.php
2026-04-10 17:15:27 +02:00

100 lines
2.8 KiB
PHP

<?php
use App\Models\UserAbo;
use App\Repositories\AboRepository;
use Carbon\Carbon;
uses(Tests\TestCase::class);
beforeEach(function () {
$this->repository = new AboRepository;
});
/**
* Szenario: Heute = 15. März, next_date = 5. April (März-Ausführung bereits erfolgt).
* Änderung auf Tag 20 → neues Datum soll der 20. April sein, NICHT der 20. März.
*/
it('erlaubt Datumsänderung auf den 20. wenn next_date im April liegt und heute der 15. März ist', function () {
Carbon::setTestNow('2026-03-15');
$abo = new UserAbo;
$abo->next_date = '2026-04-05';
$this->repository->setModel($abo);
$newNextDate = invadePrivateMethod($this->repository, 'calculateNewNextDate', [20]);
expect($newNextDate->format('Y-m-d'))->toBe('2026-04-20');
Carbon::setTestNow();
});
/**
* Szenario: Heute = 15. März, next_date = 25. März (im selben Monat, noch nicht ausgeführt).
* Änderung auf Tag 20 → 20. März ist nur 5 Tage entfernt → Referenz bleibt now().
* setNextDate gibt 20. März zurück (5 Tage → zu nah).
*/
it('berechnet neues Datum im selben Monat wenn next_date noch im März liegt', function () {
Carbon::setTestNow('2026-03-15');
$abo = new UserAbo;
$abo->next_date = '2026-03-25';
$this->repository->setModel($abo);
$newNextDate = invadePrivateMethod($this->repository, 'calculateNewNextDate', [20]);
expect($newNextDate->format('Y-m-d'))->toBe('2026-03-20');
Carbon::setTestNow();
});
/**
* Szenario: Heute = 15. März, next_date = 5. April.
* Änderung auf Tag 5 → neues Datum soll 5. April sein (identisch mit aktuell).
*/
it('setzt Datum auf den 5. April wenn Tag 5 gewählt und next_date im April liegt', function () {
Carbon::setTestNow('2026-03-15');
$abo = new UserAbo;
$abo->next_date = '2026-04-05';
$this->repository->setModel($abo);
$newNextDate = invadePrivateMethod($this->repository, 'calculateNewNextDate', [5]);
expect($newNextDate->format('Y-m-d'))->toBe('2026-04-05');
Carbon::setTestNow();
});
/**
* Szenario: Heute = 15. März, kein next_date gesetzt → Referenz ist now().
*/
it('verwendet heute als Referenz wenn kein next_date gesetzt ist', function () {
Carbon::setTestNow('2026-03-15');
$abo = new UserAbo;
$abo->next_date = null;
$this->repository->setModel($abo);
$newNextDate = invadePrivateMethod($this->repository, 'calculateNewNextDate', [20]);
expect($newNextDate->format('Y-m-d'))->toBe('2026-03-20');
Carbon::setTestNow();
});
/**
* Hilfsfunktion zum Aufruf privater Methoden.
*
* @param array<mixed> $args
*/
function invadePrivateMethod(object $object, string $methodName, array $args = []): mixed
{
$reflection = new ReflectionMethod($object, $methodName);
$reflection->setAccessible(true);
return $reflection->invoke($object, ...$args);
}