<?php

namespace SwaggerBake\Test\TestCase\Controller;

use Cake\Core\Configure;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Lib\Service\OpenApiControllerService;

class SwaggerControllerTest extends TestCase
{
    use IntegrationTestTrait;

    /**
     * @var string[]
     */
    public array $fixtures = [
        'plugin.SwaggerBake.Departments',
    ];

    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        static::setAppNamespace('SwaggerBakeTest\App');
    }

    public function test_swagger_index(): void
    {
        Configure::load('swagger_bake');
        $this->get('/');
        $this->assertResponseOk();
        $this->assertResponseContains('/swagger_bake/swagger-ui-bundle.js');
    }

    public function test_redoc_index(): void
    {
        Configure::load('swagger_bake');
        $this->get('/?doctype=redoc');
        $this->assertResponseOk();
        $this->assertResponseContains('/swagger_bake/redoc.standalone.js');
    }

    public function test_hot_reload(): void
    {
        Configure::load('swagger_bake', 'default');
        $this->mockService(OpenApiControllerService::class, function () {
            return new OpenApiControllerService(
                (new Configuration())->setHotReload(true)
            );
        });
        touch(WWW_ROOT . DS . 'swagger.json', 1);
        $time = filemtime(WWW_ROOT . DS . 'swagger.json');

        $this->get('/');
        $this->assertResponseOk();
        $this->assertTrue(filemtime(WWW_ROOT . DS . 'swagger.json') > $time);
    }
}