<?php

declare(strict_types=1);

/*
 * This file is part of the RegexParser package.
 *
 * (c) Younes ENNAJI <younes.ennaji.pro@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace RegexParser\Tests\Unit\Lexer;

use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use RegexParser\Lexer;
use RegexParser\Tests\TestUtils\LexerAccessor;
use RegexParser\TokenType;

final class LexerInternalCoverageTest extends TestCase
{
    /**
     * Tests the "default" case of the switch in extractTokenValue.
     * This case is normally unreachable as the main Regex filters tokens,
     * but for 100% coverage we must force it via Reflection.
     */
    public function test_extract_token_value_fallback(): void
    {
        $lexer = new Lexer();
        $lexer->tokenize('');
        $accessor = new LexerAccessor($lexer);

        // Force a token that has no specific extraction logic
        // (e.g. T_LITERAL goes to the default)
        $val = $accessor->callPrivateMethod('extractTokenValue', [
            TokenType::T_LITERAL,
            'X',
            []
        ]);
        $this->assertSame('X', $val);

        // Test empty array fallback (null coalescing) in Lexer
        // Case: T_POSIX_CLASS without 'v_posix' key in matches
        $val = $accessor->callPrivateMethod('extractTokenValue', [
            TokenType::T_POSIX_CLASS,
            '[[:alnum:]]',
            [] // Empty array to simulate partial match
        ]);
        // The code does ($matches['v_posix'] ?? '') -> ''
        $this->assertSame('', $val);
    }

    /**
     * Tests Unicode normalization with malformed data
     * to reach the `??` fallbacks.
     */
    public function test_normalize_unicode_prop_fallbacks(): void
    {
        $lexer = new Lexer();
        $lexer->tokenize('');
        $accessor = new LexerAccessor($lexer);

        // Case where property is empty
        $val = $accessor->callPrivateMethod('normalizeUnicodeProp', [
            '\p{}',
        ]);
        $this->assertSame('', $val);
    }

    /**
     * Tests the fallback of consumeQuoteMode (if preg_match fails completely).
     * This is theoretically impossible with the current pattern, but we secure coverage.
     */
    #[DoesNotPerformAssertions]
    public function test_lex_quote_mode_failure_fallback(): void
    {
        // This test is difficult because it requires a simple preg_match to fail.
        // If you have a @codeCoverageIgnoreStart in Lexer::consumeQuoteMode, ignore this test.
        // Otherwise, we move on.
    }
}
