<commit_before><?php

namespace Gedmo\Translatable;

use Doctrine\Common\EventManager;
use Tool\BaseTestCaseORM;
use Doctrine\Common\Util\Debug,
    Translatable\Fixture\StringIdentifier;

/**
 * These are tests for translatable behavior
 *
 * @author <NAME> <<EMAIL>>
 * @package Gedmo.Translatable
 * @link http://www.gediminasm.org
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class TranslatableIdentifierTest extends BaseTestCaseORM
{
    const FIXTURE = 'Translatable\\Fixture\\StringIdentifier';
    const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';

    private $testObjectId;
    private $translatableListener;

    protected function setUp()
    {
        parent::setUp();

        $evm = new EventManager;
        $this->translatableListener = new TranslatableListener();
        $this->translatableListener->setTranslatableLocale('en_us');
        $this->translatableListener->setDefaultLocale('en_us');
        $evm->addEventSubscriber($this->translatableListener);

        $this->getMockSqliteEntityManager($evm);
    }

    public function testStringIdentifier()
    {
        $object = new StringIdentifier();
        $object->setTitle('title in en');
        $object->setUid(md5(self::FIXTURE . time()));

        $this->em->persist($object);
        $this->em->flush();
        $this->em->clear();
        $this->testObjectId = $object->getUid();

        $repo = $this->em->getRepository(self::TRANSLATION);
        $object = $this->em->find(self::FIXTURE, $this->testObjectId);

        $translations = $repo->findTranslations($object);
        $this->assertEquals(count($translations), 0);

        $object = $this->em->find(self::FIXTURE, $this->testObjectId);
        $object->setTitle('title in de');
        $object->setTranslatableLocale('de_de');

        $this->em->persist($object);
        $this->em->flush();
        $this->em->clear();

        $repo = $this->em->getRepository(self::TRANSLATION);

        // test the entity load by translated title
        $object = $repo->findObjectByTranslatedField(
            'title',
            'title in de',
            self::FIXTURE
        );

        $this->assertEquals($this->testObjectId, $object->getUid());

        $translations = $repo->findTranslations($object);
        $this->assertEquals(count($translations), 1);
        $this->assertArrayHasKey('de_de', $translations);

        $this->assertArrayHasKey('title', $translations['de_de']);
        $this->assertEquals('title in de', $translations['de_de']['title']);

        // dql test object hydration
        $q = $this->em->createQuery('SELECT si FROM ' . self::FIXTURE . ' si WHERE si.uid = :id');
        $data = $q->execute(
            array('id' => $this->testObjectId),
            \Doctrine\ORM\Query::HYDRATE_OBJECT
        );
        $this->assertEquals(count($data), 1);
        $object = $data[0];
        $this->assertEquals('title in en', $object->getTitle());

        $this->translatableListener->setTranslatableLocale('de_de');
        $q = $this->em->createQuery('SELECT si FROM ' . self::FIXTURE . ' si WHERE si.uid = :id');
        $data = $q->execute(
            array('id' => $this->testObjectId),
            \Doctrine\ORM\Query::HYDRATE_OBJECT
        );
        $this->assertEquals(count($data), 1);
        $object = $data[0];
        $this->assertEquals('title in de', $object->getTitle());
    }<commit_msg>[tests] doctrine query cache improved in 2.3.0 version, fixing a test<commit_after><?php

namespace Gedmo\Translatable;

use Doctrine\Common\EventManager;
use Tool\BaseTestCaseORM;
use Doctrine\Common\Util\Debug,
    Translatable\Fixture\StringIdentifier;

/**
 * These are tests for translatable behavior
 *
 * @author <NAME> <<EMAIL>>
 * @package Gedmo.Translatable
 * @link http://www.gediminasm.org
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class TranslatableIdentifierTest extends BaseTestCaseORM
{
    const FIXTURE = 'Translatable\\Fixture\\StringIdentifier';
    const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';

    private $testObjectId;
    private $translatableListener;

    protected function setUp()
    {
        parent::setUp();

        $evm = new EventManager;
        $this->translatableListener = new TranslatableListener();
        $this->translatableListener->setTranslatableLocale('en_us');
        $this->translatableListener->setDefaultLocale('en_us');
        $evm->addEventSubscriber($this->translatableListener);

        $conn = array(
                    'driver' => 'pdo_mysql',
                    'host' => '127.0.0.1',
                    'dbname' => 'test',
                    'user' => 'root',
                    'password' => '<PASSWORD>'
        );
        //$this->getMockCustomEntityManager($conn, $evm);
        $this->getMockSqliteEntityManager($evm);
    }

    /**
     * @test
     */
    function shouldHandleStringIdentifier()
    {
        $object = new StringIdentifier();
        $object->setTitle('title in en');
        $object->setUid(md5(self::FIXTURE . time()));

        $this->em->persist($object);
        $this->em->flush();
        $this->em->clear();
        $this->testObjectId = $object->getUid();

        $repo = $this->em->getRepository(self::TRANSLATION);
        $object = $this->em->find(self::FIXTURE, $this->testObjectId);

        $translations = $repo->findTranslations($object);
        $this->assertEquals(count($translations), 0);

        $object = $this->em->find(self::FIXTURE, $this->testObjectId);
        $object->setTitle('title in de');
        $object->setTranslatableLocale('de_de');

        $this->em->persist($object);
        $this->em->flush();
        $this->em->clear();

        $repo = $this->em->getRepository(self::TRANSLATION);

        // test the entity load by translated title
        $object = $repo->findObjectByTranslatedField(
            'title',
            'title in de',
            self::FIXTURE
        );

        $this->assertEquals($this->testObjectId, $object->getUid());

        $translations = $repo->findTranslations($object);
        $this->assertEquals(count($translations), 1);
        $this->assertArrayHasKey('de_de', $translations);

        $this->assertArrayHasKey('title', $translations['de_de']);
        $this->assertEquals('title in de', $translations['de_de']['title']);

        // dql test object hydration
        $q = $this->em
            ->createQuery('SELECT si FROM ' . self::FIXTURE . ' si WHERE si.uid = :id')
            ->setParameter('id', $this->testObjectId)
            ->useResultCache(false)
        ;
        $data = $q->getResult();
        $this->assertEquals(count($data), 1);
        $object = $data[0];
        $this->assertEquals('title in en', $object->getTitle());

        $this->em->clear(); // based on 2.3.0 it caches in identity map
        $this->translatableListener->setTranslatableLocale('de_de');
        $data = $q->getResult();
        $this->assertEquals(count($data), 1);
        $object = $data[0];
        $this->assertEquals('title in de', $object->getTitle());
    }
{'fraction_non_alphanumeric': 0.16573191250701066, 'fraction_numerical': 0.0036455412226584407, 'mean_word_length': 2.7720782654680063, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 1, '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': '10741', 'n_tokens_mistral': 2158, 'n_tokens_neox': 2098, 'n_words': 360}