From a5cf6417b3048aa6e1336e496556de453580b25f Mon Sep 17 00:00:00 2001 From: Andreas Flack Date: Thu, 12 Jun 2014 17:28:13 +0200 Subject: [PATCH] Add failing test for DDC-3160 --- .../ORM/Functional/Ticket/DDC3160Test.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php new file mode 100644 index 000000000..6fe88e1ae --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php @@ -0,0 +1,70 @@ +useModelSet('cms'); + parent::setUp(); + } + + /** + * @group DDC-3160 + */ + public function testNoUpdateOnInsert() + { + $listener = new OnFlushListener(); + $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 OnFlushListener +{ + 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++; + } + } +} +