<?php

namespace App\Tests\Settlement;

use App\Payment\Entity\Payment;
use App\Settlement\Entity\FinancialBreakdown;
use App\Tests\ApiTestCase;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;

/**
 * A FinancialBreakdown is an immutable ledger row. Deleting the Payment it
 * settles must be blocked (ON DELETE RESTRICT) rather than silently cascading
 * the ledger row away.
 */
class FinancialBreakdownIntegrityTest extends ApiTestCase
{
    public function testDeletingPaymentWithBreakdownIsRestricted(): void
    {
        $user    = $this->createUser();
        $payment = new Payment($user, 100_000, 'mellat', Payment::TYPE_APPOINTMENT);
        $this->em->persist($payment);

        $breakdown = new FinancialBreakdown(
            $payment,
            FinancialBreakdown::SOURCE_APPOINTMENT,
            $user,
            100_000, 0, '0.00', 0, 100_000, '10.00', 10_000, 90_000,
            null, null, null,
        );
        $this->em->persist($breakdown);
        $this->em->flush();

        $this->em->remove($payment);
        $this->expectException(ForeignKeyConstraintViolationException::class);
        $this->em->flush();
    }
}
