<commit_before><?php

namespace Doctrine\Tests\ORM\Performance;

use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\Models\Cache\Country;
use Doctrine\Tests\Models\Cache\State;
use Doctrine\Tests\Models\Cache\City;
use Doctrine\ORM\EntityManagerInterface;

/**
 * @group DDC-2183
 * @group performance
 */
class SecondLevelCacheTest extends OrmFunctionalTestCase
{
    protected function setUp()
    {
        parent::useModelSet('cache');
        parent::setUp();
    }

    /**
     * @return \Doctrine\ORM\EntityManagerInterface
     */
    public function createEntityManager()
    {
        $logger = new DebugStack();
        $em     = $this->_getEntityManager();

        $em->getConnection()->getConfiguration()->setSQLLogger($logger);
        $em->getConfiguration()->setSQLLogger($logger);

        return $em;
    }

    /**
     * @param \Doctrine\ORM\EntityManagerInterface $em
     * @return integer
     */
    public function countQuery(EntityManagerInterface $em)
    {
        return count($em->getConfiguration()->getSQLLogger()->queries);
    }

    public function testFindEntityWithoutCache()
    {
        $em = $this->createEntityManager();

        $this->findEntity($em, __FUNCTION__);

        $this->assertEquals(6002, $this->countQuery($em));
    }

    public function testFindEntityWithCache()
    {
        parent::enableSecondLevelCache(false);

        $em = $this->createEntityManager();

        $this->findEntity($em, __FUNCTION__);

        $this->assertEquals(502, $this->countQuery($em));
    }

    public function testFindAllEntityWithoutCache()
    {
        $em = $this->createEntityManager();

        $this->findAllEntity($em, __FUNCTION__);

        $this->assertEquals(153, $this->countQuery($em));
    }

    public function testFindAllEntityWithCache()
    {
        parent::enableSecondLevelCache(false);

        $em = $this->createEntityManager();

        $this->findAllEntity($em, __FUNCTION__);

        $this->assertEquals(53, $this->countQuery($em));
    }

    public function testFindEntityOneToManyWithoutCache()
    {
        $em = $this->createEntityManager();

        $this->findEntityOneToMany($em, __FUNCTION__);

        $this->assertEquals(502, $this->countQuery($em));
    }

    public function testFindEntityOneToManyWithCache()
    {
        parent::enableSecondLevelCache(false);

        $em = $this->createEntityManager();

        $this->findEntityOneToMany($em, __FUNCTION__);

        $this->assertEquals(487, $this->countQuery($em));
    }

    public function testQueryEntityWithoutCache()
    {<commit_msg>Corrected SLC test expected query count: since the identity map is now consistently hashing identifiers<commit_after><?php

namespace Doctrine\Tests\ORM\Performance;

use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\Models\Cache\Country;
use Doctrine\Tests\Models\Cache\State;
use Doctrine\Tests\Models\Cache\City;
use Doctrine\ORM\EntityManagerInterface;

/**
 * @group DDC-2183
 * @group performance
 */
class SecondLevelCacheTest extends OrmFunctionalTestCase
{
    protected function setUp()
    {
        parent::useModelSet('cache');
        parent::setUp();
    }

    /**
     * @return \Doctrine\ORM\EntityManagerInterface
     */
    public function createEntityManager()
    {
        $logger = new DebugStack();
        $em     = $this->_getEntityManager();

        $em->getConnection()->getConfiguration()->setSQLLogger($logger);
        $em->getConfiguration()->setSQLLogger($logger);

        return $em;
    }

    /**
     * @param \Doctrine\ORM\EntityManagerInterface $em
     * @return integer
     */
    public function countQuery(EntityManagerInterface $em)
    {
        return count($em->getConfiguration()->getSQLLogger()->queries);
    }

    public function testFindEntityWithoutCache()
    {
        $em = $this->createEntityManager();

        $this->findEntity($em, __FUNCTION__);

        $this->assertEquals(6002, $this->countQuery($em));
    }

    public function testFindEntityWithCache()
    {
        parent::enableSecondLevelCache(false);

        $em = $this->createEntityManager();

        $this->findEntity($em, __FUNCTION__);

        $this->assertEquals(502, $this->countQuery($em));
    }

    public function testFindAllEntityWithoutCache()
    {
        $em = $this->createEntityManager();

        $this->findAllEntity($em, __FUNCTION__);

        $this->assertEquals(153, $this->countQuery($em));
    }

    public function testFindAllEntityWithCache()
    {
        parent::enableSecondLevelCache(false);

        $em = $this->createEntityManager();

        $this->findAllEntity($em, __FUNCTION__);

        $this->assertEquals(53, $this->countQuery($em));
    }

    public function testFindEntityOneToManyWithoutCache()
    {
        $em = $this->createEntityManager();

        $this->findEntityOneToMany($em, __FUNCTION__);

        $this->assertEquals(502, $this->countQuery($em));
    }

    public function testFindEntityOneToManyWithCache()
    {
        parent::enableSecondLevelCache(false);

        $em = $this->createEntityManager();

        $this->findEntityOneToMany($em, __FUNCTION__);

        $this->assertEquals(472, $this->countQuery($em));
    }

    public function testQueryEntityWithoutCache()
    {
{'fraction_non_alphanumeric': 0.1359567325624767, 'fraction_numerical': 0.008205893323386795, 'mean_word_length': 3.167055167055167, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '3314182', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1504, 'n_words': 240}