<?php
/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Tests\Integration\Adapter\SqlManager;

use PHPUnit\Framework\TestCase;
use PrestaShop\PrestaShop\Adapter\SqlManager\SqlQueryValidator;

class SqlQueryValidatorTest extends TestCase
{
    /**
     * @dataProvider dataProviderValidate
     *
     * @param string $sql
     * @param array $errors
     */
    public function testValidate(string $sql, array $errors): void
    {
        $validator = new SqlQueryValidator();
        self::assertEquals($errors, $validator->validate($sql));
    }

    public function dataProviderValidate(): iterable
    {
        // Valid queries
        yield [
            'SELECT * FROM ps_customer',
            [],
        ];
        yield [
            'SELECT MAX(o.date_add), COUNT(o.id_order), SUM(o.total_paid_tax_incl), c.id_customer, c.firstname, c.lastname, c.email, c.last_passwd_gen, c.newsletter_date_add, c.date_add, c.date_upd '
            . 'FROM ps_customer c '
            . 'LEFT JOIN ps_orders o ON c.id_customer = o.id_customer '
            . 'GROUP BY c.id_customer;',
            [],
        ];
        yield [
            'SELECT SQL_CALC_FOUND_ROWS b.*, a.* '
            . 'FROM `ps_cart_rule` a '
            . 'LEFT JOIN `ps_cart_rule_lang` b ON (b.`id_cart_rule` = a.`id_cart_rule` AND b.`id_lang` = 1) '
            . 'WHERE 1 '
            . 'ORDER BY a.id_cart_rule DESC',
            [],
        ];
        yield [
            'SELECT SQL_CALC_FOUND_ROWS b.*, a.* '
            . 'FROM `ps_cart_rule` a '
            . 'LEFT JOIN `ps_cart_rule_lang` b ON (b.`id_cart_rule` = a.`id_cart_rule` AND b.`id_lang` = a.`id_cart_rule`) '
            . 'WHERE 1 '
            . 'ORDER BY a.id_cart_rule DESC',
            [],
        ];
        yield [
            'SELECT SQL_CALC_FOUND_ROWS b.*, a.* '
            . 'FROM `ps_cart_rule` a '
            . 'LEFT JOIN `ps_cart_rule_lang` b ON (b.`id_cart_rule` = a.`id_cart_rule` AND 1) '
            . 'WHERE 1 '
            . 'ORDER BY a.id_cart_rule DESC',
            [],
        ];
        // Invalid queries
        yield [
            'SELECT * FROM ps_customera',
            [
                [
                    'key' => 'The "%tablename%" table does not exist.',
                    'parameters' => [
                        '%tablename%' => 'ps_customera',
                    ],
                    'domain' => 'Admin.Advparameters.Notification',
                ],
            ],
        ];
        yield [
            'SELECT * FROM ps_customer c LEFT JOIN ps_orders o ON c.id_customer = o.id_customera',
            [
                [
                    'key' => 'The "%attribute%" attribute does not exist in the "%table%" table.',
                    'parameters' => [
                        '%attribute%' => 'id_customera',
                        '%table%' => 'ps_orders',
                    ],
                    'domain' => 'Admin.Advparameters.Notification',
                ],
            ],
        ];
        yield [
            'SELECT * FROM ps_customer c GROUP BY c.ida',
            [
                [
                    'key' => 'The "%attribute%" attribute does not exist in the "%table%" table.',
                    'parameters' => [
                        '%attribute%' => 'ida',
                        '%table%' => 'ps_customer',
                    ],
                    'domain' => 'Admin.Advparameters.Notification',
                ],
            ],
        ];
    }
}
