<?php

namespace App\Tests\Application\Education;

use App\Entity\Education\Cycle;
use App\Tests\Application\OtWebTestCase;

class CycleTest extends OtWebTestCase
{
    public function testCycleResource()
    {
        $this->logger->info("\033[1;34mStarting test... Retrieve the list of all cycle for an admin user\033[0m");
        $this->loginAs($this->user);
        $this->get('/api/cycles');
        $this->assertResponseStatusCodeSame(200);
        $this->logger->info("\033[1;32mTest 'test get cycle collection' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Get cycle by id a an admin \033[0m");
        $this->loginAs($this->user);
        $this->get('/api/cycles/1');
        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonContains([
            'label' => 'Cycle 1',
        ]);
        $this->logger->info("\033[1;32mTest 'Get cycle by id a an admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Change a cycle a an admin \033[0m");
        $this->loginAs($this->user);
        $this->put('/api/cycles/1', [
            'label' => 'new label',
        ]);
        $this->assertResponseStatusCodeSame(200);
        $this->assertJsonContains([
            'label' => 'new label',
        ]);
        $this->logger->info("\033[1;32mTest 'Change a cycle a an admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Delete a cycle a an admin \033[0m");
        $this->loginAs($this->user);
        $this->delete('/api/cycles/1');
        $this->assertResponseStatusCodeSame(405);
        $this->logger->info("\033[1;32mTest 'Delete a cycle a an admin ' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Put a bad cycle enum cycle a an admin \033[0m");
        $this->loginAs($this->user);
        $this->put('/api/cycles/1', [
            'cycleEnum' => 'bad type',
        ]);
        $this->assertResponseStatusCodeSame(400);
        $this->logger->info("\033[1;32mTest 'Put a bad cycle enum cycle a an admin ' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Get a cycle as an non admin\033[0m");
        $this->loginAsStudent($this->user);
        $this->get('/api/cycles/1');
        $this->validateCollectionSchema(Cycle::class, 403);
        $this->assertJsonContains([
            'hydra:description' => 'Access Denied.',
        ]);
        $this->logger->info("\033[1;32mTest 'Get a cycle as an non admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Get all cycle as an non admin\033[0m");
        $this->loginAsStudent($this->user);
        $this->get('/api/cycles');
        $this->assertResponseStatusCodeSame(403);
        $this->logger->info("\033[1;32mTest 'Get all cycle as an non admin' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Get all cycle as an intruder of organization\033[0m");
        $this->loginAsintruOfRoot($this->user);
        $this->get('/api/cycles/1');
        $this->assertResponseStatusCodeSame(404);
        $this->logger->info("\033[1;32mTest 'Get all cycle as an intruder of organization' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Change a cycle as an intruder of organization\033[0m");
        $this->loginAsintruOfRoot($this->user);
        $this->put('/api/cycles/1', [
            'label' => 'new label',
            'organization' => '/api/organizations/1',
        ]);
        $this->assertResponseStatusCodeSame(404);
        $this->assertJsonContains([
            'hydra:title' => 'An error occurred',
            'hydra:description' => 'Not Found',
        ]);
        $this->logger->info("\033[1;32mTest 'Change a  cycle as an intruder of organization' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... delete a cycle as an intruder of organization\033[0m");
        $this->loginAsintruOfRoot($this->user);
        $this->delete('/api/cycles/1');
        $this->assertResponseStatusCodeSame(405);
        $this->assertJsonContains([
            'hydra:title' => 'An error occurred',
        ]);
        $this->logger->info("\033[1;32mTest 'delete a  cycle as an intruder of organization' succeed\033[0m");

        $this->logger->info("\033[1;34mStarting test... Post a cycle as an intruder of organization\033[0m");
        $this->loginAsintruOfRoot($this->user);
        $this->post('/api/cycles', [
            'label' => 'new label',
            'organization' => '/api/organizations/1',
        ]);
        $this->assertResponseStatusCodeSame(405);
        $this->assertJsonContains([
            'hydra:title' => 'An error occurred',
        ]);
        $this->logger->info("\033[1;32mTest 'Post a  cycle as an intruder of organization' succeed\033[0m");
    }
}
