Skip to content

Commit

Permalink
Merge branch 'fix/#6623-#1515-ensure-abstracthydrator-hydrateall-clea…
Browse files Browse the repository at this point in the history
…ns-up-on-unit-of-work-clear'

Close #6623
  • Loading branch information
Ocramius committed Aug 16, 2017
2 parents a0c0d3b + ff3c89d commit 57a9509
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public function hydrateAll($stmt, $resultSetMapping, array $hints = [])
$this->_rsm = $resultSetMapping;
$this->_hints = $hints;

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

$this->prepare();

$result = $this->hydrateAllData();
Expand Down
99 changes: 77 additions & 22 deletions tests/Doctrine/Tests/ORM/Hydration/AbstractHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Doctrine\Tests\ORM\Functional\Ticket;

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

/**
Expand All @@ -17,41 +17,96 @@
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
* @var EventManager|\PHPUnit_Framework_MockObject_MockObject
*/
public function testOnClearEventListenerIsDetachedOnCleanup()
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);
$mockEventManager = $this->createMock(EventManager::class);
$mockStatement = $this->createMock(Statement::class);
$mockResultMapping = $this->getMockBuilder(ResultSetMapping::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($mockEventManager);
$mockEntityManagerInterface->expects(self::any())->method('getConnection')->willReturn($mockConnection);
$mockStatement->expects(self::once())->method('fetch')->willReturn(false);
$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);

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

$mockEventManager
/**
* @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], $mockAbstractHydrator);
->with([Events::onClear], $this->hydrator);

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

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

0 comments on commit 57a9509

Please sign in to comment.