<?php

namespace AmpProject\Optimizer\Transformer;

use AmpProject\Dom\Document;
use AmpProject\Optimizer\Error;
use AmpProject\Optimizer\ErrorCollection;
use AmpProject\Tests\ErrorComparison;
use AmpProject\Tests\MarkupComparison;
use AmpProject\Tests\TestMarkup;
use PHPUnit\Framework\TestCase;

/**
 * Test the GoogleFontsPreconnect transformer.
 *
 * @package ampproject/amp-toolbox
 */
final class GoogleFontsPreconnectTest extends TestCase
{
    use ErrorComparison;
    use MarkupComparison;

    /**
     * Provide the data to test the transform() method.
     *
     * @return array[] Associative array of data arrays.
     */
    public function dataTransform()
    {
        $input = static function ($head, $hints = '') {
            return TestMarkup::DOCTYPE . '<html ⚡><head>'
                   . TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . $hints . TestMarkup::SCRIPT_AMPRUNTIME
                   . TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . TestMarkup::STYLE_AMPBOILERPLATE . TestMarkup::NOSCRIPT_AMPBOILERPLATE
                   . $head
                   . '</head><body></body></html>';
        };

        $output = static function ($head, $hints = '') {
            return TestMarkup::DOCTYPE . '<html ⚡><head>'
                   . TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . $hints . TestMarkup::SCRIPT_AMPRUNTIME
                   . TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . TestMarkup::STYLE_AMPBOILERPLATE . TestMarkup::NOSCRIPT_AMPBOILERPLATE
                   . $head
                   . '</head><body></body></html>';
        };

        return [
            'documents with google fonts do preconnect' => [
                $input(
                    '<link href="https://fonts.googleapis.com/css?family=Karla&display=swap" rel="stylesheet">'
                ),
                $output(
                    '<link href="https://fonts.googleapis.com/css?family=Karla&display=swap" rel="stylesheet">',
                    '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>'
                    . '<link rel="dns-prefetch" href="https://fonts.gstatic.com">'
                ),
            ],

            'documents without google fonts do not preconnect' => [
                $input(''),
                $output(''),
            ],
        ];
    }

    /**
     * Test the transform() method.
     *
     * @covers       \AmpProject\Optimizer\Transformer\GoogleFontsPreconnect::transform()
     * @dataProvider dataTransform()
     *
     * @param string                  $source         String of source HTML.
     * @param string                  $expectedHtml   String of expected HTML output.
     * @param ErrorCollection|Error[] $expectedErrors Set of expected errors.
     * @param array                   $config         Configuration data to use.
     */
    public function testTransform($source, $expectedHtml, $expectedErrors = [], $config = [])
    {
        $document    = Document::fromHtml($source);
        $transformer = new GoogleFontsPreconnect();
        $errors      = new ErrorCollection();

        $transformer->transform($document, $errors);

        $this->assertSimilarMarkup($expectedHtml, $document->saveHTML());
        $this->assertSameErrors($expectedErrors, $errors);
    }
}
