<commit_before><?php

namespace Oro\Bundle\LocaleBundle\Tests\EventListener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

use Oro\Bundle\LocaleBundle\EventListener\LocaleListener;

class LocaleListenerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var LocaleListener
     */
    protected $listener;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $localeSettings;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $transListener;

    /**
     * @var string
     */
    protected $defaultLocale;

    protected function setUp()
    {
        $this->localeSettings = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Model\LocaleSettings')
            ->disableOriginalConstructor()
            ->getMock();
        $this->transListener = $this->getMock('Gedmo\Translatable\TranslatableListener');

        $this->defaultLocale = \Locale::getDefault();
    }

    protected function tearDown()
    {
        \Locale::setDefault($this->defaultLocale);
    }

    /**
     * @param mixed $installed
     * @param bool $isSetLocale
     * @dataProvider onKernelRequestDataProvider
     */
    public function testOnKernelRequest($installed, $isSetLocale)
    {
        $customLanguage = 'ru';
        $customLocale = 'fr';

        $request = new Request();
        $request->setDefaultLocale($this->defaultLocale);

        if ($isSetLocale) {
            $this->localeSettings->expects($this->exactly(2))->method('getLanguage')
                ->will($this->returnValue($customLanguage));
            $this->localeSettings->expects($this->once())->method('getLocale')
                ->will($this->returnValue($customLocale));
        } else {
            $this->localeSettings->expects($this->never())->method('getLanguage');
            $this->localeSettings->expects($this->never())->method('getLocale');
        }

        $this->listener = new LocaleListener(
            $this->localeSettings,
            $this->transListener,
            $installed
        );
        $this->listener->onKernelRequest($this->createGetResponseEvent($request));

        if ($isSetLocale) {
            $this->assertEquals($customLanguage, $request->getLocale());
            $this->assertEquals($customLocale, \Locale::getDefault());
        } else {
            $this->assertEquals($this->defaultLocale, $request->getLocale());
            $this->assertEquals($this->defaultLocale, \Locale::getDefault());
        }
    }

    public function onKernelRequestDataProvider()
    {
        return array(
            'application not installed with null' => array(
                'installed' => null,
                'isSetLocale' => false,
            ),
            'application not installed with false' => array(
                'installed' => false,
                'isSetLocale' => false,
            ),
            'application installed with flag' => array(
                'installed' => true,
                'isSetLocale' => true,
            ),
            'application installed with date' => array(
                'installed' => '2012-12-12T12:12:12+02:00',
                'isSetLocale' => true,
            ),
        );
    }

    public function testOnConsoleCommand()
    {
        $this->listener = new LocaleListener(
            $this->localeSettings,
            $this->transListener,
            true
        );

        $event = $this
            ->getMockBuilder('Symfony\Component\Console\Event\ConsoleCommandEvent')
            ->disableOriginalConstructor()
            ->getMock();

        $this->localeSettings
            ->expects($this->once())
            ->method('getLocale');


        $this->localeSettings
            ->expects($this->once())
            ->method('getLanguage');

        $this->transListener
            ->expects($this->once())
            ->method('setTranslatableLocale');

        $this->listener->onConsoleCommand($event);
    }

    /**
     * @param Request $request
     * @return GetResponseEvent
     */
    protected function createGetResponseEvent(Request $request)
    {
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
            ->disableOriginalConstructor()
            ->setMethods(array('getRequest'))
            ->getMock();

        $event->expects($this->once())
            ->method('getRequest')<commit_msg>BAP-2758: Gedmo Translations don't work - fix unit tests<commit_after><?php

namespace Oro\Bundle\LocaleBundle\Tests\EventListener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

use Oro\Bundle\LocaleBundle\EventListener\LocaleListener;

class LocaleListenerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var LocaleListener
     */
    protected $listener;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $localeSettings;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $transListener;

    /**
     * @var string
     */
    protected $defaultLocale;

    protected function setUp()
    {
        $this->localeSettings = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Model\LocaleSettings')
            ->disableOriginalConstructor()
            ->getMock();
        $this->transListener = $this->getMock('Gedmo\Translatable\TranslatableListener');

        $this->defaultLocale = \Locale::getDefault();
    }

    protected function tearDown()
    {
        \Locale::setDefault($this->defaultLocale);
    }

    /**
     * @param mixed $installed
     * @param bool $isSetLocale
     * @dataProvider onKernelRequestDataProvider
     */
    public function testOnKernelRequest($installed, $isSetLocale)
    {
        $customLanguage = 'ru';
        $customLocale = 'fr';

        $request = new Request();
        $request->setDefaultLocale($this->defaultLocale);

        if ($isSetLocale) {
            $this->localeSettings->expects($this->exactly(2))->method('getLanguage')
                ->will($this->returnValue($customLanguage));
            $this->localeSettings->expects($this->once())->method('getLocale')
                ->will($this->returnValue($customLocale));
        } else {
            $this->localeSettings->expects($this->never())->method('getLanguage');
            $this->localeSettings->expects($this->never())->method('getLocale');
        }

        $this->listener = new LocaleListener(
            $this->localeSettings,
            $this->transListener,
            $installed
        );
        $this->listener->onKernelRequest($this->createGetResponseEvent($request));

        if ($isSetLocale) {
            $this->assertEquals($customLanguage, $request->getLocale());
            $this->assertEquals($customLocale, \Locale::getDefault());
        } else {
            $this->assertEquals($this->defaultLocale, $request->getLocale());
            $this->assertEquals($this->defaultLocale, \Locale::getDefault());
        }
    }

    public function onKernelRequestDataProvider()
    {
        return array(
            'application not installed with null' => array(
                'installed' => null,
                'isSetLocale' => false,
            ),
            'application not installed with false' => array(
                'installed' => false,
                'isSetLocale' => false,
            ),
            'application installed with flag' => array(
                'installed' => true,
                'isSetLocale' => true,
            ),
            'application installed with date' => array(
                'installed' => '2012-12-12T12:12:12+02:00',
                'isSetLocale' => true,
            ),
        );
    }

    public function testOnConsoleCommand()
    {
        $this->listener = new LocaleListener(
            $this->localeSettings,
            $this->transListener,
            true
        );

        $event = $this
            ->getMockBuilder('Symfony\Component\Console\Event\ConsoleCommandEvent')
            ->disableOriginalConstructor()
            ->getMock();

        $input = $this
            ->getMockBuilder('Symfony\Component\Console\Input\InputInterface')
            ->disableOriginalConstructor()
            ->getMock();

        $event
            ->expects($this->once())
            ->method('getInput')
            ->will($this->returnValue($input));

        $input
            ->expects($this->once())
            ->method('hasParameterOption')
            ->will($this->returnValue(false));

        $this->localeSettings
            ->expects($this->once())
            ->method('getLocale');

        $this->localeSettings
            ->expects($this->once())
            ->method('getLanguage');

        $this->transListener
            ->expects($this->once())
            ->method('setTranslatableLocale');

        $this->listener->onConsoleCommand($event);
    }

    /**
     * @param Request $request
     * @return GetResponseEvent
     */
    protected function createGetResponseEvent(Request $request)
    {
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
            ->disableOriginalConstructor()
            ->setMethods(array('getRequest'))
            ->getMock();

        $event->expects($this->once())
            ->method('getRequest')
{'fraction_non_alphanumeric': 0.13983860692291358, 'fraction_numerical': 0.004459545551072415, 'mean_word_length': 2.164986559139785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 182, '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': '7252209', 'n_tokens_mistral': 2478, 'n_tokens_neox': 2383, 'n_words': 367}