Skip to content

Commit

Permalink
Merge branch 'fix/#1515-clean-hydrator-listeners-on-hydration-end'
Browse files Browse the repository at this point in the history
Close #1515
  • Loading branch information
Ocramius committed Jun 24, 2017
2 parents 41bcdb3 + 995054d commit 807f142
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ protected function cleanup()
$this->_rsm = null;
$this->_cache = [];
$this->_metadataCache = [];

$this
->_em
->getEventManager()
->removeEventListener([Events::onClear], $this);
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/Doctrine/Tests/ORM/Hydration/AbstractHydratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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);

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

0 comments on commit 807f142

Please sign in to comment.