|
3 | 3 | namespace Doctrine\Tests\ORM; |
4 | 4 |
|
5 | 5 | use Doctrine\Common\Collections\ArrayCollection; |
| 6 | +use Doctrine\Common\EventManager; |
| 7 | +use Doctrine\Common\EventSubscriber; |
6 | 8 | use Doctrine\Common\NotifyPropertyChanged; |
| 9 | +use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
7 | 10 | use Doctrine\Common\PropertyChangedListener; |
| 11 | +use Doctrine\ORM\Events; |
8 | 12 | use Doctrine\ORM\Mapping\ClassMetadata; |
9 | 13 | use Doctrine\ORM\ORMInvalidArgumentException; |
10 | 14 | use Doctrine\ORM\UnitOfWork; |
@@ -48,18 +52,22 @@ class UnitOfWorkTest extends OrmTestCase |
48 | 52 | */ |
49 | 53 | private $_emMock; |
50 | 54 |
|
51 | | - protected function setUp() { |
| 55 | + /** |
| 56 | + * @var EventManager|\PHPUnit_Framework_MockObject_MockObject |
| 57 | + */ |
| 58 | + private $eventManager; |
| 59 | + |
| 60 | + protected function setUp() |
| 61 | + { |
52 | 62 | parent::setUp(); |
53 | 63 | $this->_connectionMock = new ConnectionMock([], new DriverMock()); |
54 | | - $this->_emMock = EntityManagerMock::create($this->_connectionMock); |
| 64 | + $this->eventManager = $this->getMockBuilder(EventManager::class)->getMock(); |
| 65 | + $this->_emMock = EntityManagerMock::create($this->_connectionMock, null, $this->eventManager); |
55 | 66 | // SUT |
56 | 67 | $this->_unitOfWork = new UnitOfWorkMock($this->_emMock); |
57 | 68 | $this->_emMock->setUnitOfWork($this->_unitOfWork); |
58 | 69 | } |
59 | 70 |
|
60 | | - protected function tearDown() { |
61 | | - } |
62 | | - |
63 | 71 | public function testRegisterRemovedOnNewEntityIsIgnored() |
64 | 72 | { |
65 | 73 | $user = new ForumUser(); |
@@ -488,6 +496,45 @@ public function testObjectHashesOfMergedEntitiesAreNotUsedInOriginalEntityDataMa |
488 | 496 |
|
489 | 497 | self::assertSame([], $this->_unitOfWork->getOriginalEntityData($newUser), 'No original data was stored'); |
490 | 498 | } |
| 499 | + |
| 500 | + public function testMergeWithNewEntityWillPersistItAndTriggerPrePersistListenersWithMergedEntityData() |
| 501 | + { |
| 502 | + $entity = new EntityWithListenerPopulatedField(); |
| 503 | + |
| 504 | + $generatedFieldValue = $entity->generatedField; |
| 505 | + |
| 506 | + $this |
| 507 | + ->eventManager |
| 508 | + ->expects(self::any()) |
| 509 | + ->method('hasListeners') |
| 510 | + ->willReturnCallback(function ($eventName) { |
| 511 | + return $eventName === Events::prePersist; |
| 512 | + }); |
| 513 | + $this |
| 514 | + ->eventManager |
| 515 | + ->expects(self::once()) |
| 516 | + ->method('dispatchEvent') |
| 517 | + ->with( |
| 518 | + self::anything(), |
| 519 | + self::callback(function (LifecycleEventArgs $args) use ($entity, $generatedFieldValue) { |
| 520 | + /* @var $object EntityWithListenerPopulatedField */ |
| 521 | + $object = $args->getObject(); |
| 522 | + |
| 523 | + self::assertInstanceOf(EntityWithListenerPopulatedField::class, $object); |
| 524 | + self::assertNotSame($entity, $object); |
| 525 | + self::assertSame($generatedFieldValue, $object->generatedField); |
| 526 | + |
| 527 | + return true; |
| 528 | + }) |
| 529 | + ); |
| 530 | + |
| 531 | + /* @var $object EntityWithListenerPopulatedField */ |
| 532 | + $object = $this->_unitOfWork->merge($entity); |
| 533 | + |
| 534 | + self::assertNotSame($object, $entity); |
| 535 | + self::assertInstanceOf(EntityWithListenerPopulatedField::class, $object); |
| 536 | + self::assertSame($object->generatedField, $entity->generatedField); |
| 537 | + } |
491 | 538 | } |
492 | 539 |
|
493 | 540 | /** |
@@ -634,3 +681,23 @@ class EntityWithCompositeStringIdentifier |
634 | 681 | */ |
635 | 682 | public $id2; |
636 | 683 | } |
| 684 | + |
| 685 | +/** @Entity */ |
| 686 | +class EntityWithListenerPopulatedField |
| 687 | +{ |
| 688 | + const MAX_GENERATED_FIELD_VALUE = 10000; |
| 689 | + |
| 690 | + /** @Id @Column(type="string") */ |
| 691 | + public $id; |
| 692 | + |
| 693 | + /** |
| 694 | + * @Column(type="integer") |
| 695 | + */ |
| 696 | + public $generatedField; |
| 697 | + |
| 698 | + public function __construct() |
| 699 | + { |
| 700 | + $this->id = uniqid('id', true); |
| 701 | + $this->generatedField = mt_rand(0, self::MAX_GENERATED_FIELD_VALUE); |
| 702 | + } |
| 703 | +} |
0 commit comments