<?php
namespace Tests\Unit\admin\Controllers;

use PHPUnit\Framework\TestCase;
use admin\Controllers\ShopCategoryController;
use Domain\Category\CategoryRepository;
use Domain\Languages\LanguagesRepository;

class ShopCategoryControllerTest extends TestCase
{
    private $repository;
    private $languagesRepository;
    private $controller;

    protected function setUp(): void
    {
        $this->repository = $this->createMock(CategoryRepository::class);
        $this->languagesRepository = $this->createMock(LanguagesRepository::class);
        $this->controller = new ShopCategoryController($this->repository, $this->languagesRepository);
    }

    public function testConstructorAcceptsDependencies(): void
    {
        $controller = new ShopCategoryController($this->repository, $this->languagesRepository);
        $this->assertInstanceOf(ShopCategoryController::class, $controller);
    }

    public function testHasExpectedActionMethods(): void
    {
        $this->assertTrue(method_exists($this->controller, 'view_list'));
        $this->assertTrue(method_exists($this->controller, 'list'));
        $this->assertTrue(method_exists($this->controller, 'category_edit'));
        $this->assertTrue(method_exists($this->controller, 'edit'));
        $this->assertTrue(method_exists($this->controller, 'save'));
        $this->assertTrue(method_exists($this->controller, 'category_delete'));
        $this->assertTrue(method_exists($this->controller, 'delete'));
        $this->assertTrue(method_exists($this->controller, 'category_products'));
        $this->assertTrue(method_exists($this->controller, 'products'));
        $this->assertTrue(method_exists($this->controller, 'category_url_browser'));
        $this->assertTrue(method_exists($this->controller, 'save_categories_order'));
        $this->assertTrue(method_exists($this->controller, 'save_products_order'));
        $this->assertTrue(method_exists($this->controller, 'cookie_categories'));
    }

    public function testViewActionsReturnString(): void
    {
        $reflection = new \ReflectionClass($this->controller);

        $this->assertEquals('string', (string)$reflection->getMethod('view_list')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('list')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('category_edit')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('edit')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('category_products')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('products')->getReturnType());
    }

    public function testMutationActionsReturnVoid(): void
    {
        $reflection = new \ReflectionClass($this->controller);

        $this->assertEquals('void', (string)$reflection->getMethod('save')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('category_delete')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('delete')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('category_url_browser')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('save_categories_order')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('save_products_order')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('cookie_categories')->getReturnType());
    }

    public function testConstructorRequiresCategoryAndLanguagesRepositories(): void
    {
        $reflection = new \ReflectionClass(ShopCategoryController::class);
        $constructor = $reflection->getConstructor();
        $params = $constructor->getParameters();

        $this->assertCount(2, $params);
        $this->assertEquals('Domain\\Category\\CategoryRepository', $params[0]->getType()->getName());
        $this->assertEquals('Domain\\Languages\\LanguagesRepository', $params[1]->getType()->getName());
    }
}
