<?php
namespace Tests\Unit\Domain\Cache;

use PHPUnit\Framework\TestCase;
use Domain\Cache\CacheRepository;

/**
 * Testy dla CacheRepository
 *
 * Testujemy logikę Redis (mockowaną) oraz strukturę odpowiedzi.
 * Czyszczenie katalogów delegowane do \Shared\Helpers\Helpers::delete_dir() - nietestowalne unit testami.
 */
class CacheRepositoryTest extends TestCase
{
    /**
     * Test: Czyszczenie cache z Redis
     */
    public function testClearCacheWithRedis(): void
    {
        $mockRedis = $this->createMock(\Redis::class);
        $mockRedis->expects($this->once())->method('flushAll')->willReturn(true);

        $mockRedisConnection = $this->createMock(\Shared\Cache\RedisConnection::class);
        $mockRedisConnection->expects($this->once())->method('getConnection')->willReturn($mockRedis);

        $repository = new CacheRepository($mockRedisConnection);
        $result = $repository->clearCache();

        $this->assertTrue($result['success']);
        $this->assertTrue($result['redisCleared']);
        $this->assertStringContainsString('wyczyszczony', $result['message']);
    }

    /**
     * Test: Redis niedostępny (getConnection zwraca null)
     */
    public function testClearCacheRedisUnavailable(): void
    {
        $mockRedisConnection = $this->createMock(\Shared\Cache\RedisConnection::class);
        $mockRedisConnection->expects($this->once())->method('getConnection')->willReturn(null);

        $repository = new CacheRepository($mockRedisConnection);
        $result = $repository->clearCache();

        $this->assertTrue($result['success']);
        $this->assertFalse($result['redisCleared']);
    }

    /**
     * Test: Bez RedisConnection (null)
     */
    public function testClearCacheWithoutRedis(): void
    {
        $repository = new CacheRepository(null);
        $result = $repository->clearCache();

        $this->assertTrue($result['success']);
        $this->assertFalse($result['redisCleared']);
    }

    /**
     * Test: Struktura odpowiedzi
     */
    public function testClearCacheReturnStructure(): void
    {
        $repository = new CacheRepository(null);
        $result = $repository->clearCache();

        $this->assertArrayHasKey('success', $result);
        $this->assertArrayHasKey('message', $result);
        $this->assertArrayHasKey('redisCleared', $result);
        $this->assertIsBool($result['success']);
        $this->assertIsString($result['message']);
        $this->assertIsBool($result['redisCleared']);
    }
}
