#6174 #5570 started moving tests around prePersist
event subscriber triggering on UnitOfWork
into the UnitOfWorkTest
This commit is contained in:
parent
9582ffc982
commit
30cd2d172b
@ -3,8 +3,12 @@
|
|||||||
namespace Doctrine\Tests\ORM;
|
namespace Doctrine\Tests\ORM;
|
||||||
|
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
use Doctrine\Common\EventManager;
|
||||||
|
use Doctrine\Common\EventSubscriber;
|
||||||
use Doctrine\Common\NotifyPropertyChanged;
|
use Doctrine\Common\NotifyPropertyChanged;
|
||||||
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
||||||
use Doctrine\Common\PropertyChangedListener;
|
use Doctrine\Common\PropertyChangedListener;
|
||||||
|
use Doctrine\ORM\Events;
|
||||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||||
use Doctrine\ORM\ORMInvalidArgumentException;
|
use Doctrine\ORM\ORMInvalidArgumentException;
|
||||||
use Doctrine\ORM\UnitOfWork;
|
use Doctrine\ORM\UnitOfWork;
|
||||||
@ -48,18 +52,22 @@ class UnitOfWorkTest extends OrmTestCase
|
|||||||
*/
|
*/
|
||||||
private $_emMock;
|
private $_emMock;
|
||||||
|
|
||||||
protected function setUp() {
|
/**
|
||||||
|
* @var EventManager|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
private $eventManager;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->_connectionMock = new ConnectionMock([], new DriverMock());
|
$this->_connectionMock = new ConnectionMock([], new DriverMock());
|
||||||
$this->_emMock = EntityManagerMock::create($this->_connectionMock);
|
$this->eventManager = $this->getMockBuilder(EventManager::class)->getMock();
|
||||||
|
$this->_emMock = EntityManagerMock::create($this->_connectionMock, null, $this->eventManager);
|
||||||
// SUT
|
// SUT
|
||||||
$this->_unitOfWork = new UnitOfWorkMock($this->_emMock);
|
$this->_unitOfWork = new UnitOfWorkMock($this->_emMock);
|
||||||
$this->_emMock->setUnitOfWork($this->_unitOfWork);
|
$this->_emMock->setUnitOfWork($this->_unitOfWork);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function tearDown() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRegisterRemovedOnNewEntityIsIgnored()
|
public function testRegisterRemovedOnNewEntityIsIgnored()
|
||||||
{
|
{
|
||||||
$user = new ForumUser();
|
$user = new ForumUser();
|
||||||
@ -488,6 +496,45 @@ class UnitOfWorkTest extends OrmTestCase
|
|||||||
|
|
||||||
self::assertSame([], $this->_unitOfWork->getOriginalEntityData($newUser), 'No original data was stored');
|
self::assertSame([], $this->_unitOfWork->getOriginalEntityData($newUser), 'No original data was stored');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMergeWithNewEntityWillPersistItAndTriggerPrePersistListenersWithMergedEntityData()
|
||||||
|
{
|
||||||
|
$entity = new EntityWithListenerPopulatedField();
|
||||||
|
|
||||||
|
$generatedFieldValue = $entity->generatedField;
|
||||||
|
|
||||||
|
$this
|
||||||
|
->eventManager
|
||||||
|
->expects(self::any())
|
||||||
|
->method('hasListeners')
|
||||||
|
->willReturnCallback(function ($eventName) {
|
||||||
|
return $eventName === Events::prePersist;
|
||||||
|
});
|
||||||
|
$this
|
||||||
|
->eventManager
|
||||||
|
->expects(self::once())
|
||||||
|
->method('dispatchEvent')
|
||||||
|
->with(
|
||||||
|
self::anything(),
|
||||||
|
self::callback(function (LifecycleEventArgs $args) use ($entity, $generatedFieldValue) {
|
||||||
|
/* @var $object EntityWithListenerPopulatedField */
|
||||||
|
$object = $args->getObject();
|
||||||
|
|
||||||
|
self::assertInstanceOf(EntityWithListenerPopulatedField::class, $object);
|
||||||
|
self::assertNotSame($entity, $object);
|
||||||
|
self::assertSame($generatedFieldValue, $object->generatedField);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
/* @var $object EntityWithListenerPopulatedField */
|
||||||
|
$object = $this->_unitOfWork->merge($entity);
|
||||||
|
|
||||||
|
self::assertNotSame($object, $entity);
|
||||||
|
self::assertInstanceOf(EntityWithListenerPopulatedField::class, $object);
|
||||||
|
self::assertSame($object->generatedField, $entity->generatedField);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -634,3 +681,23 @@ class EntityWithCompositeStringIdentifier
|
|||||||
*/
|
*/
|
||||||
public $id2;
|
public $id2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @Entity */
|
||||||
|
class EntityWithListenerPopulatedField
|
||||||
|
{
|
||||||
|
const MAX_GENERATED_FIELD_VALUE = 10000;
|
||||||
|
|
||||||
|
/** @Id @Column(type="string") */
|
||||||
|
public $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Column(type="integer")
|
||||||
|
*/
|
||||||
|
public $generatedField;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->id = uniqid('id', true);
|
||||||
|
$this->generatedField = mt_rand(0, self::MAX_GENERATED_FIELD_VALUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user