<commit_before><?php

/*
 * This file is part of the RollerworksPasswordStrengthBundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Rollerworks\Bundle\PasswordStrengthBundle\Tests\DependencyInjection;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Rollerworks\Bundle\PasswordStrengthBundle\DependencyInjection\RollerworksPasswordStrengthExtension;
use Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints\Blacklist as BlacklistConstraint;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\Validator\Tests\Fixtures\Reference;

class ExtensionTest extends AbstractExtensionTestCase
{
    public function testLoadDefaultConfiguration()
    {
        $this->load();
        $this->compile();

        $this->assertContainerBuilderHasService('rollerworks_password_strength.blacklist.validator');
        $this->assertContainerBuilderHasService(
            'rollerworks_password_strength.blacklist_provider',
            'Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\NoopProvider'
        );

        $constraint = new BlacklistConstraint();
        $this->assertContainerBuilderHasService($constraint->validatedBy());
    }

    public function testLoadWithSqliteConfiguration()
    {
        $this->load(array(
            'blacklist' => array(
                'default_provider' => 'rollerworks_password_strength.blacklist.provider.sqlite',
                'providers' => array(
                    'sqlite' => array('dsn' => 'sqlite:something'),
                ),
            ),
        ));

        $this->compile();

        $this->assertContainerBuilderHasService('rollerworks_password_strength.blacklist.validator');
        $this->assertContainerBuilderHasService(
            'rollerworks_password_strength.blacklist_provider',
            'Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\SqliteProvider'
        );
    }

    public function testLoadWithArrayConfiguration()
    {
        $this->load(array(
            'blacklist' => array(
                'default_provider' => 'rollerworks_password_strength.blacklist.provider.array',
                'providers' => array(
                    'array' => array('foo', 'foobar', 'kaboom'),
                ),
            ),
        ));

        $this->compile();

        $this->assertContainerBuilderHasService('rollerworks_password_strength.blacklist.validator');
        $this->assertContainerBuilderHasService(
            'rollerworks_password_strength.blacklist_provider',
            'Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\ArrayProvider'
        );

        $provider = $this->container->get('rollerworks_password_strength.blacklist_provider');

        self::assertTrue($provider->isBlacklisted('foo'));
        self::assertTrue($provider->isBlacklisted('foobar'));
        self::assertTrue($provider->isBlacklisted('kaboom'));
        self::assertFalse($provider->isBlacklisted('leeRoy'));
    }

    public function testLoadWithChainConfiguration()
    {
        $this->load(array(
            'blacklist' => array(
                'default_provider' => 'rollerworks_password_strength.blacklist.provider.chain',
                'providers' => array(
                    'array' => array('foo', 'foobar', 'kaboom'),
                    'chain' => array(
                        'providers' => array(
                            'rollerworks_password_strength.blacklist.provider.array',
                            'acme.password_blacklist.array',
                        ),
                    ),
                ),
            ),
        ));

        $this->container->set(
            'acme.password_blacklist.array',
            new \Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\ArrayProvider(array('amy', 'doctor', 'rory'))
        );

        $this->compile();

        $this->assertContainerBuilderHasService('rollerworks_password_strength.blacklist.validator');
        $this->assertContainerBuilderHasService(
            'rollerworks_password_strength.blacklist_provider',
            'Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\ChainProvider'
        );

        $provider = $this->container->get('rollerworks_password_strength.blacklist_provider');
        self::assertTrue($provider->isBlacklisted('foo'));
        self::assertTrue($provider->isBlacklisted('foobar'));
        self::assertTrue($provider->isBlacklisted('kaboom'));
        self::assertTrue($provider->isBlacklisted('doctor'));
        self::assertFalse($provider->isBlacklisted('leeRoy'));
    }

    public function testPasswordValidatorsAreRegistered()
    {
        $this->container->addCompilerPass(new AddConstraintValidatorsPass());
        $this->container->register(
            'validator.validator_factory',
            'Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory'
        )->setArguments(array(new Reference('service_container'), array()));

        $this->load();
        $this->compile();

        $validatorFactory = $this->container->getDefinition('validator.validator_factory');
        $factoryArguments = $validatorFactory->getArguments();

        // Compatibility for Symfony 3.3
        if ($factoryArguments[0] instanceof ServiceLocatorArgument) {
            $validators = $factoryArguments[0]->getValues();

            $this->assertArrayHasKey('Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints\PasswordStrengthValidator', $validators);
            $this->assertArrayHasKey('Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints\BlacklistValidator', $validators);
        } else {
            $validators = array_values($factoryArguments[1]);

            // Use only the service-id as the alias is considered deprecated.
            // https://github.com/symfony/symfony/issues/16805
            $this->assertContains('rollerworks_password_strength.validator.password_strength', $validators);
            $this->assertContains('rollerworks_password_strength.blacklist.validator', $validators);<commit_msg>bug #76 Fix tests failure for Symfony 3.3-dev (sstok)<commit_after><?php

/*
 * This file is part of the RollerworksPasswordStrengthBundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Rollerworks\Bundle\PasswordStrengthBundle\Tests\DependencyInjection;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Rollerworks\Bundle\PasswordStrengthBundle\DependencyInjection\RollerworksPasswordStrengthExtension;
use Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints\Blacklist as BlacklistConstraint;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Validator\Tests\Fixtures\Reference;

class ExtensionTest extends AbstractExtensionTestCase
{
    public function testLoadDefaultConfiguration()
    {
        $this->load();
        $this->compile();

        $this->assertContainerBuilderHasService('rollerworks_password_strength.blacklist.validator');
        $this->assertContainerBuilderHasService(
            'rollerworks_password_strength.blacklist_provider',
            'Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\NoopProvider'
        );

        $constraint = new BlacklistConstraint();
        $this->assertContainerBuilderHasService($constraint->validatedBy());
    }

    public function testLoadWithSqliteConfiguration()
    {
        $this->load(array(
            'blacklist' => array(
                'default_provider' => 'rollerworks_password_strength.blacklist.provider.sqlite',
                'providers' => array(
                    'sqlite' => array('dsn' => 'sqlite:something'),
                ),
            ),
        ));

        $this->compile();

        $this->assertContainerBuilderHasService('rollerworks_password_strength.blacklist.validator');
        $this->assertContainerBuilderHasService(
            'rollerworks_password_strength.blacklist_provider',
            'Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\SqliteProvider'
        );
    }

    public function testLoadWithArrayConfiguration()
    {
        $this->load(array(
            'blacklist' => array(
                'default_provider' => 'rollerworks_password_strength.blacklist.provider.array',
                'providers' => array(
                    'array' => array('foo', 'foobar', 'kaboom'),
                ),
            ),
        ));

        $this->compile();

        $this->assertContainerBuilderHasService('rollerworks_password_strength.blacklist.validator');
        $this->assertContainerBuilderHasService(
            'rollerworks_password_strength.blacklist_provider',
            'Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\ArrayProvider'
        );

        $provider = $this->container->get('rollerworks_password_strength.blacklist_provider');

        self::assertTrue($provider->isBlacklisted('foo'));
        self::assertTrue($provider->isBlacklisted('foobar'));
        self::assertTrue($provider->isBlacklisted('kaboom'));
        self::assertFalse($provider->isBlacklisted('leeRoy'));
    }

    public function testLoadWithChainConfiguration()
    {
        $this->load(array(
            'blacklist' => array(
                'default_provider' => 'rollerworks_password_strength.blacklist.provider.chain',
                'providers' => array(
                    'array' => array('foo', 'foobar', 'kaboom'),
                    'chain' => array(
                        'providers' => array(
                            'rollerworks_password_strength.blacklist.provider.array',
                            'acme.password_blacklist.array',
                        ),
                    ),
                ),
            ),
        ));

        $this->container->set(
            'acme.password_blacklist.array',
            new \Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\ArrayProvider(array('amy', 'doctor', 'rory'))
        );

        $this->compile();

        $this->assertContainerBuilderHasService('rollerworks_password_strength.blacklist.validator');
        $this->assertContainerBuilderHasService(
            'rollerworks_password_strength.blacklist_provider',
            'Rollerworks\Bundle\PasswordStrengthBundle\Blacklist\ChainProvider'
        );

        $provider = $this->container->get('rollerworks_password_strength.blacklist_provider');
        self::assertTrue($provider->isBlacklisted('foo'));
        self::assertTrue($provider->isBlacklisted('foobar'));
        self::assertTrue($provider->isBlacklisted('kaboom'));
        self::assertTrue($provider->isBlacklisted('doctor'));
        self::assertFalse($provider->isBlacklisted('leeRoy'));
    }

    public function testPasswordValidatorsAreRegistered()
    {
        $this->container->addCompilerPass(new AddConstraintValidatorsPass());
        $this->container->register(
            'validator.validator_factory',
            'Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory'
        )->setArguments(array(new Reference('service_container'), array()));

        $this->load();
        $this->compile();

        $validatorFactory = $this->container->getDefinition('validator.validator_factory');
        $factoryArguments = $validatorFactory->getArguments();

        // Compatibility for Symfony 3.3
        if ($factoryArguments[0] instanceof Definition) {
            $validators = $factoryArguments[0]->getArgument(0);

            $this->assertArrayHasKey('Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints\PasswordStrengthValidator', $validators);
            $this->assertArrayHasKey('Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints\BlacklistValidator', $validators);
        } else {
            $validators = array_values($factoryArguments[1]);

            // Use only the service-id as the alias is considered deprecated.
            // https://github.com/symfony/symfony/issues/16805
            $this->assertContains('rollerworks_password_strength.validator.password_strength', $validators);
            $this->assertContains('rollerworks_password_strength.blacklist.validator', $validators);
{'fraction_non_alphanumeric': 0.11687898089171975, 'fraction_numerical': 0.0019904458598726115, 'mean_word_length': 3.020806658130602, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 135, 'https://': 2, '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': '7381333', 'n_tokens_mistral': 3225, 'n_tokens_neox': 3103, 'n_words': 397}