<commit_before><?php

namespace Karma;

use Gaufrette\Filesystem;
use Gaufrette\Adapter\InMemory;
use Karma\Configuration\InMemoryReader;
use Karma\FormatterProviders\NullProvider;
use Karma\FormatterProviders\CallbackProvider;
use Karma\Formatters\Rules;

class HydratorTest extends \PHPUnit_Framework_TestCase
{
    private
        $sourceFs,
        $targetFs,
        $hydrator;

    protected function setUp()
    {
        $this->sourceFs = new Filesystem(new InMemory());
        $this->targetFs = new Filesystem(new InMemory());
        $reader = new InMemoryReader(array(
            'var:dev' => 42,
            'var:preprod' => 51,
            'var:prod' => 69,
            'db.user:dev' => 'root',
            'db.user:preprod' => 'someUser',
            'bool:dev' => true,
            'bool:prod' => false,
            'list:dev' => array('str', 2, true, null),
            'list:prod' => array(42),
            'todo:dev' => '__TODO__',
            'fixme:dev' => '__FIXME__',
        ));

        $this->hydrator = new Hydrator($this->sourceFs, $this->targetFs, $reader, new Finder($this->sourceFs), new NullProvider());
        $this->hydrator->setSuffix('-dist');
    }

    /**
     * @dataProvider providerTestSimple
     */
    public function testSimple($environment, $expectedBValue, $expectedFValue)
    {
        $this->write('a.php');
        $this->write('b.php-dist', '<%var%>');
        $this->write('c.php', '<%var%>');
        $this->write('d.php-dist', 'var');
        $this->write('e.php-dist', '<%var %>');
        $this->write('f.php-dist', '<%db.user%>');
        $this->write('dir-dist/yolo.php', 'raw content');

        $this->hydrator->hydrate($environment);

        $this->assertTrue($this->targetFs->has('b.php'));
        $this->assertFalse($this->targetFs->has('c.php'));
        $this->assertTrue($this->targetFs->has('d.php'));
        $this->assertTrue($this->targetFs->has('e.php'));
        $this->assertTrue($this->targetFs->has('f.php'));

        $this->assertSame($expectedBValue, $this->targetFs->read('b.php'));

        $this->assertSame('<%var%>', $this->sourceFs->read('c.php'));
        
        $this->assertSame('var', $this->targetFs->read('d.php'));
        $this->assertSame('<%var %>', $this->targetFs->read('e.php'));
        $this->assertSame($expectedFValue, $this->targetFs->read('f.php'));
    }

    public function testTarget()
    {
        $this->write('a.php-dist', '<%var%>');
        $this->write('b.php', '<%var%>');
        $reader = new InMemoryReader([
            'var:dev' => 'test',
        ]);

        $hydrator = new Hydrator($this->sourceFs, $this->targetFs, $reader, new Finder($this->sourceFs), new NullProvider());
        $hydrator->allowNonDistFilesOverwrite();
        $hydrator->hydrate('dev');

        $this->assertTrue($this->targetFs->has('a.php'));
        $this->assertSame('test', $this->targetFs->read('a.php'));
        $this->assertTrue($this->targetFs->has('b.php'));
        $this->assertSame('test', $this->targetFs->read('b.php'));
    }

    public function providerTestSimple()
    {
        return array(
            array('dev', '42', 'root'),
            array('preprod', '51', 'someUser'),
        );
    }

    public function testDryRun()
    {
        $this->write('a.php');
        $this->write('b.php-dist', '<%var%>');
        $this->write('c.php', '<%var%>');

        $this->hydrator
            ->setDryRun()
            ->hydrate('dev');

        $this->assertFalse($this->targetFs->has('b.php'));
    }

    public function testGetUnusedVariables()
    {
        $this->write('a.php');
        $this->write('b.php-dist', '<%var%>');
        $this->write('c.php-dist', '<%list%>');

        $this->hydrator
            ->hydrate('dev');

        $unusedVariables = $this->hydrator->getUnusedVariables();

        $this->assertContains('db.user', $unusedVariables);
        $this->assertContains('bool', $unusedVariables);
        $this->assertContains('todo', $unusedVariables);
        $this->assertContains('fixme', $unusedVariables);
        $this->assertNotContains('var', $unusedVariables);
        $this->assertNotContains('list', $unusedVariables);
        $this->assertCount(4, $unusedVariables);
    }

    public function testTrappedFilenames()
    {
        $existingFiles = array('a.php', 'b.php-dist', 'c.php-dis', 'd.php-distt', 'e.php-dist.dist', 'f.dist', 'g-dist.php', 'h.php-dist-dist');

        foreach($existingFiles as $file)
        {
            $this->write($file);
        }

        $this->hydrator->hydrate('prod');

        $createdFiles = array('b.php', 'h.php-dist');
        $allFiles = array_merge($existingFiles, $createdFiles);

        // check there is no extra generated file
        $this->assertSame(count($createdFiles), count($this->targetFs->keys()));

        foreach($createdFiles as $file)
        {
            $this->assertTrue($this->targetFs->has($file), "File $file should be created");
        }
        
        foreach($existingFiles as $file)
        {
            $this->assertFalse($this->targetFs->has($file), "File $file should'nt have been overwritted");
        }
    }
    
    public function testTrappedFilenamesToTarget()
    {<commit_msg>fix: typo<commit_after><?php

namespace Karma;

use Gaufrette\Filesystem;
use Gaufrette\Adapter\InMemory;
use Karma\Configuration\InMemoryReader;
use Karma\FormatterProviders\NullProvider;
use Karma\FormatterProviders\CallbackProvider;
use Karma\Formatters\Rules;

class HydratorTest extends \PHPUnit_Framework_TestCase
{
    private
        $sourceFs,
        $targetFs,
        $hydrator;

    protected function setUp()
    {
        $this->sourceFs = new Filesystem(new InMemory());
        $this->targetFs = new Filesystem(new InMemory());
        $reader = new InMemoryReader(array(
            'var:dev' => 42,
            'var:preprod' => 51,
            'var:prod' => 69,
            'db.user:dev' => 'root',
            'db.user:preprod' => 'someUser',
            'bool:dev' => true,
            'bool:prod' => false,
            'list:dev' => array('str', 2, true, null),
            'list:prod' => array(42),
            'todo:dev' => '__TODO__',
            'fixme:dev' => '__FIXME__',
        ));

        $this->hydrator = new Hydrator($this->sourceFs, $this->targetFs, $reader, new Finder($this->sourceFs), new NullProvider());
        $this->hydrator->setSuffix('-dist');
    }

    /**
     * @dataProvider providerTestSimple
     */
    public function testSimple($environment, $expectedBValue, $expectedFValue)
    {
        $this->write('a.php');
        $this->write('b.php-dist', '<%var%>');
        $this->write('c.php', '<%var%>');
        $this->write('d.php-dist', 'var');
        $this->write('e.php-dist', '<%var %>');
        $this->write('f.php-dist', '<%db.user%>');
        $this->write('dir-dist/yolo.php', 'raw content');

        $this->hydrator->hydrate($environment);

        $this->assertTrue($this->targetFs->has('b.php'));
        $this->assertFalse($this->targetFs->has('c.php'));
        $this->assertTrue($this->targetFs->has('d.php'));
        $this->assertTrue($this->targetFs->has('e.php'));
        $this->assertTrue($this->targetFs->has('f.php'));

        $this->assertSame($expectedBValue, $this->targetFs->read('b.php'));

        $this->assertSame('<%var%>', $this->sourceFs->read('c.php'));
        
        $this->assertSame('var', $this->targetFs->read('d.php'));
        $this->assertSame('<%var %>', $this->targetFs->read('e.php'));
        $this->assertSame($expectedFValue, $this->targetFs->read('f.php'));
    }

    public function testTarget()
    {
        $this->write('a.php-dist', '<%var%>');
        $this->write('b.php', '<%var%>');
        $reader = new InMemoryReader([
            'var:dev' => 'test',
        ]);

        $hydrator = new Hydrator($this->sourceFs, $this->targetFs, $reader, new Finder($this->sourceFs), new NullProvider());
        $hydrator->allowNonDistFilesOverwrite();
        $hydrator->hydrate('dev');

        $this->assertTrue($this->targetFs->has('a.php'));
        $this->assertSame('test', $this->targetFs->read('a.php'));
        $this->assertTrue($this->targetFs->has('b.php'));
        $this->assertSame('test', $this->targetFs->read('b.php'));
    }

    public function providerTestSimple()
    {
        return array(
            array('dev', '42', 'root'),
            array('preprod', '51', 'someUser'),
        );
    }

    public function testDryRun()
    {
        $this->write('a.php');
        $this->write('b.php-dist', '<%var%>');
        $this->write('c.php', '<%var%>');

        $this->hydrator
            ->setDryRun()
            ->hydrate('dev');

        $this->assertFalse($this->targetFs->has('b.php'));
    }

    public function testGetUnusedVariables()
    {
        $this->write('a.php');
        $this->write('b.php-dist', '<%var%>');
        $this->write('c.php-dist', '<%list%>');

        $this->hydrator
            ->hydrate('dev');

        $unusedVariables = $this->hydrator->getUnusedVariables();

        $this->assertContains('db.user', $unusedVariables);
        $this->assertContains('bool', $unusedVariables);
        $this->assertContains('todo', $unusedVariables);
        $this->assertContains('fixme', $unusedVariables);
        $this->assertNotContains('var', $unusedVariables);
        $this->assertNotContains('list', $unusedVariables);
        $this->assertCount(4, $unusedVariables);
    }

    public function testTrappedFilenames()
    {
        $existingFiles = array('a.php', 'b.php-dist', 'c.php-dis', 'd.php-distt', 'e.php-dist.dist', 'f.dist', 'g-dist.php', 'h.php-dist-dist');

        foreach($existingFiles as $file)
        {
            $this->write($file);
        }

        $this->hydrator->hydrate('prod');

        $createdFiles = array('b.php', 'h.php-dist');
        $allFiles = array_merge($existingFiles, $createdFiles);

        // check there is no extra generated file
        $this->assertSame(count($createdFiles), count($this->targetFs->keys()));

        foreach($createdFiles as $file)
        {
            $this->assertTrue($this->targetFs->has($file), "File $file should be created");
        }
        
        foreach($existingFiles as $file)
        {
            $this->assertFalse($this->targetFs->has($file), "File $file should'nt be overwritten");
        }
    }
    
    public function testTrappedFilenamesToTarget()
    {
{'fraction_non_alphanumeric': 0.20222030816346062, 'fraction_numerical': 0.002679682266245574, 'mean_word_length': 2.602206135815236, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 253, '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': '6932845', 'n_tokens_mistral': 3404, 'n_tokens_neox': 3273, 'n_words': 482}