<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\Test;

use Elasticsearch\Common\Exceptions\ElasticsearchException;
use ONGR\ElasticsearchBundle\Client\Connection;
use ONGR\ElasticsearchBundle\ORM\Manager;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Base test which creates unique connection to test with.
 */
abstract class AbstractElasticsearchTestCase extends WebTestCase
{
    /**
     * @var Manager[] Holds used managers.
     */
    private $managers = [];

    /**
     * @var ContainerInterface
     */
    private $container;

    /**
     * {@inheritdoc}
     */
    public function runTest()
    {
        if ($this->getNumberOfRetries() < 1) {
            return parent::runTest();
        }

        foreach (range(1, $this->getNumberOfRetries()) as $try) {
            try {
                return parent::runTest();
            } catch (\Exception $e) {
                if (!($e instanceof ElasticsearchException)) {
                    throw $e;
                }
                // If error was from elasticsearch re-setup tests and retry.
                if ($try !== $this->getNumberOfRetries()) {
                    $this->tearDown();
                    $this->setUp();
                }
            }
        }

        throw $e;
    }

    /**
     * {@inheritdoc}
     */
    protected function setUp()
    {
        $this->getContainer();
        $this->getManager();
    }

    /**
     * Returns number of retries tests should execute.
     *
     * @return int
     */
    protected function getNumberOfRetries()
    {
        return 0;
    }

    /**
     * Can be overwritten in child class to populate elasticsearch index with data.
     *
     * Example:
     *      "managername" =>
     *      [
     *          'acmetype' => [
     *              [
     *                  '_id' => 1,
     *                  'title' => 'foo',
     *              ],
     *              [
     *                  '_id' => 2,
     *                  'title' => 'bar',
     *              ]
     *          ]
     *      ]
     *
     * @return array
     */
    protected function getDataArray()
    {
        return [];
    }

    /**
     * Ignores versions specified.
     *
     * Returns two dimensional array, first item in sub array is version to ignore, second is comparator.
     * Comparator types can be found in `version_compare` documentation.
     *
     * Example: [
     *   ['1.2.7', '<='],
     *   ['1.2.9', '==']
     * ]
     *
     * @return array
     */
    protected function getIgnoredVersions()
    {
        return [];
    }

    /**
     * Ignores version specified.
     *
     * @param Connection $connection
     */
    protected function ignoreVersions(Connection $connection)
    {
        $currentVersion = $connection->getVersionNumber();
        foreach ($this->getIgnoredVersions() as $ignoredVersion) {
            if (version_compare($currentVersion, $ignoredVersion[0], $ignoredVersion[1])) {
                $this->markTestSkipped("Elasticsearch version {$currentVersion} not supported by this test.");

                return;
            }
        }
    }

    /**
     * Removes manager from local cache and drops its index.
     *
     * @param string $name
     */
    protected function removeManager($name)
    {
        if (isset($this->managers[$name])) {
            $this->managers[$name]->getConnection()->dropIndex();
            unset($this->managers[$name]);
        }
    }

    /**
     * Populates elasticsearch with data.
     *
     * @param Manager $manager
     * @param array   $data<commit_msg>added test ignoring by test name in AbstractElasticsearchTestCase<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\Test;

use Elasticsearch\Common\Exceptions\ElasticsearchException;
use ONGR\ElasticsearchBundle\Client\Connection;
use ONGR\ElasticsearchBundle\ORM\Manager;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Base test which creates unique connection to test with.
 */
abstract class AbstractElasticsearchTestCase extends WebTestCase
{
    /**
     * @var Manager[] Holds used managers.
     */
    private $managers = [];

    /**
     * @var ContainerInterface
     */
    private $container;

    /**
     * {@inheritdoc}
     */
    public function runTest()
    {
        if ($this->getNumberOfRetries() < 1) {
            return parent::runTest();
        }

        foreach (range(1, $this->getNumberOfRetries()) as $try) {
            try {
                return parent::runTest();
            } catch (\Exception $e) {
                if (!($e instanceof ElasticsearchException)) {
                    throw $e;
                }
                // If error was from elasticsearch re-setup tests and retry.
                if ($try !== $this->getNumberOfRetries()) {
                    $this->tearDown();
                    $this->setUp();
                }
            }
        }

        throw $e;
    }

    /**
     * {@inheritdoc}
     */
    protected function setUp()
    {
        $this->getContainer();
        $this->getManager();
    }

    /**
     * Returns number of retries tests should execute.
     *
     * @return int
     */
    protected function getNumberOfRetries()
    {
        return 0;
    }

    /**
     * Can be overwritten in child class to populate elasticsearch index with data.
     *
     * Example:
     *      "managername" =>
     *      [
     *          'acmetype' => [
     *              [
     *                  '_id' => 1,
     *                  'title' => 'foo',
     *              ],
     *              [
     *                  '_id' => 2,
     *                  'title' => 'bar',
     *              ]
     *          ]
     *      ]
     *
     * @return array
     */
    protected function getDataArray()
    {
        return [];
    }

    /**
     * Ignores versions specified.
     *
     * Returns two dimensional array, first item in sub array is version to ignore, second is comparator,
     * last test name. If no test name is provided it will be used on all test class.
     *
     * Comparator types can be found in `version_compare` documentation.
     *
     * Example: [
     *   ['1.2.7', '<='],
     *   ['1.2.9', '==', 'testSmth']
     * ]
     *
     * @return array
     */
    protected function getIgnoredVersions()
    {
        return [];
    }

    /**
     * Ignores version specified.
     *
     * @param Connection $connection
     */
    protected function ignoreVersions(Connection $connection)
    {
        $currentVersion = $connection->getVersionNumber();
        $ignore = null;

        foreach ($this->getIgnoredVersions() as $ignoredVersion) {
            if (version_compare($currentVersion, $ignoredVersion[0], $ignoredVersion[1]) === true) {
                $ignore = true;
                if (isset($ignoredVersion[2])) {
                    if ($ignoredVersion[2] === $this->getName()) {
                        break;
                    }
                    $ignore = false;
                }
            }
        }

        if ($ignore === true) {
            $this->markTestSkipped("Elasticsearch version {$currentVersion} not supported by this test.");
        }
    }

    /**
     * Removes manager from local cache and drops its index.
     *
     * @param string $name
     */
    protected function removeManager($name)
    {
        if (isset($this->managers[$name])) {
            $this->managers[$name]->getConnection()->dropIndex();
            unset($this->managers[$name]);
        }
    }

    /**
     * Populates elasticsearch with data.
     *
     * @param Manager $manager
     * @param array   $data
{'fraction_non_alphanumeric': 0.12082574377656345, 'fraction_numerical': 0.003400121432908318, 'mean_word_length': 1.632992327365729, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 50, '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': '3383789', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2207, 'n_words': 549}