<?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\Cart\LegacyOrderImport;

use CpCreation\VitiCore\Cart\LegacyOrderImport\LegacyOrderSourceReader;
use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;

final class LegacyOrderSourceReaderTest extends TestCase
{
    public function testGiftCountIsZeroWhenTheOptionalLegacyTableDoesNotExist(): void
    {
        $source = $this->createMock(Connection::class);
        $source->expects(self::once())
            ->method('fetchOne')
            ->willReturn(0);

        self::assertSame(0, (new LegacyOrderSourceReader())->giftCount($source, 'all'));
    }

    public function testGiftCountUsesOnlyPaidOrdersWhenRequested(): void
    {
        $source = $this->createMock(Connection::class);
        $source->expects(self::exactly(2))
            ->method('fetchOne')
            ->willReturnCallback(static function (string $sql): int {
                if (str_contains($sql, 'information_schema.tables')) {
                    return 1;
                }

                self::assertStringContainsString('checkout.payed_at IS NOT NULL', $sql);

                return 3;
            });

        self::assertSame(3, (new LegacyOrderSourceReader())->giftCount($source, 'paid'));
    }
}
