<?php

namespace AutoAdmin\Bundle\Tests\Unit\Content\Frontend;

use AutoAdmin\Bundle\Content\Frontend\FrontendUrlChecker;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

class FrontendUrlCheckerTest extends TestCase
{
    private function dataFrontendUrlCheckerTest(): array
    {
        $domain = 'https://local.wsl';

        return [
            'null route' => [
                null,
                $domain.'/',
                false,
            ],
            'route starts with underscore' => [
                '_some_route',
                $domain.'/',
                false,
            ],
            'route starts with aa_admin_' => [
                'aa_admin_some_route',
                $domain.'/',
                false,
            ],
            'route url starts with /admin/' => [
                'my_custom_admin_route',
                $domain.'/admin/',
                false,
            ],
            'valid front url' => [
                'some_route',
                $domain.'/',
                true,
            ],
            'valid front page url' => [
                'some_route',
                $domain.'/my-page',
                true,
            ],
        ];
    }

    /**
     * @dataProvider dataFrontendUrlCheckerTest
     */
    public function testFrontendUrlChecker(?string $route, string $uri, bool $expected): void
    {
        $request = $this->createMock(Request::class);
        $path = parse_url($uri, PHP_URL_PATH);
        $request->method('get')->with('_route')->willReturn($route);
        $request->method('getRequestUri')->willReturn($path);

        $service = $this->getMockBuilder(FrontendUrlChecker::class)
            ->disableOriginalConstructor()
            ->getMock();

        $refl = new \ReflectionClass(FrontendUrlChecker::class);
        $method = $refl->getMethod('isFrontUrl');
        $res = $method->invokeArgs($service, [$request]);


        $this->assertEquals($expected, $res);
    }
}
