<commit_before><?php

namespace Herrera\Wise\Tests;

use Herrera\PHPUnit\TestCase;
use Herrera\Wise\WiseServiceProvider;
use Silex\Application;

class WiseServiceProviderTest extends TestCase
{
    /**
     * @var Application
     */
    private $app;

    /**
     * @var WiseServiceProvider
     */
    private $provider;

    public function testRegister()
    {
        $this->app->register(
            $this->provider,
            array(
                'wise.cache_dir' => $this->createDir(),
                'wise.options' => array(
                    'parameters' => array(
                        'global' => array(
                            'value' => 456
                        )
                    )
                ),
                'wise.path' => $this->createDir(),
            )
        );

        $this->app->boot();

        file_put_contents(
            $this->app['wise.path'] . '/test.ini',
            <<<CONTENTS
[imports]
0[resource] = "test.php"

[ini]
test = 123
global = "%global.value%"
CONTENTS
        );

        file_put_contents(
            $this->app['wise.path'] . '/test.php',
            <<<CONTENTS
<?php return array(
    'imports' => array(
        array('resource' => 'test.json')
    ),
    'php' => array(
        'test' => 456
    )
);
CONTENTS
        );

        file_put_contents(
            $this->app['wise.path'] . '/test.json',
            <<<CONTENTS
{
    "imports": [
        {
            "resource": "test.xml"
        }
    ],
    "json": {
        "test": 789
    }
}
CONTENTS
        );

        file_put_contents(
            $this->app['wise.path'] . '/test.xml',
            <<<CONTENTS
<array>
  <array key="imports">
    <array>
      <str key="resource">test.yml</str>
    </array>
  </array>
  <array key="xml">
    <int key="test">987</int>
  </array>
</array>
CONTENTS
        );

        file_put_contents(
            $this->app['wise.path'] . '/test.yml',
            <<<CONTENTS
yaml:
    test: 654
CONTENTS
        );

        $this->assertEquals(
            array(
                'imports' => array(
                    array('resource' => 'test.yml')
                ),
                'ini' => array('test' => '123', 'global' => '456'),
                'php' => array('test' => 456),
                'json' => array('test' => 789),
                'xml' => array('test' =>  987),
                'yaml' => array('test' => 654),
            ),
            $this->app['wise']->load('test.ini')
        );
    }

    protected function setUp()
    {
        $this->app = new Application(array('debug' => true));
        $this->provider = new WiseServiceProvider();
    }
}
<commit_msg>More thorough testing.<commit_after><?php

namespace Herrera\Wise\Tests;

use ArrayObject;
use Herrera\PHPUnit\TestCase;
use Herrera\Wise\Loader;
use Herrera\Wise\Processor\AbstractProcessor;
use Herrera\Wise\WiseServiceProvider;
use Silex\Application;

class WiseServiceProviderTest extends TestCase
{
    /**
     * @var Application
     */
    private $app;

    private $cwd;
    private $dir;
    private $params;

    /**
     * @var WiseServiceProvider
     */
    private $provider;

    public function getLoaders()
    {
        $loaders = array(
            array(0, 'Herrera\\Wise\\Loader\\IniFileLoader'),
            array(1, 'Herrera\\Wise\\Loader\\PhpFileLoader'),
        );

        if (class_exists('Herrera\\Json\\Json')) {
            $loaders[] = array(2, 'Herrera\\Wise\\Loader\\JsonFileLoader');
        }

        if (class_exists('DOMDocument')) {
            $loaders[] = array(3, 'Herrera\\Wise\\Loader\\XmlFileLoader');
        }

        if (class_exists('Symfony\\Component\\Yaml\\Yaml')) {
            $loaders[] = array(4, 'Herrera\\Wise\\Loader\\YamlFileLoader');
        }

        return $loaders;
    }

    public function testRegister()
    {
        /** @var $wise \Herrera\Wise\Wise */
        $wise = $this->app['wise'];

        $this->assertInstanceOf('Herrera\\Wise\\Wise', $wise);
        $this->assertSame($this->app['wise.loader'], $wise->getLoader());
        $this->assertSame($this->app['wise.collector'], $wise->getCollector());
        $this->assertSame(
            $this->app['wise.options']['parameters'],
            $wise->getGlobalParameters()
        );
        $this->assertSame($this->app['wise.processor'], $wise->getProcessor());
        $this->assertEquals($this->app['wise.cache_dir'], $wise->getCacheDir());

        /** @var $resolver \Herrera\Wise\Loader\LoaderResolver */
        $resolver = $wise->getLoader()->getResolver();

        /** @var $loader \Herrera\Wise\Loader\AbstractFileLoader */
        foreach ($resolver->getLoaders() as $loader) {
            $this->assertSame($wise, $loader->getWise());
            $this->assertSame(
                $wise->getCollector(),
                $loader->getResourceCollector()
            );
        }
    }

    public function testRegisterCollector()
    {
        $this->assertInstanceOf(
            'Herrera\\Wise\\Resource\\ResourceCollector',
            $this->app['wise.collector']
        );
    }

    public function testRegisterLoader()
    {
        /** @var $loader \Symfony\Component\Config\Loader\DelegatingLoader */
        $loader = $this->app['wise.loader'];

        $this->assertInstanceOf(
            'Symfony\\Component\\Config\\Loader\\DelegatingLoader',
            $loader
        );
        $this->assertSame(
            $this->app['wise.loader_resolver'],
            $loader->getResolver()
        );
    }

    public function testRegisterLoaderResolver()
    {
        /** @var $resolver \Herrera\Wise\Loader\LoaderResolver */
        $resolver = $this->app['wise.loader_resolver'];

        $this->assertInstanceOf(
            'Herrera\\Wise\\Loader\\LoaderResolver',
            $resolver
        );
        $this->assertSame(
            $this->app['wise.loaders'],
            $resolver->getLoaders()
        );
    }

    /**
     * @dataProvider getLoaders
     */
    public function testRegisterLoaders($index, $class)
    {
        /** @var $loader \Herrera\Wise\Loader\AbstractFileLoader */
        $loader = $this->app['wise.loaders'][$index];

        $this->assertInstanceOf($class, $loader);
        $this->assertSame(
            $this->app['wise.locator'],
            $loader->getLocator()
        );
    }

    public function testRegisterLocator()
    {
        $this->assertInstanceOf(
            'Symfony\\Component\\Config\\FileLocator',
            $this->app['wise.locator']
        );
        $this->assertEquals(
            array($this->app['wise.path']),
            $this->getPropertyValue($this->app['wise.locator'], 'paths')
        );
    }

    public function testRegisterProcessor()
    {
        /** @var \Herrera\Wise\Processor\DelegatingProcessor */
        $processor = $this->app['wise.processor'];

        $this->assertInstanceOf(
            'Herrera\\Wise\\Processor\\DelegatingProcessor',
            $processor
        );
        $this->assertSame(
            $this->app['wise.processor_resolver'],
            $processor->getResolver()
        );
    }

    public function testRegisterProcessorResolver()
    {
        $this->app['wise.processors'] = array(new TestProcessor());

        /** @var $resolver \Herrera\Wise\Processor\ProcessorResolver */
        $resolver = $this->app['wise.processor_resolver'];

        $this->assertInstanceOf(
            'Herrera\\Wise\\Processor\\ProcessorResolver',
            $resolver
        );
        $this->assertSame(
            $this->app['wise.processors'],
            $resolver->getProcessors()
        );
    }

    public function testRegisterProcessors()
    {
        $this->assertSame(array(), $this->app['wise.processors']);
    }

    protected function setUp()
    {
        $this->app = new Application();
        $this->cwd = getcwd();
        $this->dir = $this->createDir();
        $this->params = new ArrayObject();
        $this->provider = new WiseServiceProvider();

        chdir($this->dir);

        $this->app['wise.cache_dir'] = $this->dir;
        $this->app['wise.path'] = $this->dir;

        $this->provider->register($this->app);
    }

    protected function tearDown()
    {
        chdir($this->cwd);

        parent::tearDown();
    }
}

class TestProcessor extends AbstractProcessor
{
    public function getConfigTreeBuilder()
    {
    }

    public function supports($resource, $type = null)
    {
    }
}

{'fraction_non_alphanumeric': 0.16414737836561172, 'fraction_numerical': 0.004959848842701937, 'mean_word_length': 1.8611486486486486, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 132, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7586698', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2454, 'n_words': 374}