<commit_before><?php

namespace Environaut\Tests\Checks;

use Environaut\Report\Results\Messages\Message;
use Environaut\Tests\BaseTestCase;
use Environaut\Tests\Checks\Fixtures\TestableConfigurator;

class ConfiguratorTest extends BaseTestCase
{
    public function testConstruct()
    {
        $intro = 'some text for introductory purposes';
        $check = new \Environaut\Checks\Configurator('foo', array('introduction' => $intro));

        $this->assertEquals('foo', $check->getName());
        $this->assertInstanceOf('\Environaut\Report\Results\IResult', $check->getResult());
        $this->assertEquals(null, $check->getCommand());
    }

    public function testGroupSimpleValueQuestion()
    {
        $email = "<EMAIL>";
        $check = $this->runConfigurator(
            $email,
            array(
                'question' => 'Your email?',
                'setting' => 'core.email'
            ),
            'trololo'
        );

        $this->assertEquals('trololo', $check->getGroup());
        $this->assertContains('Your email?', $check->getOutput());
        $this->assertCount(1, $check->getResult()->getMessages());

        $settings = $check->getResult()->getSettingsAsArray();
        $this->assertCount(1, $settings, 'expected all settings when group is not specified');
        $settings = $check->getResult()->getSettingsAsArray('default');
        $this->assertCount(0, $settings, 'expected default group to be empty/nonexisting as "trololo" was the group name.');
        $settings = $check->getResult()->getSettingsAsArray('trololo');
        $this->assertCount(1, $settings, 'group "trololo" should contain the setting');
        $this->assertArrayHasKey('core.email', $settings);
        $this->assertEquals($email, $settings['core.email']);
    }

    public function testSimpleValueQuestion()
    {
        $email = "<EMAIL>";
        $check = $this->runConfigurator(
            $email,
            array(
                'question' => 'Your email?',
                'setting' => 'core.email'
            )
        );

        $this->assertContains('Your email?', $check->getOutput());
        $this->assertCount(1, $check->getResult()->getMessages());

        $settings = $check->getResult()->getSettingsAsArray('default');
        $this->assertCount(1, $settings);
        $this->assertArrayHasKey('core.email', $settings);
        $this->assertEquals($email, $settings['core.email']);
    }

    public function testSimpleValueQuestionWithFailedValidationAfterSomeAttempts()
    {
        $email = "nonworking-email";
        $this->setExpectedException('InvalidArgumentException');
        $this->runConfigurator(
            implode(
                PHP_EOL,
                array(
                    $email,
                    $email,
                    $email,
                    $email,
                    $email
                )
            ) . PHP_EOL,
            array(
                'question' => 'Your email?',
                'setting' => 'core.email',
                'validator' => 'Environaut\Checks\Validator::validEmail',
                'max_attempts' => 5
            )
        );
    }

    public function testSimpleValueQuestionWithSuccessValidationAfterSomeAttempts()
    {
        $email = "nonworking-email" . PHP_EOL;

        $check = $this->runConfigurator(
            implode(
                PHP_EOL,
                array(
                    $email,
                    $email,
                    $email,
                    $email,
                    "<EMAIL>" . PHP_EOL
                )
            ),
            array(
                'question' => 'Your email?',
                'setting' => 'core.email',
                'validator' => 'Environaut\Checks\Validator::validEmail',
                'max_attempts' => 5
            )
        );<commit_msg>removed PHP_EOL<commit_after><?php

namespace Environaut\Tests\Checks;

use Environaut\Report\Results\Messages\Message;
use Environaut\Tests\BaseTestCase;
use Environaut\Tests\Checks\Fixtures\TestableConfigurator;

class ConfiguratorTest extends BaseTestCase
{
    public function testConstruct()
    {
        $intro = 'some text for introductory purposes';
        $check = new \Environaut\Checks\Configurator('foo', array('introduction' => $intro));

        $this->assertEquals('foo', $check->getName());
        $this->assertInstanceOf('\Environaut\Report\Results\IResult', $check->getResult());
        $this->assertEquals(null, $check->getCommand());
    }

    public function testGroupSimpleValueQuestion()
    {
        $email = "<EMAIL>";
        $check = $this->runConfigurator(
            $email,
            array(
                'question' => 'Your email?',
                'setting' => 'core.email'
            ),
            'trololo'
        );

        $this->assertEquals('trololo', $check->getGroup());
        $this->assertContains('Your email?', $check->getOutput());
        $this->assertCount(1, $check->getResult()->getMessages());

        $settings = $check->getResult()->getSettingsAsArray();
        $this->assertCount(1, $settings, 'expected all settings when group is not specified');
        $settings = $check->getResult()->getSettingsAsArray('default');
        $this->assertCount(0, $settings, 'expected default group to be empty/nonexisting as "trololo" was the group name.');
        $settings = $check->getResult()->getSettingsAsArray('trololo');
        $this->assertCount(1, $settings, 'group "trololo" should contain the setting');
        $this->assertArrayHasKey('core.email', $settings);
        $this->assertEquals($email, $settings['core.email']);
    }

    public function testSimpleValueQuestion()
    {
        $email = "<EMAIL>";
        $check = $this->runConfigurator(
            $email,
            array(
                'question' => 'Your email?',
                'setting' => 'core.email'
            )
        );

        $this->assertContains('Your email?', $check->getOutput());
        $this->assertCount(1, $check->getResult()->getMessages());

        $settings = $check->getResult()->getSettingsAsArray('default');
        $this->assertCount(1, $settings);
        $this->assertArrayHasKey('core.email', $settings);
        $this->assertEquals($email, $settings['core.email']);
    }

    public function testSimpleValueQuestionWithFailedValidationAfterSomeAttempts()
    {
        $email = "nonworking-email";
        $this->setExpectedException('InvalidArgumentException');
        $this->runConfigurator(
            implode(
                PHP_EOL,
                array(
                    $email,
                    $email,
                    $email,
                    $email,
                    $email
                )
            ) . PHP_EOL,
            array(
                'question' => 'Your email?',
                'setting' => 'core.email',
                'validator' => 'Environaut\Checks\Validator::validEmail',
                'max_attempts' => 5
            )
        );
    }

    public function testSimpleValueQuestionWithSuccessValidationAfterSomeAttempts()
    {
        $email = "nonworking-email";

        $check = $this->runConfigurator(
            implode(
                PHP_EOL,
                array(
                    $email,
                    $email,
                    $email,
                    $email,
                    "<EMAIL>" . PHP_EOL
                )
            ),
            array(
                'question' => 'Your email?',
                'setting' => 'core.email',
                'validator' => 'Environaut\Checks\Validator::validEmail',
                'max_attempts' => 5
            )
        );
{'fraction_non_alphanumeric': 0.14083224967490246, 'fraction_numerical': 0.002080624187256177, 'mean_word_length': 1.9121544869367664, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 113, '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': '5006281', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2007, 'n_words': 354}