<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\AgeVerification;

use CpCreation\VitiCore\AgeVerification\RobotRequestDetector;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class AgeVerificationRequestHelpersTest extends TestCase
{
    #[DataProvider('robotUserAgentProvider')]
    public function testItDetectsRobots(string $userAgent): void
    {
        $request = Request::create('/', server: ['HTTP_USER_AGENT' => $userAgent]);

        self::assertTrue(new RobotRequestDetector()->isRobot($request));
    }

    public static function robotUserAgentProvider(): iterable
    {
        yield 'Google' => ['Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'];
        yield 'Bing' => ['Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)'];
        yield 'social preview' => ['facebookexternalhit/1.1'];
    }

    public function testItDoesNotClassifyARegularBrowserAsRobot(): void
    {
        $request = Request::create('/', server: [
            'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Safari/537.36',
        ]);

        self::assertFalse(new RobotRequestDetector()->isRobot($request));
    }

}
