<commit_before><?php
namespace inklabs\kommerce\Service\Import;

use inklabs\kommerce\Entity\AbstractPayment;
use inklabs\kommerce\Entity\CashPayment;
use inklabs\kommerce\Entity\CheckPayment;
use inklabs\kommerce\Entity\Order;
use inklabs\kommerce\Entity\TaxRate;
use inklabs\kommerce\Entity\User;
use inklabs\kommerce\Lib\CSVIterator;
use inklabs\kommerce\tests\Helper;

class ImportPaymentServiceTest extends Helper\TestCase\ServiceTestCase
{
    protected $metaDataClassNames = [
        Order::class,
        AbstractPayment::class,
        User::class,
        TaxRate::class,
    ];

    public function testImport()
    {
        $this->setupOrdersForImport();

        $this->setCountLogger();

        $repositoryFactory = $this->getRepositoryFactory();
        $paymentService = new ImportPaymentService(
            $repositoryFactory->getOrderRepository(),
            $repositoryFactory->getPaymentRepository()
        );

        $iterator = new CSVIterator(__DIR__ . '/ImportPaymentServiceTest.csv');
        $importResult = $paymentService->import($iterator);

        $this->assertSame(11, $importResult->getSuccessCount());
        $this->assertSame(1, $importResult->getFailedCount());
        $this->assertSame(25, $this->getTotalQueries());

        $paymentTypes = $this->getPaymentTypesInRepository($repositoryFactory);

        $this->assertSame(4, $paymentTypes[CashPayment::class]);
        $this->assertSame(7, $paymentTypes[CheckPayment::class]);
    }

    private function setupOrdersForImport()
    {
        $cartTotal = $this->dummyData->getCartTotal();

        $order1 = $this->dummyData->getOrder($cartTotal);
        $order2 = $this->dummyData->getOrder($cartTotal);
        $order3 = $this->dummyData->getOrder($cartTotal);

        $order1->setExternalId('CO1102-0016');
        $order2->setExternalId('CO1103-0027');
        $order3->setExternalId('CO1104-0032');

        $this->entityManager->persist($order1);
        $this->entityManager->persist($order2);
        $this->entityManager->persist($order3);

        $this->entityManager->flush();
    }

    /**
     * @param $repositoryFactory
     * @return array
     */
    private function getPaymentTypesInRepository($repositoryFactory)
    {
        $paymentTypes = [];
        foreach ($repositoryFactory->getPaymentRepository()->findAll() as $payment) {
            $class = get_class($payment);

            if (! isset($paymentTypes[$class])) {
                $paymentTypes[$class] = 1;
            } else {
                $paymentTypes[$class]++;
            }
        }
        return $paymentTypes;
    }
}<commit_msg>Adding type declaration in import test.<commit_after><?php
namespace inklabs\kommerce\Service\Import;

use inklabs\kommerce\Entity\AbstractPayment;
use inklabs\kommerce\Entity\CashPayment;
use inklabs\kommerce\Entity\CheckPayment;
use inklabs\kommerce\Entity\Order;
use inklabs\kommerce\Entity\TaxRate;
use inklabs\kommerce\Entity\User;
use inklabs\kommerce\EntityRepository\RepositoryFactory;
use inklabs\kommerce\Lib\CSVIterator;
use inklabs\kommerce\tests\Helper;

class ImportPaymentServiceTest extends Helper\TestCase\ServiceTestCase
{
    protected $metaDataClassNames = [
        Order::class,
        AbstractPayment::class,
        User::class,
        TaxRate::class,
    ];

    public function testImport()
    {
        $this->setupOrdersForImport();

        $this->setCountLogger();

        $repositoryFactory = $this->getRepositoryFactory();
        $paymentService = new ImportPaymentService(
            $repositoryFactory->getOrderRepository(),
            $repositoryFactory->getPaymentRepository()
        );

        $iterator = new CSVIterator(__DIR__ . '/ImportPaymentServiceTest.csv');
        $importResult = $paymentService->import($iterator);

        $this->assertSame(11, $importResult->getSuccessCount());
        $this->assertSame(1, $importResult->getFailedCount());
        $this->assertSame(25, $this->getTotalQueries());

        $paymentTypes = $this->getPaymentTypesInRepository($repositoryFactory);

        $this->assertSame(4, $paymentTypes[CashPayment::class]);
        $this->assertSame(7, $paymentTypes[CheckPayment::class]);
    }

    private function setupOrdersForImport()
    {
        $cartTotal = $this->dummyData->getCartTotal();

        $order1 = $this->dummyData->getOrder($cartTotal);
        $order2 = $this->dummyData->getOrder($cartTotal);
        $order3 = $this->dummyData->getOrder($cartTotal);

        $order1->setExternalId('CO1102-0016');
        $order2->setExternalId('CO1103-0027');
        $order3->setExternalId('CO1104-0032');

        $this->entityManager->persist($order1);
        $this->entityManager->persist($order2);
        $this->entityManager->persist($order3);

        $this->entityManager->flush();
    }

    /**
     * @param RepositoryFactory $repositoryFactory
     * @return array
     */
    private function getPaymentTypesInRepository(RepositoryFactory $repositoryFactory)
    {
        $paymentTypes = [];
        foreach ($repositoryFactory->getPaymentRepository()->findAll() as $payment) {
            $class = get_class($payment);

            if (! isset($paymentTypes[$class])) {
                $paymentTypes[$class] = 1;
            } else {
                $paymentTypes[$class]++;
            }
        }
        return $paymentTypes;
    }
}
{'fraction_non_alphanumeric': 0.1375395495998511, 'fraction_numerical': 0.015261492648427322, 'mean_word_length': 3.1085626911314983, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6954118', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1485, 'n_words': 207}