-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1515 cleaning up test case, since the PHPUnit 5.4+ API is much nicer
- Loading branch information
Showing
1 changed file
with
28 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,64 +2,59 @@ | |
|
||
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\Query\ResultSetMapping; | ||
use Doctrine\ORM\Internal\Hydration\AbstractHydrator; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
/** | ||
* @group DDC-3146 | ||
* @author Emiel Nijpels <[email protected]> | ||
*/ | ||
class DDC3146Test extends \Doctrine\Tests\OrmFunctionalTestCase | ||
class DDC3146Test extends OrmFunctionalTestCase | ||
{ | ||
/** | ||
* 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 testEventListeners() | ||
{ | ||
// Create mock connection to be returned from the entity manager interface | ||
$mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock(); | ||
$mockEntityManagerInterface = $this->getMockBuilder('Doctrine\ORM\EntityManagerInterface')->disableOriginalConstructor()->getMock(); | ||
$mockEntityManagerInterface->expects($this->any())->method('getConnection')->will($this->returnValue($mockConnection)); | ||
|
||
// Create mock event manager to be returned from the entity manager interface | ||
$mockEventManager = $this->getMockBuilder('Doctrine\Common\EventManager')->disableOriginalConstructor()->getMock(); | ||
$mockEntityManagerInterface->expects($this->any())->method('getEventManager')->will($this->returnValue($mockEventManager)); | ||
$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); | ||
|
||
// Create mock statement and result mapping | ||
$mockStatement = $this->getMockBuilder('Doctrine\DBAL\Driver\Statement')->disableOriginalConstructor()->getMock(); | ||
$mockStatement->expects($this->once())->method('fetch')->will($this->returnValue(false)); | ||
$mockResultMapping = $this->getMockBuilder('Doctrine\ORM\Query\ResultSetMapping')->disableOriginalConstructor()->getMock(); | ||
$mockEntityManagerInterface->expects(self::any())->method('getEventManager')->willReturn($mockEventManager); | ||
$mockEntityManagerInterface->expects(self::any())->method('getConnection')->willReturn($mockConnection); | ||
$mockStatement->expects(self::once())->method('fetch')->willReturn(false); | ||
|
||
// Create mock abstract hydrator | ||
$mockAbstractHydrator = $this->getMockBuilder('Doctrine\ORM\Internal\Hydration\AbstractHydrator') | ||
$mockAbstractHydrator = $this->getMockBuilder(AbstractHydrator::class) | ||
->setConstructorArgs(array($mockEntityManagerInterface)) | ||
->setMethods(array('hydrateAllData')) | ||
->setMethods(['hydrateAllData']) | ||
->getMock(); | ||
|
||
// Increase counter every time the event listener is added and decrease the counter every time the event listener is removed | ||
$eventCounter = 0; | ||
$mockEventManager->expects($this->any()) | ||
$mockEventManager->expects(self::atLeastOnce()) | ||
->method('addEventListener') | ||
->will( | ||
$this->returnCallback( | ||
function () use (&$eventCounter) { | ||
$eventCounter++; | ||
} | ||
) | ||
); | ||
->willReturnCallback(function () use (&$eventCounter) { | ||
$eventCounter++; | ||
}); | ||
|
||
$mockEventManager->expects($this->any()) | ||
$mockEventManager->expects(self::atLeastOnce()) | ||
->method('removeEventListener') | ||
->will( | ||
$this->returnCallback( | ||
function () use (&$eventCounter) { | ||
$eventCounter--; | ||
} | ||
) | ||
); | ||
->willReturnCallback(function () use (&$eventCounter) { | ||
$eventCounter--; | ||
}); | ||
|
||
// Create iterable result | ||
$iterableResult = $mockAbstractHydrator->iterate($mockStatement, $mockResultMapping, array()); | ||
$iterableResult->next(); | ||
|
||
// Number of added events listeners should be equal or less than the number of removed events | ||
$this->assertLessThanOrEqual(0, $eventCounter, 'More events added to the event listener than removed; this can create a memory leak when references are not cleaned up'); | ||
self::assertSame(0, $eventCounter, 'More events added to the event listener than removed; this can create a memory leak when references are not cleaned up'); | ||
} | ||
} |