<commit_before><?php

/*
 * This file is part of the ONGR package.
 *
 * (c) NFQ Technologies UAB <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ONGR\ElasticsearchBundle\Tests\Unit\DependencyInjection;

use ONGR\ElasticsearchBundle\DependencyInjection\ONGRElasticsearchExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Unit tests for ElasticsearchExtension.
 */
class ElasticsearchExtensionTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Check if load adds parameters to container as expected.
     */
    public function testLoad()
    {
        $container = new ContainerBuilder();
        $container->setParameter('kernel.bundles', []);
        $container->setParameter('kernel.cache_dir', '');
        $container->setParameter('kernel.debug', true);
        $extension = new ONGRElasticsearchExtension();
        $extension->load(
            [
                'elasticsearch' => [
                    'document_dir' => 'customDir',
                    'connections' => [
                        'test' => ['index_name' => 'test'],
                    ],
                    'managers' => [
                        'test' => [
                            'connection' => 'test2',
                            'debug' => false,
                            'readonly' => false,
                            'mappings' => ['testBundle'],
                        ],
                    ],
                ],
            ],
            $container
        );

        $expectedConnections = [
            'test' => [
                'index_name' => 'test',
                'hosts' => ['127.0.0.1:9200'],
                'settings' => [],
            ],
        ];
        $expectedManagers = [
            'test' => [
                'connection' => 'test2',
                'debug' => false,
                'readonly' => false,
                'mappings' => ['testBundle'],
            ],
        ];

        $this->assertEquals(
            $expectedConnections,
            $container->getParameter('es.connections'),
            'Incorrect connections parameter.'
        );
        $this->assertEquals(
            $expectedManagers,
            $container->getParameter('es.managers'),<commit_msg>Additional tests for ElasticsearchExtension class<commit_after><?php

/*
 * This file is part of the ONGR package.
 *
 * (c) NFQ Technologies UAB <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ONGR\ElasticsearchBundle\Tests\Unit\DependencyInjection;

use ONGR\ElasticsearchBundle\DependencyInjection\ONGRElasticsearchExtension;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Unit tests for ElasticsearchExtension.
 */
class ElasticsearchExtensionTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @return array
     */
    public function getData()
    {
        $parameters = [
            'elasticsearch' => [
                'document_dir' => 'customDir',
                'connections' => [
                    'test' => ['index_name' => 'test'],
                ],
                'managers' => [
                    'test' => [
                        'connection' => 'test2',
                        'debug' => true,
                        'readonly' => false,
                        'mappings' => ['testBundle'],
                    ],
                ],
            ],
        ];

        $expectedConnections = [
            'test' => [
                'index_name' => 'test',
                'hosts' => ['127.0.0.1:9200'],
                'settings' => [],
            ],
        ];

        $expectedManagers = [
            'test' => [
                'connection' => 'test2',
                'debug' => true,
                'readonly' => false,
                'mappings' => ['testBundle'],
            ],
        ];

        $out[] = [
            $parameters,
            $expectedConnections,
            $expectedManagers,
        ];

        $parameters = [
            'elasticsearch' => [
                'document_dir' => 'customDir',
                'connections' => [
                    'test' => ['index_name' => 'test'],
                ],
                'managers' => [
                    'test' => [
                        'connection' => 'test2',
                        'debug' => false,
                        'readonly' => false,
                        'mappings' => ['testBundle'],
                    ],
                ],
            ],
        ];

        $expectedManagers = [
            'test' => [
                'connection' => 'test2',
                'debug' => false,
                'readonly' => false,
                'mappings' => ['testBundle'],
            ],
        ];

        $out[] = [
            $parameters,
            $expectedConnections,
            $expectedManagers,
        ];

        return $out;
    }

    /**
     * Check if load adds parameters to container as expected.
     *
     * @param array $parameters
     * @param array $expectedConnections
     * @param array $expectedManagers
     *
     * @dataProvider getData
     */
    public function testLoad($parameters, $expectedConnections, $expectedManagers)
    {
        $container = new ContainerBuilder();
        class_exists('testClass') ? : eval('class testClass {}');
        $container->setParameter('kernel.bundles', ['testBundle' => 'testClass']);
        $container->setParameter('kernel.cache_dir', '');
        $container->setParameter('kernel.debug', true);
        $extension = new ONGRElasticsearchExtension();
        $extension->load(
            $parameters,
            $container
        );

        if ($parameters['elasticsearch']['managers']['test']['debug']) {
            $reflection = new \ReflectionClass($this);
            $dir = dirname($reflection->getFileName()) . DIRECTORY_SEPARATOR . 'customDir';

            $handler = new Definition('ONGR\ElasticsearchBundle\Logger\Handler\CollectionHandler', []);
            $logger = new Definition(
                'Monolog\Logger',
                [
                    'tracer',
                    [$handler],
                ]
            );

            $collector = new Definition('ONGR\ElasticsearchBundle\DataCollector\ElasticsearchDataCollector');
            $collector->addMethodCall('setManagers', [new Parameter('es.managers')]);
            $collector->addMethodCall('addLogger', [new Reference('es.logger.trace')]);
            $collector->addTag(
                'data_collector',
                [
                    'template' => 'ONGRElasticsearchBundle:Profiler:profiler.html.twig',
                    'id' => 'es',
                ]
            );

            $this->assertEquals(
                $collector,
                $container->getDefinition('es.collector')
            );
            $this->assertEquals(
                $logger,
                $container->getDefinition('es.logger.trace')
            );
            $this->assertEquals(
                new DirectoryResource($dir),
                $container->getResources()[1]
            );
        }

        $this->assertEquals(
            $expectedConnections,
            $container->getParameter('es.connections'),
            'Incorrect connections parameter.'
        );
        $this->assertEquals(
            $expectedManagers,
            $container->getParameter('es.managers'),
{'fraction_non_alphanumeric': 0.1285604311008468, 'fraction_numerical': 0.003464203233256351, 'mean_word_length': 1.5128949065119277, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 92, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3954448', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1880, 'n_words': 345}