<?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\LegacyOrderImporter;
use CpCreation\VitiCore\Cart\LegacyOrderImport\LegacyOrderSourceReader;
use CpCreation\VitiCore\Cart\LegacyOrderImport\LegacyPricingSnapshotBuilder;
use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\TestCase;

final class LegacyOrderImporterAddressTest extends TestCase
{
    public function testTruncatesAddressValuesToTheExistingColumns(): void
    {
        $target = DriverManager::getConnection([
            'driver' => 'pdo_sqlite',
            'memory' => true,
        ]);
        $importer = new LegacyOrderImporter(
            $target,
            new LegacyOrderSourceReader(),
            new LegacyPricingSnapshotBuilder(),
        );
        $order = [
            'source_invoice_address_id' => 'invoice-id',
            'invoice_phone' => ' +33 (0)3 26 00 00 00 poste 123 ',
            'invoice_vat_id' => ' FR 12-3456-7890-1234-5678 ',
            'source_delivery_address_id' => 'delivery-id',
            'delivery_phone' => '0033 3 26 00 00 00 extension 456',
        ];

        try {
            $normalizeAddress = new \ReflectionMethod($importer, 'normalizeAddress');
            $invoice = $normalizeAddress->invoke($importer, $order, 'invoice');

            self::assertSame('+33326000000', $invoice['phone']);
            self::assertSame('FR12345678901234', $invoice['vat_id']);
        } finally {
            $target->close();
        }
    }
}
