2008-06-16 22:31:21 +04:00
|
|
|
<?php
|
2009-01-04 19:15:32 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests\ORM;
|
2009-01-04 19:15:32 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock;
|
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock;
|
|
|
|
use Doctrine\Tests\Mocks\UnitOfWorkMock;
|
|
|
|
use Doctrine\Tests\Mocks\EntityPersisterMock;
|
|
|
|
use Doctrine\Tests\Mocks\IdentityIdGeneratorMock;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\Forum\ForumUser;
|
|
|
|
use Doctrine\Tests\Models\Forum\ForumAvatar;
|
|
|
|
|
2009-01-24 19:56:44 +03:00
|
|
|
require_once __DIR__ . '/../TestInit.php';
|
2009-01-22 22:38:10 +03:00
|
|
|
|
|
|
|
#require_once 'lib/mocks/Doctrine_EntityManagerMock.php';
|
|
|
|
#require_once 'lib/mocks/Doctrine_ConnectionMock.php';
|
|
|
|
#require_once 'lib/mocks/Doctrine_UnitOfWorkMock.php';
|
|
|
|
#require_once 'lib/mocks/Doctrine_IdentityIdGeneratorMock.php';
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UnitOfWork tests.
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase
|
2008-06-16 22:31:21 +04:00
|
|
|
{
|
2009-01-04 19:15:32 +03:00
|
|
|
// SUT
|
2008-06-16 22:31:21 +04:00
|
|
|
private $_unitOfWork;
|
2008-07-21 00:13:24 +04:00
|
|
|
// Provides a sequence mock to the UnitOfWork
|
|
|
|
private $_connectionMock;
|
2009-01-04 19:15:32 +03:00
|
|
|
// The EntityManager mock that provides the mock persisters
|
2008-07-21 00:13:24 +04:00
|
|
|
private $_emMock;
|
|
|
|
|
2008-06-16 22:31:21 +04:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_connectionMock = new ConnectionMock(array());
|
|
|
|
$this->_emMock = EntityManagerMock::create($this->_connectionMock, "uowMockEm");
|
2008-09-07 17:48:40 +04:00
|
|
|
// SUT
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_unitOfWork = new UnitOfWorkMock($this->_emMock);
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_emMock->setUnitOfWork($this->_unitOfWork);
|
2008-06-16 22:31:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown() {
|
|
|
|
}
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
public function testRegisterRemovedOnNewEntityIsIgnored()
|
2008-06-16 22:31:21 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$user = new ForumUser();
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($user));
|
|
|
|
$this->_unitOfWork->registerDeleted($user);
|
|
|
|
$this->assertFalse($this->_unitOfWork->isRegisteredRemoved($user));
|
2008-06-16 22:31:21 +04:00
|
|
|
}
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/* Operational tests */
|
|
|
|
|
|
|
|
public function testSavingSingleEntityWithIdentityColumnForcesInsert()
|
2009-01-03 22:50:13 +03:00
|
|
|
{
|
|
|
|
// Setup fake persister and id generator for identity generation
|
2009-01-22 22:38:10 +03:00
|
|
|
$userPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata("Doctrine\Tests\Models\Forum\ForumUser"));
|
|
|
|
$this->_emMock->setEntityPersister('Doctrine\Tests\Models\Forum\ForumUser', $userPersister);
|
|
|
|
$idGeneratorMock = new IdentityIdGeneratorMock($this->_emMock);
|
|
|
|
$this->_emMock->setIdGenerator('Doctrine\Tests\Models\Forum\ForumUser', $idGeneratorMock);
|
|
|
|
$userPersister->setMockIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY);
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
// Test
|
|
|
|
$user = new ForumUser();
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$this->_unitOfWork->save($user);
|
|
|
|
|
|
|
|
// Check
|
|
|
|
$this->assertEquals(1, count($userPersister->getInserts())); // insert forced
|
|
|
|
$this->assertEquals(0, count($userPersister->getUpdates()));
|
|
|
|
$this->assertEquals(0, count($userPersister->getDeletes()));
|
|
|
|
$this->assertTrue($this->_unitOfWork->isInIdentityMap($user));
|
2008-07-21 00:13:24 +04:00
|
|
|
// should no longer be scheduled for insert
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->assertFalse($this->_unitOfWork->isRegisteredNew($user));
|
2008-07-21 00:13:24 +04:00
|
|
|
// should have an id
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->assertTrue(is_numeric($user->id));
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-08-01 22:46:14 +04:00
|
|
|
// Now lets check whether a subsequent commit() does anything
|
2009-01-03 22:50:13 +03:00
|
|
|
$userPersister->reset();
|
|
|
|
|
|
|
|
// Test
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_unitOfWork->commit(); // shouldnt do anything
|
|
|
|
|
2009-01-03 22:50:13 +03:00
|
|
|
// Check. Verify that nothing happened.
|
|
|
|
$this->assertEquals(0, count($userPersister->getInserts()));
|
|
|
|
$this->assertEquals(0, count($userPersister->getUpdates()));
|
|
|
|
$this->assertEquals(0, count($userPersister->getDeletes()));
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a scenario where a save() operation is cascaded from a ForumUser
|
|
|
|
* to its associated ForumAvatar, both entities using IDENTITY id generation.
|
|
|
|
*/
|
|
|
|
public function testCascadedIdentityColumnInsert()
|
2008-08-22 13:05:14 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
// Setup fake persister and id generator for identity generation
|
|
|
|
//ForumUser
|
2009-01-22 22:38:10 +03:00
|
|
|
$userPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata("Doctrine\Tests\Models\Forum\ForumUser"));
|
|
|
|
$this->_emMock->setEntityPersister('Doctrine\Tests\Models\Forum\ForumUser', $userPersister);
|
|
|
|
$userIdGeneratorMock = new IdentityIdGeneratorMock($this->_emMock);
|
|
|
|
$this->_emMock->setIdGenerator('Doctrine\Tests\Models\Forum\ForumUser', $userIdGeneratorMock);
|
|
|
|
$userPersister->setMockIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY);
|
2009-01-03 22:50:13 +03:00
|
|
|
// ForumAvatar
|
2009-01-22 22:38:10 +03:00
|
|
|
$avatarPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata("Doctrine\Tests\Models\Forum\ForumAvatar"));
|
|
|
|
$this->_emMock->setEntityPersister('Doctrine\Tests\Models\Forum\ForumAvatar', $avatarPersister);
|
|
|
|
$avatarIdGeneratorMock = new IdentityIdGeneratorMock($this->_emMock);
|
|
|
|
$this->_emMock->setIdGenerator('Doctrine\Tests\Models\Forum\ForumAvatar', $avatarIdGeneratorMock);
|
|
|
|
$avatarPersister->setMockIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_IDENTITY);
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
// Test
|
|
|
|
$user = new ForumUser();
|
|
|
|
$user->username = 'romanb';
|
2008-08-22 13:05:14 +04:00
|
|
|
$avatar = new ForumAvatar();
|
2009-01-03 22:50:13 +03:00
|
|
|
$user->avatar = $avatar;
|
|
|
|
$this->_unitOfWork->save($user); // save cascaded to avatar
|
|
|
|
|
|
|
|
$this->assertTrue(is_numeric($user->id));
|
|
|
|
$this->assertTrue(is_numeric($avatar->id));
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($userPersister->getInserts())); // insert forced
|
|
|
|
$this->assertEquals(0, count($userPersister->getUpdates()));
|
|
|
|
$this->assertEquals(0, count($userPersister->getDeletes()));
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($avatarPersister->getInserts())); // insert forced
|
|
|
|
$this->assertEquals(0, count($avatarPersister->getUpdates()));
|
|
|
|
$this->assertEquals(0, count($avatarPersister->getDeletes()));
|
2008-08-22 13:05:14 +04:00
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
2009-01-09 19:25:06 +03:00
|
|
|
public function testComputeEntityChangeSets()
|
2009-01-03 22:50:13 +03:00
|
|
|
{
|
2009-01-09 19:25:06 +03:00
|
|
|
// We need an ID generator for ForumAvatar, because we attach a NEW ForumAvatar
|
|
|
|
// to a (faked) MANAGED instance. During changeset computation this will result
|
|
|
|
// in the UnitOfWork requesting the Id generator of ForumAvatar.
|
2009-01-22 22:38:10 +03:00
|
|
|
$avatarIdGeneratorMock = new IdentityIdGeneratorMock($this->_emMock);
|
|
|
|
$this->_emMock->setIdGenerator('Doctrine\Tests\Models\Forum\ForumAvatar', $avatarIdGeneratorMock);
|
2009-01-09 19:25:06 +03:00
|
|
|
|
2009-01-03 22:50:13 +03:00
|
|
|
$user1 = new ForumUser();
|
|
|
|
$user1->id = 1;
|
|
|
|
$user1->username = "romanb";
|
|
|
|
$user1->avatar = new ForumAvatar();
|
|
|
|
// Fake managed state
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_unitOfWork->setEntityState($user1, \Doctrine\ORM\UnitOfWork::STATE_MANAGED);
|
2009-01-09 19:25:06 +03:00
|
|
|
|
2009-01-03 22:50:13 +03:00
|
|
|
$user2 = new ForumUser();
|
|
|
|
$user2->id = 2;
|
|
|
|
$user2->username = "jwage";
|
2009-01-03 22:53:07 +03:00
|
|
|
// Fake managed state
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_unitOfWork->setEntityState($user2, \Doctrine\ORM\UnitOfWork::STATE_MANAGED);
|
2009-01-03 22:50:13 +03:00
|
|
|
|
2009-01-04 19:15:32 +03:00
|
|
|
// Fake original entity date
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_unitOfWork->setOriginalEntityData($user1, array(
|
|
|
|
'id' => 1, 'username' => 'roman'
|
|
|
|
));
|
|
|
|
$this->_unitOfWork->setOriginalEntityData($user2, array(
|
|
|
|
'id' => 2, 'username' => 'jon'
|
|
|
|
));
|
|
|
|
|
2009-01-04 19:15:32 +03:00
|
|
|
// Go
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->_unitOfWork->computeEntityChangeSets(array($user1, $user2));
|
2009-01-03 22:50:13 +03:00
|
|
|
|
2009-01-04 19:15:32 +03:00
|
|
|
// Verify
|
2009-01-09 19:25:06 +03:00
|
|
|
$user1ChangeSet = $this->_unitOfWork->getEntityChangeSet($user1);
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->assertTrue(is_array($user1ChangeSet));
|
|
|
|
$this->assertEquals(2, count($user1ChangeSet));
|
|
|
|
$this->assertTrue(isset($user1ChangeSet['username']));
|
|
|
|
$this->assertEquals(array('roman' => 'romanb'), $user1ChangeSet['username']);
|
|
|
|
$this->assertTrue(isset($user1ChangeSet['avatar']));
|
|
|
|
$this->assertSame(array(null => $user1->avatar), $user1ChangeSet['avatar']);
|
|
|
|
|
2009-01-09 19:25:06 +03:00
|
|
|
$user2ChangeSet = $this->_unitOfWork->getEntityChangeSet($user2);
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->assertTrue(is_array($user2ChangeSet));
|
|
|
|
$this->assertEquals(1, count($user2ChangeSet));
|
|
|
|
$this->assertTrue(isset($user2ChangeSet['username']));
|
|
|
|
$this->assertEquals(array('jon' => 'jwage'), $user2ChangeSet['username']);
|
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/*
|
2008-07-21 00:13:24 +04:00
|
|
|
public function testSavingSingleEntityWithSequenceIdGeneratorSchedulesInsert()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingSingleEntityWithTableIdGeneratorSchedulesInsert()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingSingleEntityWithSingleNaturalIdForcesInsert()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingSingleEntityWithCompositeIdForcesInsert()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingEntityGraphWithIdentityColumnsForcesInserts()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingEntityGraphWithSequencesDelaysInserts()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingEntityGraphWithNaturalIdsForcesInserts()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingEntityGraphWithMixedIdGenerationStrategies()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2008-06-16 22:31:21 +04:00
|
|
|
}
|