2009-01-07 17:46:02 +00:00
|
|
|
<?php
|
|
|
|
|
2009-01-22 19:38:10 +00:00
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
use Doctrine\DBAL\Logging\DebugStack;
|
|
|
|
use Doctrine\ORM\EntityNotFoundException;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2016-06-18 13:01:59 +02:00
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException;
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\ORM\PersistentCollection;
|
|
|
|
use Doctrine\ORM\Proxy\Proxy;
|
2010-10-11 22:15:18 +02:00
|
|
|
use Doctrine\ORM\Query;
|
2015-09-29 16:36:48 -07:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsEmail;
|
2016-05-11 02:41:26 +07:00
|
|
|
use Doctrine\ORM\UnitOfWork;
|
2009-02-02 11:55:50 +00:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsAddress;
|
2010-07-08 00:20:54 +02:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsArticle;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsComment;
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
2016-05-11 02:41:26 +07:00
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
2009-01-22 19:38:10 +00:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
class BasicFunctionalTest extends OrmFunctionalTestCase
|
2009-02-18 07:59:11 +00:00
|
|
|
{
|
2009-03-30 19:43:05 +00:00
|
|
|
protected function setUp()
|
2009-02-18 07:59:11 +00:00
|
|
|
{
|
2009-03-30 19:43:05 +00:00
|
|
|
$this->useModelSet('cms');
|
|
|
|
parent::setUp();
|
2009-03-28 20:59:07 +00:00
|
|
|
}
|
2009-01-07 17:46:02 +00:00
|
|
|
|
2009-03-28 20:59:07 +00:00
|
|
|
public function testBasicUnitsOfWorkWithOneToManyAssociation()
|
|
|
|
{
|
2009-01-07 17:46:02 +00:00
|
|
|
// Create
|
|
|
|
$user = new CmsUser;
|
2009-02-02 11:55:50 +00:00
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($user);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->flush();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-01-07 17:46:02 +00:00
|
|
|
$this->assertTrue(is_numeric($user->id));
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->assertTrue($this->_em->contains($user));
|
2009-01-07 17:46:02 +00:00
|
|
|
|
|
|
|
// Read
|
2016-12-08 18:01:04 +01:00
|
|
|
$user2 = $this->_em->find(CmsUser::class, $user->id);
|
2009-01-09 16:25:06 +00:00
|
|
|
$this->assertTrue($user === $user2);
|
2009-01-08 11:23:24 +00:00
|
|
|
|
2009-01-09 16:25:06 +00:00
|
|
|
// Add a phonenumber
|
2009-01-08 11:23:24 +00:00
|
|
|
$ph = new CmsPhonenumber;
|
|
|
|
$ph->phonenumber = "12345";
|
2009-01-09 16:25:06 +00:00
|
|
|
$user->addPhonenumber($ph);
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->assertTrue($this->_em->contains($ph));
|
|
|
|
$this->assertTrue($this->_em->contains($user));
|
2009-01-08 11:23:24 +00:00
|
|
|
|
2009-01-29 17:00:44 +00:00
|
|
|
// Update name
|
2009-01-09 16:25:06 +00:00
|
|
|
$user->name = 'guilherme';
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->_em->flush();
|
2009-01-09 16:25:06 +00:00
|
|
|
$this->assertEquals('guilherme', $user->name);
|
|
|
|
|
2009-01-29 17:00:44 +00:00
|
|
|
// Add another phonenumber
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "6789";
|
|
|
|
$user->addPhonenumber($ph2);
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->assertTrue($this->_em->contains($ph2));
|
2009-01-29 17:00:44 +00:00
|
|
|
|
2009-01-09 16:25:06 +00:00
|
|
|
// Delete
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->remove($user);
|
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($user));
|
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
|
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->_em->flush();
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($user));
|
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
|
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user));
|
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($ph));
|
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($ph2));
|
2009-01-09 16:25:06 +00:00
|
|
|
}
|
2009-01-08 11:23:24 +00:00
|
|
|
|
2009-02-18 07:59:11 +00:00
|
|
|
public function testOneToManyAssociationModification()
|
|
|
|
{
|
2009-02-02 11:55:50 +00:00
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-01-12 13:34:41 +00:00
|
|
|
|
2009-02-02 11:55:50 +00:00
|
|
|
$ph1 = new CmsPhonenumber;
|
|
|
|
$ph1->phonenumber = "0301234";
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "987654321";
|
|
|
|
|
|
|
|
$user->addPhonenumber($ph1);
|
|
|
|
$user->addPhonenumber($ph2);
|
|
|
|
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($user);
|
2009-02-02 11:55:50 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
// Remove the first element from the collection
|
|
|
|
unset($user->phonenumbers[0]);
|
|
|
|
$ph1->user = null; // owning side!
|
2009-01-12 13:34:41 +00:00
|
|
|
|
2009-02-02 11:55:50 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($user->phonenumbers));
|
|
|
|
$this->assertNull($ph1->user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicOneToOne()
|
|
|
|
{
|
2010-08-09 13:13:21 +02:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
|
2009-02-02 11:55:50 +00:00
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-01-12 13:34:41 +00:00
|
|
|
|
2009-02-02 11:55:50 +00:00
|
|
|
$address = new CmsAddress;
|
|
|
|
$address->country = 'Germany';
|
|
|
|
$address->city = 'Berlin';
|
|
|
|
$address->zip = '12345';
|
|
|
|
|
|
|
|
$user->address = $address; // inverse side
|
|
|
|
$address->user = $user; // owning side!
|
|
|
|
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($user);
|
2009-01-12 13:34:41 +00:00
|
|
|
$this->_em->flush();
|
2009-02-02 11:55:50 +00:00
|
|
|
|
|
|
|
// Check that the foreign key has been set
|
2010-03-31 21:13:34 +00:00
|
|
|
$userId = $this->_em->getConnection()->executeQuery(
|
2016-12-07 23:33:41 +01:00
|
|
|
"SELECT user_id FROM cms_addresses WHERE id=?", [$address->id]
|
2010-03-31 21:13:34 +00:00
|
|
|
)->fetchColumn();
|
2009-02-02 11:55:50 +00:00
|
|
|
$this->assertTrue(is_numeric($userId));
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-03-29 13:20:41 +00:00
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-08-08 15:01:03 +02:00
|
|
|
$user2 = $this->_em->createQuery('select u from \Doctrine\Tests\Models\CMS\CmsUser u where u.id=?1')
|
2010-03-29 13:20:41 +00:00
|
|
|
->setParameter(1, $userId)
|
|
|
|
->getSingleResult();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-03-29 13:20:41 +00:00
|
|
|
// Address has been eager-loaded because it cant be lazy
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(CmsAddress::class, $user2->address);
|
|
|
|
$this->assertNotInstanceOf(Proxy::class, $user2->address);
|
2009-02-02 11:55:50 +00:00
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-06-28 21:37:53 +02:00
|
|
|
/**
|
|
|
|
* @group DDC-1230
|
|
|
|
*/
|
|
|
|
public function testRemove()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-06-28 21:37:53 +02:00
|
|
|
$this->_em->persist($user);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals(UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_MANAGED");
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-06-28 21:37:53 +02:00
|
|
|
$this->_em->remove($user);
|
2011-08-28 15:57:33 +02:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-06-28 21:37:53 +02:00
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$id = $user->getId();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-06-28 21:37:53 +02:00
|
|
|
$this->_em->remove($user);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals(UnitOfWork::STATE_REMOVED, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_REMOVED");
|
2011-06-28 21:37:53 +02:00
|
|
|
$this->_em->flush();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertNull($this->_em->find(CmsUser::class, $id));
|
2011-06-28 21:37:53 +02:00
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-23 09:52:16 +00:00
|
|
|
public function testOneToManyOrphanRemoval()
|
2009-07-21 15:53:58 +00:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
for ($i=0; $i<3; ++$i) {
|
|
|
|
$phone = new CmsPhonenumber;
|
|
|
|
$phone->phonenumber = 100 + $i;
|
|
|
|
$user->addPhonenumber($phone);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$user->getPhonenumbers()->remove(0);
|
2009-07-23 09:52:16 +00:00
|
|
|
$this->assertEquals(2, count($user->getPhonenumbers()));
|
2009-07-21 15:53:58 +00:00
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-07-23 09:52:16 +00:00
|
|
|
// Check that there are just 2 phonenumbers left
|
2010-07-08 17:30:39 +02:00
|
|
|
$count = $this->_em->getConnection()->fetchColumn("SELECT COUNT(*) FROM cms_phonenumbers");
|
2009-07-21 15:53:58 +00:00
|
|
|
$this->assertEquals(2, $count); // only 2 remaining
|
2010-07-08 17:30:39 +02:00
|
|
|
|
|
|
|
// check that clear() removes the others via orphan removal
|
|
|
|
$user->getPhonenumbers()->clear();
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_phonenumbers"));
|
2009-07-23 09:52:16 +00:00
|
|
|
}
|
2009-03-28 20:59:07 +00:00
|
|
|
|
|
|
|
public function testBasicQuery()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($user);
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");
|
|
|
|
|
2009-08-03 17:18:37 +00:00
|
|
|
$users = $query->getResult();
|
2009-03-28 20:59:07 +00:00
|
|
|
|
2009-08-03 13:25:56 +00:00
|
|
|
$this->assertEquals(1, count($users));
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->assertEquals('Guilherme', $users[0]->name);
|
|
|
|
$this->assertEquals('gblanco', $users[0]->username);
|
|
|
|
$this->assertEquals('developer', $users[0]->status);
|
2009-05-13 15:19:27 +00:00
|
|
|
//$this->assertNull($users[0]->phonenumbers);
|
|
|
|
//$this->assertNull($users[0]->articles);
|
2009-03-30 19:43:05 +00:00
|
|
|
|
2009-08-03 17:18:37 +00:00
|
|
|
$usersArray = $query->getArrayResult();
|
2009-03-30 19:43:05 +00:00
|
|
|
|
|
|
|
$this->assertTrue(is_array($usersArray));
|
|
|
|
$this->assertEquals(1, count($usersArray));
|
|
|
|
$this->assertEquals('Guilherme', $usersArray[0]['name']);
|
|
|
|
$this->assertEquals('gblanco', $usersArray[0]['username']);
|
|
|
|
$this->assertEquals('developer', $usersArray[0]['status']);
|
|
|
|
|
|
|
|
$usersScalar = $query->getScalarResult();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($usersScalar));
|
|
|
|
$this->assertEquals(1, count($usersScalar));
|
|
|
|
$this->assertEquals('Guilherme', $usersScalar[0]['u_name']);
|
|
|
|
$this->assertEquals('gblanco', $usersScalar[0]['u_username']);
|
|
|
|
$this->assertEquals('developer', $usersScalar[0]['u_status']);
|
2009-03-28 20:59:07 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 13:54:01 +00:00
|
|
|
public function testBasicOneToManyInnerJoin()
|
2009-03-28 20:59:07 +00:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($user);
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p");
|
|
|
|
|
2009-08-03 17:18:37 +00:00
|
|
|
$users = $query->getResult();
|
2009-03-28 20:59:07 +00:00
|
|
|
|
2009-08-03 13:25:56 +00:00
|
|
|
$this->assertEquals(0, count($users));
|
2009-03-28 20:59:07 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 13:54:01 +00:00
|
|
|
public function testBasicOneToManyLeftJoin()
|
2009-03-28 20:59:07 +00:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($user);
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select u,p from Doctrine\Tests\Models\CMS\CmsUser u left join u.phonenumbers p");
|
|
|
|
|
2009-08-03 17:18:37 +00:00
|
|
|
$users = $query->getResult();
|
2009-03-28 20:59:07 +00:00
|
|
|
|
2009-08-03 13:25:56 +00:00
|
|
|
$this->assertEquals(1, count($users));
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->assertEquals('Guilherme', $users[0]->name);
|
|
|
|
$this->assertEquals('gblanco', $users[0]->username);
|
|
|
|
$this->assertEquals('developer', $users[0]->status);
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(PersistentCollection::class, $users[0]->phonenumbers);
|
2010-02-19 21:28:17 +00:00
|
|
|
$this->assertTrue($users[0]->phonenumbers->isInitialized());
|
2009-03-28 20:59:07 +00:00
|
|
|
$this->assertEquals(0, $users[0]->phonenumbers->count());
|
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-21 15:53:58 +00:00
|
|
|
public function testBasicRefresh()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-21 15:53:58 +00:00
|
|
|
$user->status = 'mascot';
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-21 15:53:58 +00:00
|
|
|
$this->assertEquals('mascot', $user->status);
|
|
|
|
$this->_em->refresh($user);
|
|
|
|
$this->assertEquals('developer', $user->status);
|
|
|
|
}
|
2010-10-11 22:15:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-833
|
|
|
|
*/
|
|
|
|
public function testRefreshResetsCollection()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
// Add a phonenumber
|
|
|
|
$ph1 = new CmsPhonenumber;
|
|
|
|
$ph1->phonenumber = "12345";
|
|
|
|
$user->addPhonenumber($ph1);
|
|
|
|
|
|
|
|
// Add a phonenumber
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "54321";
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->persist($ph1);
|
|
|
|
$this->_em->persist($ph2);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$user->addPhonenumber($ph2);
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($user->phonenumbers));
|
|
|
|
$this->_em->refresh($user);
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($user->phonenumbers));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-833
|
|
|
|
*/
|
|
|
|
public function testDqlRefreshResetsCollection()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
// Add a phonenumber
|
|
|
|
$ph1 = new CmsPhonenumber;
|
|
|
|
$ph1->phonenumber = "12345";
|
|
|
|
$user->addPhonenumber($ph1);
|
|
|
|
|
|
|
|
// Add a phonenumber
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "54321";
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->persist($ph1);
|
|
|
|
$this->_em->persist($ph2);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$user->addPhonenumber($ph2);
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($user->phonenumbers));
|
|
|
|
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
|
|
|
|
$user = $this->_em->createQuery($dql)
|
|
|
|
->setParameter(1, $user->id)
|
|
|
|
->setHint(Query::HINT_REFRESH, true)
|
|
|
|
->getSingleResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($user->phonenumbers));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-833
|
|
|
|
*/
|
|
|
|
public function testCreateEntityOfProxy()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
// Add a phonenumber
|
|
|
|
$ph1 = new CmsPhonenumber;
|
|
|
|
$ph1->phonenumber = "12345";
|
|
|
|
$user->addPhonenumber($ph1);
|
|
|
|
|
|
|
|
// Add a phonenumber
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "54321";
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->persist($ph1);
|
|
|
|
$this->_em->persist($ph2);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$userId = $user->id;
|
2016-12-08 18:01:04 +01:00
|
|
|
$user = $this->_em->getReference(CmsUser::class, $user->id);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-10-11 22:15:18 +02:00
|
|
|
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
|
|
|
|
$user = $this->_em->createQuery($dql)
|
|
|
|
->setParameter(1, $userId)
|
|
|
|
->getSingleResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($user->phonenumbers));
|
|
|
|
}
|
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
public function testAddToCollectionDoesNotInitialize()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
for ($i=0; $i<3; ++$i) {
|
|
|
|
$phone = new CmsPhonenumber;
|
|
|
|
$phone->phonenumber = 100 + $i;
|
|
|
|
$user->addPhonenumber($phone);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->assertEquals(3, $user->getPhonenumbers()->count());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
|
|
|
|
|
|
|
|
$gblanco = $query->getSingleResult();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$newPhone = new CmsPhonenumber;
|
2009-11-07 11:41:16 +00:00
|
|
|
$newPhone->phonenumber = 555;
|
|
|
|
$gblanco->addPhonenumber($newPhone);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
|
2009-11-08 11:07:49 +00:00
|
|
|
$this->_em->persist($gblanco);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-08 11:07:49 +00:00
|
|
|
$query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
|
|
|
|
$gblanco2 = $query->getSingleResult();
|
|
|
|
$this->assertEquals(4, $gblanco2->getPhonenumbers()->count());
|
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-08 11:07:49 +00:00
|
|
|
public function testInitializeCollectionWithNewObjectsRetainsNewObjects()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
for ($i=0; $i<3; ++$i) {
|
|
|
|
$phone = new CmsPhonenumber;
|
|
|
|
$phone->phonenumber = 100 + $i;
|
|
|
|
$user->addPhonenumber($phone);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-08 11:07:49 +00:00
|
|
|
$this->assertEquals(3, $user->getPhonenumbers()->count());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-08 11:07:49 +00:00
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
|
|
|
|
|
|
|
|
$gblanco = $query->getSingleResult();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-08 11:07:49 +00:00
|
|
|
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-08 11:07:49 +00:00
|
|
|
$newPhone = new CmsPhonenumber;
|
|
|
|
$newPhone->phonenumber = 555;
|
|
|
|
$gblanco->addPhonenumber($newPhone);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-08 11:07:49 +00:00
|
|
|
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
|
|
|
|
$this->assertEquals(4, $gblanco->getPhonenumbers()->count());
|
|
|
|
$this->assertTrue($gblanco->getPhonenumbers()->isInitialized());
|
2009-10-15 14:39:43 +00:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
|
|
|
|
$gblanco2 = $query->getSingleResult();
|
|
|
|
$this->assertEquals(4, $gblanco2->getPhonenumbers()->count());
|
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
public function testSetSetAssociationWithGetReference()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
2011-12-19 22:56:19 +01:00
|
|
|
$user->status = 'developer';
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->_em->persist($user);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$address = new CmsAddress;
|
|
|
|
$address->country = 'Germany';
|
|
|
|
$address->city = 'Berlin';
|
|
|
|
$address->zip = '12345';
|
|
|
|
$this->_em->persist($address);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->detach($address);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->assertFalse($this->_em->contains($address));
|
|
|
|
$this->assertTrue($this->_em->contains($user));
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
// Assume we only got the identifier of the address and now want to attach
|
|
|
|
// that address to the user without actually loading it, using getReference().
|
2016-12-08 18:01:04 +01:00
|
|
|
$addressRef = $this->_em->getReference(CmsAddress::class, $address->getId());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-15 14:39:43 +00:00
|
|
|
$user->setAddress($addressRef); // Ugh! Initializes address 'cause of $address->setUser($user)!
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
// Check with a fresh load that the association is indeed there
|
|
|
|
$query = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u join u.address a where u.username='gblanco'");
|
|
|
|
$gblanco = $query->getSingleResult();
|
2009-10-15 14:39:43 +00:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(CmsUser::class, $gblanco);
|
|
|
|
$this->assertInstanceOf(CmsAddress::class, $gblanco->getAddress());
|
2009-10-07 12:39:46 +00:00
|
|
|
$this->assertEquals('Berlin', $gblanco->getAddress()->getCity());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-07 12:39:46 +00:00
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-13 19:34:10 +00:00
|
|
|
public function testOneToManyCascadeRemove()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
for ($i=0; $i<3; ++$i) {
|
|
|
|
$phone = new CmsPhonenumber;
|
|
|
|
$phone->phonenumber = 100 + $i;
|
|
|
|
$user->addPhonenumber($phone);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-13 19:34:10 +00:00
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
|
|
|
|
$gblanco = $query->getSingleResult();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-13 19:34:10 +00:00
|
|
|
$this->_em->remove($gblanco);
|
|
|
|
$this->_em->flush();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-13 19:34:10 +00:00
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-13 19:34:10 +00:00
|
|
|
$this->assertEquals(0, $this->_em->createQuery(
|
|
|
|
"select count(p.phonenumber) from Doctrine\Tests\Models\CMS\CmsPhonenumber p")
|
|
|
|
->getSingleScalarResult());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-13 19:34:10 +00:00
|
|
|
$this->assertEquals(0, $this->_em->createQuery(
|
|
|
|
"select count(u.id) from Doctrine\Tests\Models\CMS\CmsUser u")
|
|
|
|
->getSingleScalarResult());
|
|
|
|
}
|
2009-12-06 23:40:38 +00:00
|
|
|
|
|
|
|
public function testTextColumnSaveAndRetrieve()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$article = new CmsArticle();
|
2009-12-06 23:40:38 +00:00
|
|
|
$article->text = "Lorem ipsum dolor sunt.";
|
|
|
|
$article->topic = "A Test Article!";
|
|
|
|
$article->setAuthor($user);
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->flush();
|
|
|
|
$articleId = $article->id;
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2010-08-08 15:01:03 +02:00
|
|
|
// test find() with leading backslash at the same time
|
|
|
|
$articleNew = $this->_em->find('\Doctrine\Tests\Models\CMS\CmsArticle', $articleId);
|
|
|
|
$this->assertTrue($this->_em->contains($articleNew));
|
2009-12-06 23:49:30 +00:00
|
|
|
$this->assertEquals("Lorem ipsum dolor sunt.", $articleNew->text);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-12-06 23:49:30 +00:00
|
|
|
$this->assertNotSame($article, $articleNew);
|
|
|
|
|
|
|
|
$articleNew->text = "Lorem ipsum dolor sunt. And stuff!";
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$articleNew = $this->_em->find(CmsArticle::class, $articleId);
|
2009-12-06 23:49:30 +00:00
|
|
|
$this->assertEquals("Lorem ipsum dolor sunt. And stuff!", $articleNew->text);
|
2010-07-09 13:18:53 +02:00
|
|
|
$this->assertTrue($this->_em->contains($articleNew));
|
2009-12-06 23:40:38 +00:00
|
|
|
}
|
2010-07-08 00:20:54 +02:00
|
|
|
|
2009-12-09 17:00:18 +00:00
|
|
|
public function testFlushDoesNotIssueUnnecessaryUpdates()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-12-09 17:00:18 +00:00
|
|
|
$address = new CmsAddress;
|
|
|
|
$address->country = 'Germany';
|
|
|
|
$address->city = 'Berlin';
|
|
|
|
$address->zip = '12345';
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-12-09 17:00:18 +00:00
|
|
|
$address->user = $user;
|
|
|
|
$user->address = $address;
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$article = new CmsArticle();
|
2009-12-10 21:27:20 +00:00
|
|
|
$article->text = "Lorem ipsum dolor sunt.";
|
|
|
|
$article->topic = "A Test Article!";
|
|
|
|
$article->setAuthor($user);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-12-10 21:27:20 +00:00
|
|
|
$this->_em->persist($article);
|
2009-12-09 17:00:18 +00:00
|
|
|
$this->_em->persist($user);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-03-31 20:47:35 +00:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-12-09 17:00:18 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-12-10 21:27:20 +00:00
|
|
|
$query = $this->_em->createQuery('select u,a,ad from Doctrine\Tests\Models\CMS\CmsUser u join u.articles a join u.address ad');
|
2009-12-09 17:00:18 +00:00
|
|
|
$user2 = $query->getSingleResult();
|
2009-12-10 21:27:20 +00:00
|
|
|
|
|
|
|
$this->assertEquals(1, count($user2->articles));
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(CmsAddress::class, $user2->address);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-03-31 20:47:35 +00:00
|
|
|
$oldLogger = $this->_em->getConnection()->getConfiguration()->getSQLLogger();
|
2016-05-11 02:41:26 +07:00
|
|
|
$debugStack = new DebugStack();
|
2010-03-31 20:47:35 +00:00
|
|
|
$this->_em->getConnection()->getConfiguration()->setSQLLogger($debugStack);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-12-09 17:00:18 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->assertEquals(0, count($debugStack->queries));
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-03-31 20:47:35 +00:00
|
|
|
$this->_em->getConnection()->getConfiguration()->setSQLLogger($oldLogger);
|
2009-12-09 17:00:18 +00:00
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-02-10 10:47:42 +00:00
|
|
|
public function testRemoveEntityByReference()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-03-31 20:47:35 +00:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-02-10 10:47:42 +00:00
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$userRef = $this->_em->getReference(CmsUser::class, $user->getId());
|
2010-02-10 10:47:42 +00:00
|
|
|
$this->_em->remove($userRef);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-02-10 10:47:42 +00:00
|
|
|
$this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_users"));
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-03-31 20:47:35 +00:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(null);
|
2010-02-10 10:47:42 +00:00
|
|
|
}
|
2010-07-08 00:20:54 +02:00
|
|
|
|
2010-02-23 14:58:12 +00:00
|
|
|
public function testQueryEntityByReference()
|
2010-02-20 18:27:05 +00:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-02-20 18:27:05 +00:00
|
|
|
$address = new CmsAddress;
|
|
|
|
$address->country = 'Germany';
|
|
|
|
$address->city = 'Berlin';
|
|
|
|
$address->zip = '12345';
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-02-20 18:27:05 +00:00
|
|
|
$user->setAddress($address);
|
2010-05-13 13:19:59 +02:00
|
|
|
|
|
|
|
$this->_em->transactional(function($em) use($user) {
|
|
|
|
$em->persist($user);
|
|
|
|
});
|
2010-02-20 18:27:05 +00:00
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-03-31 20:47:35 +00:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$userRef = $this->_em->getReference(CmsUser::class, $user->getId());
|
2010-02-20 18:27:05 +00:00
|
|
|
$address2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsAddress a where a.user = :user')
|
|
|
|
->setParameter('user', $userRef)
|
|
|
|
->getSingleResult();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(Proxy::class, $address2->getUser());
|
2010-02-23 14:58:12 +00:00
|
|
|
$this->assertTrue($userRef === $address2->getUser());
|
|
|
|
$this->assertFalse($userRef->__isInitialized__);
|
|
|
|
$this->assertEquals('Germany', $address2->country);
|
|
|
|
$this->assertEquals('Berlin', $address2->city);
|
|
|
|
$this->assertEquals('12345', $address2->zip);
|
|
|
|
}
|
2010-07-08 00:20:54 +02:00
|
|
|
|
|
|
|
public function testOneToOneNullUpdate()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
|
|
|
|
$address = new CmsAddress();
|
|
|
|
$address->city = "Bonn";
|
|
|
|
$address->zip = "12354";
|
|
|
|
$address->country = "Germany";
|
|
|
|
$address->street = "somestreet";
|
|
|
|
$address->user = $user;
|
|
|
|
|
|
|
|
$this->_em->persist($address);
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id));
|
|
|
|
|
|
|
|
$address->user = null;
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2010-07-28 23:36:52 +02:00
|
|
|
$this->assertNotEquals(1, $this->_em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id));
|
2010-07-08 00:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-600
|
|
|
|
* @group DDC-455
|
|
|
|
*/
|
|
|
|
public function testNewAssociatedEntityDuringFlushThrowsException()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
|
|
|
|
$address = new CmsAddress();
|
|
|
|
$address->city = "Bonn";
|
|
|
|
$address->zip = "12354";
|
|
|
|
$address->country = "Germany";
|
|
|
|
$address->street = "somestreet";
|
|
|
|
$address->user = $user;
|
|
|
|
|
|
|
|
$this->_em->persist($address);
|
2017-05-31 08:14:53 +02:00
|
|
|
|
|
|
|
// flushing without persisting $user should raise an exception
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->_em->flush();
|
2010-07-08 00:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-600
|
|
|
|
* @group DDC-455
|
|
|
|
*/
|
|
|
|
public function testNewAssociatedEntityDuringFlushThrowsException2()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
|
|
|
|
$address = new CmsAddress();
|
|
|
|
$address->city = "Bonn";
|
|
|
|
$address->zip = "12354";
|
|
|
|
$address->country = "Germany";
|
|
|
|
$address->street = "somestreet";
|
|
|
|
$address->user = $user;
|
|
|
|
|
|
|
|
$this->_em->persist($address);
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$u2 = new CmsUser;
|
|
|
|
$u2->username = "beberlei";
|
|
|
|
$u2->name = "Benjamin E.";
|
|
|
|
$u2->status = 'inactive';
|
|
|
|
$address->user = $u2;
|
2017-05-31 08:14:53 +02:00
|
|
|
|
|
|
|
// flushing without persisting $u2 should raise an exception
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->_em->flush();
|
2010-07-08 00:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-600
|
|
|
|
* @group DDC-455
|
|
|
|
*/
|
|
|
|
public function testNewAssociatedEntityDuringFlushThrowsException3()
|
|
|
|
{
|
|
|
|
$art = new CmsArticle();
|
|
|
|
$art->topic = 'topic';
|
|
|
|
$art->text = 'the text';
|
|
|
|
|
|
|
|
$com = new CmsComment();
|
|
|
|
$com->topic = 'Good';
|
|
|
|
$com->text = 'Really good!';
|
|
|
|
$art->addComment($com);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2010-07-08 00:20:54 +02:00
|
|
|
$this->_em->persist($art);
|
2017-05-31 08:14:53 +02:00
|
|
|
|
|
|
|
// flushing without persisting $com should raise an exception
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->_em->flush();
|
2010-07-08 00:20:54 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 16:36:48 -07:00
|
|
|
/**
|
|
|
|
* @group DDC-2922
|
|
|
|
*/
|
|
|
|
public function testNewAssociatedEntityWorksWithJustOnePath()
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* First we persist and flush an e-mail with no user. This seems
|
|
|
|
* Save an un-owned email with no user. This seems to
|
|
|
|
* matter for reproducing the bug
|
|
|
|
*/
|
|
|
|
$mail = new CmsEmail();
|
|
|
|
$mail->email = "nobody@example.com";
|
|
|
|
$mail->user = null;
|
|
|
|
|
|
|
|
$this->_em->persist($mail);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
|
|
|
|
$mail->user = $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note that we have NOT directly persisted the CmsUser, and CmsAddress
|
|
|
|
* does NOT have cascade-persist.
|
|
|
|
*
|
|
|
|
* However, CmsEmail *does* have a cascade-persist, which ought to
|
|
|
|
* allow us to save the CmsUser anyway through that connection.
|
|
|
|
*/
|
|
|
|
$address = new CmsAddress();
|
|
|
|
$address->city = "Bonn";
|
|
|
|
$address->zip = "12354";
|
|
|
|
$address->country = "Germany";
|
|
|
|
$address->street = "somestreet";
|
|
|
|
$address->user = $user;
|
|
|
|
|
|
|
|
$this->_em->persist($address);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-08 00:20:54 +02:00
|
|
|
public function testOneToOneOrphanRemoval()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
|
|
|
|
$address = new CmsAddress();
|
|
|
|
$address->city = "Bonn";
|
|
|
|
$address->zip = "12354";
|
|
|
|
$address->country = "Germany";
|
|
|
|
$address->street = "somestreet";
|
|
|
|
$address->user = $user;
|
|
|
|
$user->address = $address;
|
|
|
|
|
|
|
|
$this->_em->persist($address);
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$addressId = $address->getId();
|
|
|
|
|
|
|
|
$user->address = null;
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
|
|
|
|
|
|
|
|
// check orphan removal through replacement
|
|
|
|
$user->address = $address;
|
|
|
|
$address->user = $user;
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
|
|
|
|
|
2010-08-10 22:07:43 +02:00
|
|
|
// remove $address to free up unique key id
|
|
|
|
$this->_em->remove($address);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2010-07-08 00:20:54 +02:00
|
|
|
$newAddress = new CmsAddress();
|
|
|
|
$newAddress->city = "NewBonn";
|
|
|
|
$newAddress->zip = "12354";
|
|
|
|
$newAddress->country = "NewGermany";
|
|
|
|
$newAddress->street = "somenewstreet";
|
|
|
|
$newAddress->user = $user;
|
|
|
|
$user->address = $newAddress;
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
|
|
|
|
}
|
2010-07-08 17:30:39 +02:00
|
|
|
|
2010-07-20 14:20:13 +02:00
|
|
|
public function testGetPartialReferenceToUpdateObjectWithoutLoadingIt()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$userId = $user->id;
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$user = $this->_em->getPartialReference(CmsUser::class, $userId);
|
2010-07-20 14:20:13 +02:00
|
|
|
$this->assertTrue($this->_em->contains($user));
|
|
|
|
$this->assertNull($user->getName());
|
|
|
|
$this->assertEquals($userId, $user->id);
|
|
|
|
|
|
|
|
$user->name = 'Stephan';
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2011-10-15 11:52:41 +02:00
|
|
|
$this->assertEquals('Benjamin E.', $this->_em->find(get_class($user), $userId)->name);
|
2010-07-20 14:20:13 +02:00
|
|
|
}
|
2010-07-29 14:08:36 +02:00
|
|
|
|
|
|
|
public function testMergePersistsNewEntities()
|
2009-11-21 18:52:02 +00:00
|
|
|
{
|
2010-07-29 14:08:36 +02:00
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
|
|
|
|
$managedUser = $this->_em->merge($user);
|
|
|
|
$this->assertEquals('beberlei', $managedUser->username);
|
|
|
|
$this->assertEquals('Benjamin E.', $managedUser->name);
|
|
|
|
$this->assertEquals('active', $managedUser->status);
|
|
|
|
|
|
|
|
$this->assertTrue($user !== $managedUser);
|
|
|
|
$this->assertTrue($this->_em->contains($managedUser));
|
|
|
|
|
2009-11-21 18:52:02 +00:00
|
|
|
$this->_em->flush();
|
2010-07-29 14:08:36 +02:00
|
|
|
$userId = $managedUser->id;
|
2009-11-21 18:52:02 +00:00
|
|
|
$this->_em->clear();
|
2010-07-29 14:08:36 +02:00
|
|
|
|
2010-07-29 22:27:00 +02:00
|
|
|
$user2 = $this->_em->find(get_class($managedUser), $userId);
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(CmsUser::class, $user2);
|
2010-07-29 14:08:36 +02:00
|
|
|
}
|
|
|
|
|
2011-11-02 12:34:28 +01:00
|
|
|
public function testMergeNonPersistedProperties()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
$user->nonPersistedProperty = 'test';
|
|
|
|
$user->nonPersistedPropertyObject = new CmsPhonenumber();
|
|
|
|
|
|
|
|
$managedUser = $this->_em->merge($user);
|
|
|
|
$this->assertEquals('test', $managedUser->nonPersistedProperty);
|
|
|
|
$this->assertSame($user->nonPersistedProperty, $managedUser->nonPersistedProperty);
|
|
|
|
$this->assertSame($user->nonPersistedPropertyObject, $managedUser->nonPersistedPropertyObject);
|
|
|
|
|
|
|
|
$this->assertTrue($user !== $managedUser);
|
|
|
|
$this->assertTrue($this->_em->contains($managedUser));
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$userId = $managedUser->id;
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$user2 = $this->_em->find(get_class($managedUser), $userId);
|
|
|
|
$this->assertNull($user2->nonPersistedProperty);
|
|
|
|
$this->assertNull($user2->nonPersistedPropertyObject);
|
|
|
|
$this->assertEquals('active', $user2->status);
|
|
|
|
}
|
|
|
|
|
2010-07-29 14:08:36 +02:00
|
|
|
public function testMergeThrowsExceptionIfEntityWithGeneratedIdentifierDoesNotExist()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
$user->id = 42;
|
2017-05-31 08:14:53 +02:00
|
|
|
|
|
|
|
$this->expectException(EntityNotFoundException::class);
|
|
|
|
$this->_em->merge($user);
|
2009-11-21 18:52:02 +00:00
|
|
|
}
|
2010-07-29 22:27:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-634
|
|
|
|
*/
|
|
|
|
public function testOneToOneMergeSetNull()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
|
|
|
|
$ph = new CmsPhonenumber();
|
|
|
|
$ph->phonenumber = "12345";
|
|
|
|
$user->addPhonenumber($ph);
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->persist($ph);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$ph->user = null;
|
|
|
|
$managedPh = $this->_em->merge($ph);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$this->assertNull($this->_em->find(get_class($ph), $ph->phonenumber)->getUser());
|
|
|
|
}
|
2011-03-16 22:51:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-952
|
|
|
|
*/
|
|
|
|
public function testManyToOneFetchModeQuery()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->username = "beberlei";
|
|
|
|
$user->name = "Benjamin E.";
|
|
|
|
$user->status = 'active';
|
|
|
|
|
|
|
|
$article = new CmsArticle();
|
|
|
|
$article->topic = "foo";
|
|
|
|
$article->text = "bar";
|
|
|
|
$article->user = $user;
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$qc = $this->getCurrentQueryCount();
|
|
|
|
$dql = "SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.id = ?1";
|
|
|
|
$article = $this->_em->createQuery($dql)
|
|
|
|
->setParameter(1, $article->id)
|
2016-12-08 18:01:04 +01:00
|
|
|
->setFetchMode(CmsArticle::class, 'user', ClassMetadata::FETCH_EAGER)
|
2011-03-16 22:51:32 +01:00
|
|
|
->getSingleResult();
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(Proxy::class, $article->user, "It IS a proxy, ...");
|
2011-03-16 22:51:32 +01:00
|
|
|
$this->assertTrue($article->user->__isInitialized__, "...but its initialized!");
|
|
|
|
$this->assertEquals($qc+2, $this->getCurrentQueryCount());
|
|
|
|
}
|
2011-08-14 16:12:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1278
|
|
|
|
*/
|
|
|
|
public function testClearWithEntityName()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$address = new CmsAddress();
|
|
|
|
$address->city = "Springfield";
|
|
|
|
$address->zip = "12354";
|
|
|
|
$address->country = "Germany";
|
|
|
|
$address->street = "Foo Street";
|
|
|
|
$address->user = $user;
|
|
|
|
$user->address = $address;
|
|
|
|
|
|
|
|
$article1 = new CmsArticle();
|
|
|
|
$article1->topic = 'Foo';
|
|
|
|
$article1->text = 'Foo Text';
|
|
|
|
|
|
|
|
$article2 = new CmsArticle();
|
|
|
|
$article2->topic = 'Bar';
|
|
|
|
$article2->text = 'Bar Text';
|
|
|
|
|
|
|
|
$user->addArticle($article1);
|
|
|
|
$user->addArticle($article2);
|
|
|
|
|
|
|
|
$this->_em->persist($article1);
|
|
|
|
$this->_em->persist($article2);
|
|
|
|
$this->_em->persist($address);
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$unitOfWork = $this->_em->getUnitOfWork();
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->_em->clear(CmsUser::class);
|
2011-08-14 16:12:12 +02:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($user));
|
|
|
|
$this->assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($article1));
|
|
|
|
$this->assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($article2));
|
|
|
|
$this->assertEquals(UnitOfWork::STATE_MANAGED, $unitOfWork->getEntityState($address));
|
2011-08-14 16:12:12 +02:00
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($address));
|
2011-08-14 16:12:12 +02:00
|
|
|
}
|
2011-10-22 13:44:33 +02:00
|
|
|
|
2012-03-15 16:53:50 -05:00
|
|
|
public function testFlushManyExplicitEntities()
|
|
|
|
{
|
|
|
|
$userA = new CmsUser;
|
|
|
|
$userA->username = 'UserA';
|
|
|
|
$userA->name = 'UserA';
|
|
|
|
|
|
|
|
$userB = new CmsUser;
|
|
|
|
$userB->username = 'UserB';
|
|
|
|
$userB->name = 'UserB';
|
|
|
|
|
|
|
|
$userC = new CmsUser;
|
|
|
|
$userC->username = 'UserC';
|
|
|
|
$userC->name = 'UserC';
|
|
|
|
|
|
|
|
$this->_em->persist($userA);
|
|
|
|
$this->_em->persist($userB);
|
|
|
|
$this->_em->persist($userC);
|
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->_em->flush([$userA, $userB, $userB]);
|
2012-03-15 16:53:50 -05:00
|
|
|
|
|
|
|
$userC->name = 'changed name';
|
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->_em->flush([$userA, $userB]);
|
2012-03-15 16:53:50 -05:00
|
|
|
$this->_em->refresh($userC);
|
|
|
|
|
|
|
|
$this->assertTrue($userA->id > 0, 'user a has an id');
|
|
|
|
$this->assertTrue($userB->id > 0, 'user b has an id');
|
|
|
|
$this->assertTrue($userC->id > 0, 'user c has an id');
|
|
|
|
$this->assertEquals('UserC', $userC->name, 'name has not changed because we did not flush it');
|
|
|
|
}
|
|
|
|
|
2011-10-22 13:44:33 +02:00
|
|
|
/**
|
|
|
|
* @group DDC-720
|
|
|
|
*/
|
|
|
|
public function testFlushSingleManagedEntity()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$user->status = 'administrator';
|
|
|
|
$this->_em->flush($user);
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$user = $this->_em->find(get_class($user), $user->id);
|
|
|
|
$this->assertEquals('administrator', $user->status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-720
|
|
|
|
*/
|
|
|
|
public function testFlushSingleUnmanagedEntity()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
2016-06-18 13:01:59 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Entity has to be managed or scheduled for removal for single computation');
|
|
|
|
|
2011-10-22 13:44:33 +02:00
|
|
|
$this->_em->flush($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-720
|
|
|
|
*/
|
|
|
|
public function testFlushSingleAndNewEntity()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$otherUser = new CmsUser;
|
|
|
|
$otherUser->name = 'Dominik2';
|
|
|
|
$otherUser->username = 'domnikl2';
|
|
|
|
$otherUser->status = 'developer';
|
|
|
|
|
|
|
|
$user->status = 'administrator';
|
|
|
|
|
|
|
|
$this->_em->persist($otherUser);
|
|
|
|
$this->_em->flush($user);
|
|
|
|
|
|
|
|
$this->assertTrue($this->_em->contains($otherUser), "Other user is contained in EntityManager");
|
|
|
|
$this->assertTrue($otherUser->id > 0, "other user has an id");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-720
|
|
|
|
*/
|
|
|
|
public function testFlushAndCascadePersist()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$address = new CmsAddress();
|
|
|
|
$address->city = "Springfield";
|
|
|
|
$address->zip = "12354";
|
|
|
|
$address->country = "Germany";
|
|
|
|
$address->street = "Foo Street";
|
|
|
|
$address->user = $user;
|
|
|
|
$user->address = $address;
|
|
|
|
|
|
|
|
$this->_em->flush($user);
|
|
|
|
|
|
|
|
$this->assertTrue($this->_em->contains($address), "Other user is contained in EntityManager");
|
|
|
|
$this->assertTrue($address->id > 0, "other user has an id");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-720
|
|
|
|
*/
|
|
|
|
public function testFlushSingleAndNoCascade()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$article1 = new CmsArticle();
|
|
|
|
$article1->topic = 'Foo';
|
|
|
|
$article1->text = 'Foo Text';
|
|
|
|
$article1->author = $user;
|
|
|
|
$user->articles[] = $article1;
|
|
|
|
|
2016-06-18 13:01:59 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage("A new entity was found through the relationship 'Doctrine\Tests\Models\CMS\CmsUser#articles'");
|
|
|
|
|
2011-10-22 13:44:33 +02:00
|
|
|
$this->_em->flush($user);
|
|
|
|
}
|
|
|
|
|
2012-01-21 13:06:30 +01:00
|
|
|
/**
|
|
|
|
* @group DDC-720
|
|
|
|
* @group DDC-1612
|
2013-05-04 13:36:37 +02:00
|
|
|
* @group DDC-2267
|
2012-01-21 13:06:30 +01:00
|
|
|
*/
|
2013-05-04 13:36:37 +02:00
|
|
|
public function testFlushSingleNewEntityThenRemove()
|
2012-01-21 13:06:30 +01:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush($user);
|
2013-05-04 13:36:37 +02:00
|
|
|
|
|
|
|
$userId = $user->id;
|
|
|
|
|
|
|
|
$this->_em->remove($user);
|
|
|
|
$this->_em->flush($user);
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$this->assertNull($this->_em->find(get_class($user), $userId));
|
2012-01-21 13:06:30 +01:00
|
|
|
}
|
|
|
|
|
2011-10-22 13:44:33 +02:00
|
|
|
/**
|
|
|
|
* @group DDC-720
|
|
|
|
*/
|
|
|
|
public function testProxyIsIgnored()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$user = $this->_em->getReference(get_class($user), $user->id);
|
|
|
|
|
|
|
|
$otherUser = new CmsUser;
|
|
|
|
$otherUser->name = 'Dominik2';
|
|
|
|
$otherUser->username = 'domnikl2';
|
|
|
|
$otherUser->status = 'developer';
|
|
|
|
|
|
|
|
$this->_em->persist($otherUser);
|
|
|
|
$this->_em->flush($user);
|
|
|
|
|
|
|
|
$this->assertTrue($this->_em->contains($otherUser), "Other user is contained in EntityManager");
|
|
|
|
$this->assertTrue($otherUser->id > 0, "other user has an id");
|
|
|
|
}
|
2011-10-22 14:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-720
|
|
|
|
*/
|
|
|
|
public function testFlushSingleSaveOnlySingle()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
$this->_em->persist($user);
|
|
|
|
|
|
|
|
$user2 = new CmsUser;
|
|
|
|
$user2->name = 'Dominik';
|
|
|
|
$user2->username = 'domnikl2';
|
|
|
|
$user2->status = 'developer';
|
|
|
|
$this->_em->persist($user2);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$user->status = 'admin';
|
|
|
|
$user2->status = 'admin';
|
|
|
|
|
|
|
|
$this->_em->flush($user);
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$user2 = $this->_em->find(get_class($user2), $user2->id);
|
|
|
|
$this->assertEquals('developer', $user2->status);
|
|
|
|
}
|
2012-01-15 14:58:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1585
|
|
|
|
*/
|
2013-03-11 00:08:58 +00:00
|
|
|
public function testWrongAssociationInstance()
|
2012-01-15 14:58:56 +01:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Dominik';
|
|
|
|
$user->username = 'domnikl';
|
|
|
|
$user->status = 'developer';
|
|
|
|
$user->address = $user;
|
|
|
|
|
2016-06-18 13:01:59 +02:00
|
|
|
$this->expectException(ORMInvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
'Expected value of type "Doctrine\Tests\Models\CMS\CmsAddress" for association field ' .
|
|
|
|
'"Doctrine\Tests\Models\CMS\CmsUser#$address", got "Doctrine\Tests\Models\CMS\CmsUser" instead.'
|
2015-01-18 00:59:58 +01:00
|
|
|
);
|
|
|
|
|
2012-01-15 14:58:56 +01:00
|
|
|
$this->_em->persist($user);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
}
|
2009-07-28 11:43:42 +00:00
|
|
|
}
|