<commit_before><?php

namespace Oro\Bundle\FormBundle\Tests\Behat\Element;

use Behat\Mink\Element\NodeElement;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Element;

class Select2Entity extends Element
{
    /**
     * {@inheritdoc}
     */
    public function setValue($value)
    {
        $this->fillSearchField($value);
        $results = $this->getSuggestions();

        if (1 < count($results)) {
            // Try remove (Add new) records
            $results = array_filter($results, function (NodeElement $result) {
                return !stripos($result->getText(), 'Add new');
            });

            if (1 === count($results)) {
                array_shift($results)->click();
                $this->getDriver()->waitForAjax();
                return;
            }

            foreach ($results as $result) {
                if ($result->getText() == $value) {
                    $result->click();

                    return;
                }
            }

            self::fail(sprintf('Many results "%s" was found, but no one exactly match', $value));
        }

        self::assertNotCount(0, $results, sprintf('Not found result for "%s"', $value));

        array_shift($results)->click();
        $this->getDriver()->waitForAjax();
    }

    /**
     * @param string $value
     */
    public function fillSearchField($value)
    {
        $this->open();
        /** @var NodeElement[] $inputs */
        $inputs = array_filter(
            $this->getPage()->findAll('css', '.select2-search input'),
            function (NodeElement $element) {
                return $element->isVisible();
            }
        );

        self::assertCount(1, $inputs);
        array_shift($inputs)->setValue($value);
    }

    /**
     * {@inheritdoc}
     */
    public function selectOption($option, $multiple = false)
    {
        $this->setValue($option);
    }

    /**
     * @return NodeElement[]
     */
    public function getSuggestions()
    {
        $this->open();
        $this->getDriver()->waitForAjax();
        $this->waitFor(60, function () {
            return null === $this->getPage()->find('css', '.select2-results li.select2-searching');
        });
        $this->getDriver()->waitForAjax();

        /** @var NodeElement $resultSet */
        foreach ($this->getPage()->findAll('css', '.select2-results') as $resultSet) {
            if ($resultSet->isVisible()) {
                return $resultSet->findAll('css', 'li');
            }
        }

        return [];
    }

    /**
     * @return string[]
     */
    public function getSuggestedValues()
    {
        $suggestions = array_map(
            function (NodeElement $element) {
                return $element->getText();
            },
            $this->getSuggestions()
        );
        $this->close();

        return $suggestions;
    }

    public function open()
    {
        if (!$this->isOpen()) {
            $openArrow = $this->getParent()->find('css', '.select2-arrow');
            // Although ajax is already loaded element need some extra time to appear by js animation
            $openArrow->waitFor(1500, function (NodeElement $element) {
                return $element->isVisible();
            });
            $openArrow->click();
        }
    }

    public function close()
    {
        if ($dropDownMask = $this->getPage()->find('css', '.select2-drop-mask')) {
            $dropDownMask->click();
        } elseif ($this->isOpen()) {
            $this->getParent()->find('css', '.select2-arrow')->click();
        }
    }

    /**
     * @return bool
     */
    public function isOpen()
    {
        return 0 !== count(array_filter(
            $this->getPage()->findAll('css', '.select2-search'),
            function (NodeElement $element) {
                return $element->isVisible();
            }
        ));
    }
}<commit_msg>CRM-7538: Fixed code style notices - added new step context for entity add from plus dropdown - moved some logic to Select2Entity class - fixed phone, email checking - removed unnecessary check from create scenario - renamed inline edit scenario<commit_after><?php

namespace Oro\Bundle\FormBundle\Tests\Behat\Element;

use Behat\Mink\Element\NodeElement;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Element;

class Select2Entity extends Element
{
    /**
     * {@inheritdoc}
     */
    public function setValue($value)
    {
        $this->fillSearchField($value);
        $results = $this->getSuggestions();

        if (1 < count($results)) {
            // Try remove (Add new) records
            $results = array_filter($results, function (NodeElement $result) {
                return !stripos($result->getText(), 'Add new');
            });

            if (1 === count($results)) {
                array_shift($results)->click();
                $this->getDriver()->waitForAjax();
                return;
            }

            foreach ($results as $result) {
                if ($result->getText() == $value) {
                    $result->click();

                    return;
                }
            }

            self::fail(sprintf('Many results "%s" was found, but no one exactly match', $value));
        }

        self::assertNotCount(0, $results, sprintf('Not found result for "%s"', $value));

        array_shift($results)->click();
        $this->getDriver()->waitForAjax();
    }

    /**
     * @param string $value
     */
    public function fillSearchField($value)
    {
        $this->open();
        /** @var NodeElement[] $inputs */
        $inputs = array_filter(
            $this->getPage()->findAll('css', '.select2-search input'),
            function (NodeElement $element) {
                return $element->isVisible();
            }
        );

        self::assertCount(1, $inputs);
        array_shift($inputs)->setValue($value);
    }

    /**
     * {@inheritdoc}
     */
    public function selectOption($option, $multiple = false)
    {
        $this->setValue($option);
    }

    /**
     * @return NodeElement[]
     */
    public function getSuggestions()
    {
        $this->open();
        $this->getDriver()->waitForAjax();
        $this->waitFor(60, function () {
            return null === $this->getPage()->find('css', '.select2-results li.select2-searching');
        });
        $this->getDriver()->waitForAjax();

        /** @var NodeElement $resultSet */
        foreach ($this->getPage()->findAll('css', '.select2-results') as $resultSet) {
            if ($resultSet->isVisible()) {
                return $resultSet->findAll('css', 'li');
            }
        }

        return [];
    }

    /**
     * @return string[]
     */
    public function getSuggestedValues()
    {
        $suggestions = array_map(
            function (NodeElement $element) {
                return $element->getText();
            },
            $this->getSuggestions()
        );
        $this->close();

        return $suggestions;
    }

    public function open()
    {
        if (!$this->isOpen()) {
            $openArrow = $this->getParent()->find('css', '.select2-arrow');
            // Although ajax is already loaded element need some extra time to appear by js animation
            $openArrow->waitFor(1500, function (NodeElement $element) {
                return $element->isVisible();
            });
            $openArrow->click();
        }
    }

    public function close()
    {
        if ($dropDownMask = $this->getPage()->find('css', '.select2-drop-mask')) {
            $dropDownMask->click();
        } elseif ($this->isOpen()) {
            $this->getParent()->find('css', '.select2-arrow')->click();
        }
    }

    /**
     * @return bool
     */
    public function isOpen()
    {
        return 0 !== count(array_filter(
            $this->getPage()->findAll('css', '.select2-search'),
            function (NodeElement $element) {
                return $element->isVisible();
            }
        ));
    }

    public function openFromPlusButtonDropDown($buttonName)
    {
        $content = $this->getPage();
        $content->find('css', '.entity-create-dropdown button')->click();
        $this->getPage()->pressButton($buttonName);
    }
}
{'fraction_non_alphanumeric': 0.1586491739552964, 'fraction_numerical': 0.005466472303206997, 'mean_word_length': 1.8321293429652563, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 104, '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': '2142241', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2200, 'n_words': 449}