<?php

namespace KupShop\FeedGeneratorBundle\Tests;

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

/**
 * @phpExtension SimpleXML
 */
class FeedGeneratorTest extends \DatabaseTestCase
{
    /** @var FeedGenerator */
    protected $feedGenerator;

    public function setUp(): void
    {
        parent::setUp();

        self::markTestSkipped('JSParser not installed');

        $this->feedGenerator = ServiceContainer::getService(FeedGenerator::class);
    }

    public function testFeedGeneratorService()
    {
        $feedGenerator = $this->feedGenerator;
        $feedGenerator->setConfiguration(file_get_contents(__DIR__.'/FeedGeneratorTest_config.json'));
        $configuration = $feedGenerator->getConfiguration();

        $initialXML = new \DOMDocument();
        $initialXML->load(__DIR__.'/FeedGeneratorTest_initial.xml');

        $initialData = json_decode(
            json_encode(simplexml_load_file(__DIR__.'/FeedGeneratorTest_initial.xml'))
        );
        $initialData = (array) $initialData;
        $itemsData = (array) $initialData['item'];
        unset($initialData['item']);
        foreach ($itemsData as $key => $value) {
            $itemsData[$key] = (array) $value;
        }

        $stringResult = $configuration->getBeginning();

        ob_start();
        $feedGenerator->transform($itemsData, $initialData);
        $stringResult .= ob_get_clean();

        $stringResult .= $configuration->getEnd();
        $actualDOM = new \DOMDocument();
        $actualDOM->preserveWhiteSpace = false;
        $actualDOM->loadXML($stringResult);
        $actualDOM->formatOutput = true;

        $this->assertXmlStringEqualsXmlFile(__DIR__.'/FeedGeneratorTest_expected.xml', $actualDOM->saveXML());
    }
}
