<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiEtagConditionalMutationContractTest extends FullSurfaceE2EContractCase
{
    public function test_mutating_routes_do_not_succeed_silently_with_stale_conditional_headers(): void
    {
        $routes = array_slice(array_filter($this->apiRoutes(), fn ($route) => in_array($this->primaryMethod($route), ['PUT', 'PATCH', 'DELETE'], true)), 0, 120);

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();

            $this->actingAs($this->actorFor($uri), 'api');
            $response = $this->withHeaders([
                'If-Match' => '"definitely-stale-e2e-etag"',
                'If-Unmodified-Since' => 'Mon, 01 Jan 1990 00:00:00 GMT',
                'Accept' => 'application/json',
            ])->json($method, $this->materializePath($uri), $this->payloadFor($method, $uri));

            $this->assertControlled($response, $method, $uri);
            $this->assertNoServerError($response, 'stale conditional mutation at ' . $method . ' ' . $uri);
        }
    }
}
