<commit_before><?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\ORM\Events;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
 * @covers \Doctrine\ORM\Internal\Hydration\AbstractHydrator
 */
class AbstractHydratorTest extends OrmFunctionalTestCase
{
    /**
     * @group DDC-3146
     * @group #1515
     *
     * Verify that the number of added events to the event listener from the abstract hydrator class is equal to the
     * number of removed events
     */
    public function testOnClearEventListenerIsDetachedOnCleanup()
    {
        $mockConnection             = $this->createMock(Connection::class);
        $mockEntityManagerInterface = $this->createMock(EntityManagerInterface::class);
        $mockEventManager           = $this->createMock(EventManager::class);
        $mockStatement              = $this->createMock(Statement::class);
        $mockResultMapping          = $this->getMockBuilder(ResultSetMapping::class);

        $mockEntityManagerInterface->expects(self::any())->method('getEventManager')->willReturn($mockEventManager);
        $mockEntityManagerInterface->expects(self::any())->method('getConnection')->willReturn($mockConnection);
        $mockStatement->expects(self::once())->method('fetch')->willReturn(false);

        /* @var $mockAbstractHydrator AbstractHydrator */
        $mockAbstractHydrator = $this
            ->getMockBuilder(AbstractHydrator::class)
            ->setConstructorArgs([$mockEntityManagerInterface])
            ->setMethods(['hydrateAllData'])
            ->getMock();

        $mockEventManager
            ->expects(self::at(0))
            ->method('addEventListener')
            ->with([Events::onClear], $mockAbstractHydrator);

        $mockEventManager
            ->expects(self::at(1))
            ->method('removeEventListener')
            ->with([Events::onClear], $mockAbstractHydrator);

        $mockEventManager
            ->expects(self::at(2))
            ->method('addEventListener')
            ->with([Events::onClear], $mockAbstractHydrator);

        $mockEventManager
            ->expects(self::at(3))
            ->method('removeEventListener')
            ->with([Events::onClear], $mockAbstractHydrator);

        iterator_to_array($mockAbstractHydrator->iterate($mockStatement, $mockResultMapping));
        $mockAbstractHydrator->hydrateAll($mockStatement, $mockResultMapping);
    }
}<commit_msg>#6623 splitting test scenario into two bits<commit_after><?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
 * @covers \Doctrine\ORM\Internal\Hydration\AbstractHydrator
 */
class AbstractHydratorTest extends OrmFunctionalTestCase
{
    /**
     * @var EventManager|\PHPUnit_Framework_MockObject_MockObject
     */
    private $mockEventManager;

    /**
     * @var Statement|\PHPUnit_Framework_MockObject_MockObject
     */
    private $mockStatement;

    /**
     * @var ResultSetMapping|\PHPUnit_Framework_MockObject_MockObject
     */
    private $mockResultMapping;

    /**
     * @var AbstractHydrator
     */
    private $hydrator;

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

        $mockConnection             = $this->createMock(Connection::class);
        $mockEntityManagerInterface = $this->createMock(EntityManagerInterface::class);
        $this->mockEventManager     = $this->createMock(EventManager::class);
        $this->mockStatement        = $this->createMock(Statement::class);
        $this->mockResultMapping    = $this->getMockBuilder(ResultSetMapping::class);

        $mockEntityManagerInterface
            ->expects(self::any())
            ->method('getEventManager')
            ->willReturn($this->mockEventManager);
        $mockEntityManagerInterface
            ->expects(self::any())
            ->method('getConnection')
            ->willReturn($mockConnection);
        $this->mockStatement
            ->expects(self::any())
            ->method('fetch')
            ->willReturn(false);

        $this->hydrator = $this
            ->getMockBuilder(AbstractHydrator::class)
            ->setConstructorArgs([$mockEntityManagerInterface])
            ->setMethods(['hydrateAllData'])
            ->getMock();
    }

    /**
     * @group DDC-3146
     * @group #1515
     *
     * Verify that the number of added events to the event listener from the abstract hydrator class is equal to the
     * number of removed events
     */
    public function testOnClearEventListenerIsDetachedOnCleanup() : void
    {
        $this
            ->mockEventManager
            ->expects(self::at(0))
            ->method('addEventListener')
            ->with([Events::onClear], $this->hydrator);

        $this
            ->mockEventManager
            ->expects(self::at(1))
            ->method('removeEventListener')
            ->with([Events::onClear], $this->hydrator);

        iterator_to_array($this->hydrator->iterate($this->mockStatement, $this->mockResultMapping));
    }

    /**
     * @group #6623
     */
    public function testHydrateAllRegistersAndClearsAllAttachedListeners() : void
    {
        $this
            ->mockEventManager
            ->expects(self::at(0))
            ->method('addEventListener')
            ->with([Events::onClear], $this->hydrator);

        $this
            ->mockEventManager
            ->expects(self::at(1))
            ->method('removeEventListener')
            ->with([Events::onClear], $this->hydrator);

        $this->hydrator->hydrateAll($this->mockStatement, $this->mockResultMapping);
    }
}
{'fraction_non_alphanumeric': 0.12865593164640157, 'fraction_numerical': 0.0052579691094314825, 'mean_word_length': 2.862309644670051, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, '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': '3630982', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1631, 'n_words': 248}