forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request doctrine#1074 from zimmermanj42/DDC-3160
[DDC-3160] Alternate fix for DDC-2996 bug
- Loading branch information
Showing
2 changed files
with
79 additions
and
8 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
70 changes: 70 additions & 0 deletions
70
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket; | ||
|
||
use Doctrine\Tests\Models\CMS\CmsUser; | ||
use Doctrine\ORM\Event\OnFlushEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
/** | ||
* FlushEventTest | ||
* | ||
* @author robo | ||
*/ | ||
class DDC3160Test extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp() { | ||
$this->useModelSet('cms'); | ||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* @group DDC-3160 | ||
*/ | ||
public function testNoUpdateOnInsert() | ||
{ | ||
$listener = new DDC3160OnFlushListener(); | ||
$this->_em->getEventManager()->addEventListener(Events::onFlush, $listener); | ||
|
||
$user = new CmsUser; | ||
$user->username = 'romanb'; | ||
$user->name = 'Roman'; | ||
$user->status = 'Dev'; | ||
|
||
$this->_em->persist($user); | ||
$this->_em->flush(); | ||
|
||
$this->_em->refresh($user); | ||
|
||
$this->assertEquals('romanc', $user->username); | ||
$this->assertEquals(1, $listener->inserts); | ||
$this->assertEquals(0, $listener->updates); | ||
} | ||
} | ||
|
||
class DDC3160OnFlushListener | ||
{ | ||
public $inserts = 0; | ||
public $updates = 0; | ||
|
||
public function onFlush(OnFlushEventArgs $args) | ||
{ | ||
$em = $args->getEntityManager(); | ||
$uow = $em->getUnitOfWork(); | ||
|
||
foreach ($uow->getScheduledEntityInsertions() as $entity) { | ||
$this->inserts++; | ||
if ($entity instanceof CmsUser) { | ||
$entity->username = 'romanc'; | ||
$cm = $em->getClassMetadata(get_class($entity)); | ||
$uow->recomputeSingleEntityChangeSet($cm, $entity); | ||
} | ||
} | ||
|
||
foreach ($uow->getScheduledEntityUpdates() as $entity) { | ||
$this->updates++; | ||
} | ||
} | ||
} | ||
|