<?php

namespace App\Tests\Application;

use App\Entity\Public\FederationStructure;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DomCrawler\Crawler;
use ApiPlatform\Symfony\Bundle\Test\Client;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
 * Base class for applicative tests
 */
abstract class OtWebTestCase extends ApiTestCase
{
    protected Client $client;

    public function setup(): void {
        $this->client = static::createClient();
    }

    /**
     * Send a requests, parse the hydra response and return an object or a Collection
     *
     * @param string $method
     * @param string $route
     * @param array<mixed> $parameters
     * @return ResponseInterface
     */
    protected function request(string $method, string $route, array $parameters = []): ResponseInterface {
        return $this->client->request(
            $method,
            $route,
            $parameters
        );
    }

    /**
     * Send a GET request and return the response parsed content
     *
     * @param string $route
     * @param array<mixed> $parameters
     * @return ResponseInterface
     */
    protected function get(string $route, array $parameters = []): ResponseInterface {
        return $this->request(
            Request::METHOD_GET,
            $route,
            $parameters
        );
    }

    protected function validateCollectionSchema(string $resourceClass): void {
        $this->assertResponseIsSuccessful();

        // Asserts that the returned content type is JSON-LD (the default)
        $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');

        // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
        // >>> Issue with the json typed PublicStructure::addresses properties
        // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
    }

}
