<?php

namespace Tests\Feature\Api\V1\FullSurface;

use Tests\Feature\Api\V1\FullSurface\Support\FullSurfaceE2EContractCase;


class ApiBatch17FinancialPeriodCloseAndAdjustmentContractTest extends FullSurfaceE2EContractCase
{
    public function test_closed_period_adjustments_writeoffs_and_credit_transfers_are_guarded(): void
    {
        $routes = $this->apiRoutesContainingAny(['finance', 'invoice', 'payment', 'refund', 'credit', 'writeoff', 'adjustment', 'ledger']);
        $payload = [
            'invoice_id' => $this->ids['invoiceId'],
            'parent_id' => $this->ids['parentId'],
            'student_id' => $this->ids['studentId'],
            'amount' => '-9999.99',
            'period' => '2020-01',
            'closed_period' => true,
            'force_adjustment' => true,
            'skip_reconciliation' => true,
            'credit_to_parent_id' => 999999,
            'write_off_reason' => '',
        ];

        foreach (array_slice($routes, 0, 100) as $route) {
            $method = $this->primaryMethod($route);
            if (! in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE'], true)) {
                continue;
            }

            $uri = $route->uri();
            $response = $this->requestAs($this->admin, $method, $uri, $this->payloadFor($method, $uri) + $payload);
            $this->assertControlled($response, $method, $uri);
            $this->assertStringNotContainsString('division by zero', strtolower($response->getContent()), $method.' '.$uri.' leaked math internals.');
        }
    }
}
