<commit_before><?php
namespace Doctrineum\Tests\Entity;

use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Setup;
use Doctrineum\Entity\Entity;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Tests\Fixtures\DummyOutput;

abstract class AbstractDoctrineEntitiesTest extends \PHPUnit_Framework_TestCase
{
    /** @var \Symfony\Component\Console\Application */
    private $application;

    /** @var  EntityManager */
    private $entityManager;

    /** @var string */
    private $proxiesUniqueTempDir;

    /** @var DebugStack */
    private $sqlLogger;

    protected function setUp()
    {
        $this->checkSqlExtensionAvailability();

        $paths = (array)$this->getDirsWithEntities();
        $this->checkPathsExistence($paths);
        $config = Setup::createAnnotationMetadataConfiguration(
            $paths,
            true /* dev mode */,
            $this->getProxiesUniqueTempDir()
        );
        $cache = new \Doctrine\Common\Cache\ArrayCache();
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);
        $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(
            new \Doctrine\Common\Annotations\AnnotationReader(),
            $paths
        );
        $config->setMetadataDriverImpl($driver);
        $this->sqlLogger = new DebugStack();
        $config->setSQLLogger($this->sqlLogger);
        $this->entityManager = EntityManager::create(
            [
                'driver' => $this->getSqlExtensionName(),
                'path' => ':memory:',
            ],
            $config
        );

        $helperSet = ConsoleRunner::createHelperSet($this->entityManager);
        $this->application = \Doctrine\ORM\Tools\Console\ConsoleRunner::createApplication($helperSet);
        $this->application->setAutoExit(false);
    }

    /**
     * @codeCoverageIgnore
     */
    protected function checkSqlExtensionAvailability()
    {
        if (!extension_loaded($this->getSqlExtensionName())) {
            self::markTestSkipped("The {$this->getSqlExtensionName()} extension is not available.");
        }
    }

    /**
     * @return string[]|array
     */
    abstract protected function getDirsWithEntities();

    protected function checkPathsExistence(array $paths)
    {
        self::assertNotEmpty($paths, 'No dirs with entities-to-test given');
        foreach ($paths as $path) {
            self::assertTrue(is_dir($path), "Given dir {$path} with entities has not been found");
        }
    }

    protected function getSqlExtensionName()
    {
        return 'pdo_sqlite';
    }

    /**
     * @return array
     */
    protected function getQueries()
    {
        return $this->sqlLogger->queries;
    }

    protected function tearDown()
    {
        if ($this->entityManager !== null) {
            $this->entityManager->getConnection()->close();
        }
        if ($this->proxiesUniqueTempDir !== null) {
            foreach (scandir($this->proxiesUniqueTempDir) as $folder) {
                if ($folder === '.' || $folder === '..') {
                    continue;
                }
                $folderFullPath = $this->proxiesUniqueTempDir . DIRECTORY_SEPARATOR . $folder;
                unlink($folderFullPath);
            }
            rmdir($this->proxiesUniqueTempDir);
        }
    }

    /**
     * @test
     */
    public function I_can_persist_and_fetch_entities()
    {
        $this->I_can_create_schema();
        $originalEntities = (array)$this->createEntitiesToPersist();
        $proxies = $this->I_can_generate_proxies($originalEntities);

        foreach ($originalEntities = (array)$this->createEntitiesToPersist() as $entityToPersist) {
            $this->entityManager->persist($entityToPersist);
        }
        try {
            $this->entityManager->flush();
        } catch (TableNotFoundException $tableNotFoundException) {
            throw new \RuntimeException(
                $tableNotFoundException->getMessage()
                . "\n" . 'Have you listed all dirs with entities by getDirsWithEntities() ?'
                . "\n(" . $this->getSchemaDescription() . ')'
            );
        }

        $originalGroupedByClass = $this->groupByClass($originalEntities);
        $originalGrouped = $this->groupById($originalGroupedByClass); // ids are set to entities after flush

        $this->entityManager->clear(); // clear disconnects all entities, but also by them containing entities

        $fetchedEntities = (array)$this->fetchEntitiesByOriginals($originalEntities, $this->entityManager);
        self::assertCount(
            $countOfOriginals = count($originalEntities),
            $fetchedEntities,
            "Expected {$countOfOriginals} fetched entities (same as originals)"<commit_msg>Doctrine entities test comments clarified a bit<commit_after><?php
namespace Doctrineum\Tests\Entity;

use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Setup;
use Doctrineum\Entity\Entity;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Tests\Fixtures\DummyOutput;

abstract class AbstractDoctrineEntitiesTest extends \PHPUnit_Framework_TestCase
{
    /** @var \Symfony\Component\Console\Application */
    private $application;

    /** @var  EntityManager */
    private $entityManager;

    /** @var string */
    private $proxiesUniqueTempDir;

    /** @var DebugStack */
    private $sqlLogger;

    protected function setUp()
    {
        $this->checkSqlExtensionAvailability();

        $paths = (array)$this->getDirsWithEntities();
        $this->checkPathsExistence($paths);
        $config = Setup::createAnnotationMetadataConfiguration(
            $paths,
            true /* dev mode */,
            $this->getProxiesUniqueTempDir()
        );
        $cache = new \Doctrine\Common\Cache\ArrayCache();
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);
        $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(
            new \Doctrine\Common\Annotations\AnnotationReader(),
            $paths
        );
        $config->setMetadataDriverImpl($driver);
        $this->sqlLogger = new DebugStack();
        $config->setSQLLogger($this->sqlLogger);
        $this->entityManager = EntityManager::create(
            [
                'driver' => $this->getSqlExtensionName(),
                'path' => ':memory:',
            ],
            $config
        );

        $helperSet = ConsoleRunner::createHelperSet($this->entityManager);
        $this->application = \Doctrine\ORM\Tools\Console\ConsoleRunner::createApplication($helperSet);
        $this->application->setAutoExit(false);
    }

    /**
     * @codeCoverageIgnore
     */
    protected function checkSqlExtensionAvailability()
    {
        if (!extension_loaded($this->getSqlExtensionName())) {
            self::markTestSkipped("The {$this->getSqlExtensionName()} extension is not available.");
        }
    }

    /**
     * @return string[]|array
     */
    abstract protected function getDirsWithEntities();

    protected function checkPathsExistence(array $paths)
    {
        self::assertNotEmpty($paths, 'No dirs with entities-to-test given');
        foreach ($paths as $path) {
            self::assertTrue(is_dir($path), "Given dir {$path} with entities has not been found");
        }
    }

    protected function getSqlExtensionName()
    {
        return 'pdo_sqlite';
    }

    /**
     * @return array
     */
    protected function getQueries()
    {
        return $this->sqlLogger->queries;
    }

    protected function tearDown()
    {
        if ($this->entityManager !== null) {
            $this->entityManager->getConnection()->close();
        }
        if ($this->proxiesUniqueTempDir !== null) {
            foreach (scandir($this->proxiesUniqueTempDir) as $folder) {
                if ($folder === '.' || $folder === '..') {
                    continue;
                }
                $folderFullPath = $this->proxiesUniqueTempDir . DIRECTORY_SEPARATOR . $folder;
                unlink($folderFullPath);
            }
            rmdir($this->proxiesUniqueTempDir);
        }
    }

    /**
     * @test
     */
    public function I_can_persist_and_fetch_entities()
    {
        $this->I_can_create_schema();
        $originalEntities = (array)$this->createEntitiesToPersist();
        $proxies = $this->I_can_generate_proxies($originalEntities);

        foreach ($originalEntities = (array)$this->createEntitiesToPersist() as $entityToPersist) {
            $this->entityManager->persist($entityToPersist);
        }
        try {
            $this->entityManager->flush();
        } catch (TableNotFoundException $tableNotFoundException) {
            throw new \RuntimeException(
                $tableNotFoundException->getMessage()
                . "\n" . 'Have you listed all dirs with entities by getDirsWithEntities() ?'
                . "\n(" . $this->getSchemaDescription() . ')'
            );
        }

        $originalGroupedByClass = $this->groupByClass($originalEntities);
        $originalGrouped = $this->groupById($originalGroupedByClass); // ids are set to entities after flush

        $this->entityManager->clear(); // 'clear' disconnects all entities, but also by-them-containing entities

        $fetchedEntities = (array)$this->fetchEntitiesByOriginals($originalEntities, $this->entityManager);
        self::assertCount(
            $countOfOriginals = count($originalEntities),
            $fetchedEntities,
            "Expected {$countOfOriginals} fetched entities (same as originals)"
{'fraction_non_alphanumeric': 0.12656692276587142, 'fraction_numerical': 0.0, 'mean_word_length': 2.5143872113676733, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 95, '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': '1213945', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2649, 'n_words': 467}