1
0
mirror of synced 2025-02-02 21:41:45 +03:00

Throw an exception instead of a workaround

This commit is contained in:
Jeremy Benoist 2016-09-12 12:08:42 +02:00 committed by Marco Pivetta
parent 4a87f00fab
commit 2a7d21ad18
2 changed files with 18 additions and 6 deletions

View File

@ -2363,6 +2363,10 @@ class UnitOfWork implements PropertyChangedListener
*/ */
public function clear($entityName = null) public function clear($entityName = null)
{ {
if ($entityName !== null && !is_string($entityName)) {
throw new \InvalidArgumentException(sprintf('Argument 1 passed to %s() must be a string, %s given', __METHOD__, gettype($entityName)));
}
if ($entityName === null) { if ($entityName === null) {
$this->identityMap = $this->identityMap =
$this->entityIdentifiers = $this->entityIdentifiers =
@ -3442,10 +3446,6 @@ class UnitOfWork implements PropertyChangedListener
*/ */
private function clearIdentityMapForEntityName($entityName) private function clearIdentityMapForEntityName($entityName)
{ {
if (is_object($entityName)) {
return;
}
if (! isset($this->identityMap[$entityName])) { if (! isset($this->identityMap[$entityName])) {
return; return;
} }

View File

@ -351,16 +351,28 @@ class UnitOfWorkTest extends OrmTestCase
public function testClearManagerWithObject() public function testClearManagerWithObject()
{ {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('must be a string');
$entity = new Country(456, 'United Kingdom'); $entity = new Country(456, 'United Kingdom');
$this->_unitOfWork->persist($entity); $this->_unitOfWork->persist($entity);
$this->assertTrue($this->_unitOfWork->isInIdentityMap($entity)); $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity));
$this->_unitOfWork->clear($entity); $this->_unitOfWork->clear($entity);
}
// true because entity wasn't a string so it wasn't cleared public function testClearManagerWithNullValue()
{
$entity = new Country(456, 'United Kingdom');
$this->_unitOfWork->persist($entity);
$this->assertTrue($this->_unitOfWork->isInIdentityMap($entity)); $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity));
$this->assertTrue($this->_unitOfWork->isScheduledForInsert($entity));
$this->_unitOfWork->clear();
$this->assertFalse($this->_unitOfWork->isInIdentityMap($entity));
$this->assertFalse($this->_unitOfWork->isScheduledForInsert($entity));
} }
/** /**