2009-01-07 20:46:02 +03:00
|
|
|
<?php
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
use Doctrine\ORM\Tools\SchemaTool;
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
|
2009-02-02 14:55:50 +03:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsAddress;
|
2009-02-06 20:16:39 +03:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsGroup;
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-01-24 19:56:44 +03:00
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
2009-01-07 20:46:02 +03:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
2009-02-18 10:59:11 +03:00
|
|
|
{
|
2009-03-30 23:43:05 +04:00
|
|
|
protected function setUp()
|
2009-02-18 10:59:11 +03:00
|
|
|
{
|
2009-03-30 23:43:05 +04:00
|
|
|
$this->useModelSet('cms');
|
|
|
|
parent::setUp();
|
2009-03-28 23:59:07 +03:00
|
|
|
}
|
2009-01-07 20:46:02 +03:00
|
|
|
|
2009-03-28 23:59:07 +03:00
|
|
|
public function testBasicUnitsOfWorkWithOneToManyAssociation()
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
// Create
|
|
|
|
$user = new CmsUser;
|
2009-02-02 14:55:50 +03:00
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-01-07 20:46:02 +03:00
|
|
|
$this->assertTrue(is_numeric($user->id));
|
2009-03-28 23:59:07 +03:00
|
|
|
$this->assertTrue($this->_em->contains($user));
|
2009-01-07 20:46:02 +03:00
|
|
|
|
|
|
|
// Read
|
2009-03-28 23:59:07 +03:00
|
|
|
$user2 = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->assertTrue($user === $user2);
|
2009-01-08 14:23:24 +03:00
|
|
|
|
2009-01-09 19:25:06 +03:00
|
|
|
// Add a phonenumber
|
2009-01-08 14:23:24 +03:00
|
|
|
$ph = new CmsPhonenumber;
|
|
|
|
$ph->phonenumber = "12345";
|
2009-01-09 19:25:06 +03:00
|
|
|
$user->addPhonenumber($ph);
|
2009-03-28 23:59:07 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->assertTrue($this->_em->contains($ph));
|
|
|
|
$this->assertTrue($this->_em->contains($user));
|
2009-02-06 20:16:39 +03:00
|
|
|
//$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
|
2009-01-08 14:23:24 +03:00
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
// Update name
|
2009-01-09 19:25:06 +03:00
|
|
|
$user->name = 'guilherme';
|
2009-03-28 23:59:07 +03:00
|
|
|
$this->_em->flush();
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->assertEquals('guilherme', $user->name);
|
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
// Add another phonenumber
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "6789";
|
|
|
|
$user->addPhonenumber($ph2);
|
2009-03-28 23:59:07 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->assertTrue($this->_em->contains($ph2));
|
2009-01-29 20:00:44 +03:00
|
|
|
|
2009-01-09 19:25:06 +03:00
|
|
|
// Delete
|
2009-07-19 20:54:53 +04: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 23:59:07 +03:00
|
|
|
$this->_em->flush();
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($user));
|
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
|
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
|
2009-01-09 19:25:06 +03:00
|
|
|
}
|
2009-01-08 14:23:24 +03:00
|
|
|
|
2009-02-18 10:59:11 +03:00
|
|
|
public function testOneToManyAssociationModification()
|
|
|
|
{
|
2009-02-02 14:55:50 +03:00
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-01-12 16:34:41 +03:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
$ph1 = new CmsPhonenumber;
|
|
|
|
$ph1->phonenumber = "0301234";
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "987654321";
|
|
|
|
|
|
|
|
$user->addPhonenumber($ph1);
|
|
|
|
$user->addPhonenumber($ph2);
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
//$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
// Remove the first element from the collection
|
|
|
|
unset($user->phonenumbers[0]);
|
|
|
|
$ph1->user = null; // owning side!
|
2009-01-12 16:34:41 +03:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($user->phonenumbers));
|
|
|
|
$this->assertNull($ph1->user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicOneToOne()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-01-12 16:34:41 +03:00
|
|
|
|
2009-02-02 14:55:50 +03: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 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-01-12 16:34:41 +03:00
|
|
|
$this->_em->flush();
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
// Check that the foreign key has been set
|
|
|
|
$userId = $this->_em->getConnection()->execute("SELECT user_id FROM cms_addresses WHERE id=?",
|
|
|
|
array($address->id))->fetchColumn();
|
|
|
|
$this->assertTrue(is_numeric($userId));
|
|
|
|
}
|
2009-02-06 20:16:39 +03:00
|
|
|
|
|
|
|
public function testBasicManyToMany()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$group = new CmsGroup;
|
|
|
|
$group->name = 'Developers';
|
|
|
|
|
|
|
|
$user->groups[] = $group;
|
|
|
|
$group->users[] = $user;
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-02-06 20:16:39 +03:00
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
unset($group->users[0]); // inverse side
|
|
|
|
unset($user->groups[0]); // owning side!
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
// Check that the link in the association table has been deleted
|
|
|
|
$count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
|
|
|
|
array())->fetchColumn();
|
|
|
|
$this->assertEquals(0, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManyToManyCollectionClearing()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
for ($i=0; $i<10; ++$i) {
|
|
|
|
$group = new CmsGroup;
|
|
|
|
$group->name = 'Developers_' . $i;
|
|
|
|
$user->groups[] = $group;
|
|
|
|
$group->users[] = $user;
|
|
|
|
}
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-02-06 20:16:39 +03:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_em->flush();
|
2009-07-19 20:54:53 +04:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
// Check that there are indeed 10 links in the association table
|
|
|
|
$count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
|
|
|
|
array())->fetchColumn();
|
|
|
|
$this->assertEquals(10, $count);
|
2009-02-06 20:16:39 +03:00
|
|
|
|
|
|
|
//$user->groups->clear();
|
|
|
|
unset($user->groups);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
// Check that the links in the association table have been deleted
|
|
|
|
$count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
|
|
|
|
array())->fetchColumn();
|
|
|
|
$this->assertEquals(0, $count);
|
|
|
|
}
|
2009-03-28 23:59:07 +03:00
|
|
|
|
|
|
|
public function testBasicQuery()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-03-28 23:59:07 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");
|
|
|
|
|
|
|
|
$users = $query->getResultList();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $users->count());
|
|
|
|
$this->assertEquals('Guilherme', $users[0]->name);
|
|
|
|
$this->assertEquals('gblanco', $users[0]->username);
|
|
|
|
$this->assertEquals('developer', $users[0]->status);
|
2009-05-13 19:19:27 +04:00
|
|
|
//$this->assertNull($users[0]->phonenumbers);
|
|
|
|
//$this->assertNull($users[0]->articles);
|
2009-03-30 23:43:05 +04:00
|
|
|
|
|
|
|
$usersArray = $query->getResultArray();
|
|
|
|
|
|
|
|
$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 23:59:07 +03:00
|
|
|
}
|
|
|
|
|
2009-05-07 17:54:01 +04:00
|
|
|
public function testBasicOneToManyInnerJoin()
|
2009-03-28 23:59:07 +03:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-03-28 23:59:07 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p");
|
|
|
|
|
|
|
|
$users = $query->getResultList();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $users->count());
|
|
|
|
}
|
|
|
|
|
2009-05-07 17:54:01 +04:00
|
|
|
public function testBasicOneToManyLeftJoin()
|
2009-03-28 23:59:07 +03:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-03-28 23:59:07 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select u,p from Doctrine\Tests\Models\CMS\CmsUser u left join u.phonenumbers p");
|
|
|
|
|
|
|
|
$users = $query->getResultList();
|
|
|
|
|
|
|
|
$this->assertEquals(1, $users->count());
|
|
|
|
$this->assertEquals('Guilherme', $users[0]->name);
|
|
|
|
$this->assertEquals('gblanco', $users[0]->username);
|
|
|
|
$this->assertEquals('developer', $users[0]->status);
|
|
|
|
$this->assertTrue($users[0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
|
|
|
|
$this->assertEquals(0, $users[0]->phonenumbers->count());
|
2009-05-13 19:19:27 +04:00
|
|
|
//$this->assertNull($users[0]->articles);
|
2009-03-28 23:59:07 +03:00
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
|
|
|
|
public function testBasicManyToManyJoin()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$group1 = new CmsGroup;
|
|
|
|
$group1->setName('Doctrine Developers');
|
|
|
|
|
|
|
|
$user->addGroup($group1);
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($user);
|
2009-05-07 17:54:01 +04:00
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $this->_em->getUnitOfWork()->size());
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select u, g from Doctrine\Tests\Models\CMS\CmsUser u join u.groups g");
|
|
|
|
|
|
|
|
$result = $query->getResultList();
|
|
|
|
|
|
|
|
$this->assertEquals(2, $this->_em->getUnitOfWork()->size());
|
|
|
|
$this->assertTrue($result[0] instanceof CmsUser);
|
|
|
|
$this->assertEquals('Guilherme', $result[0]->name);
|
|
|
|
$this->assertEquals(1, $result[0]->getGroups()->count());
|
|
|
|
$groups = $result[0]->getGroups();
|
|
|
|
$this->assertEquals('Doctrine Developers', $groups[0]->getName());
|
2009-05-07 20:36:27 +04:00
|
|
|
|
|
|
|
$this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($result[0]));
|
|
|
|
$this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($groups[0]));
|
|
|
|
|
|
|
|
$this->assertTrue($groups instanceof \Doctrine\ORM\PersistentCollection);
|
|
|
|
$this->assertTrue($groups[0]->getUsers() instanceof \Doctrine\ORM\PersistentCollection);
|
|
|
|
|
|
|
|
$groups[0]->getUsers()->clear();
|
|
|
|
$groups->clear();
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select u, g from Doctrine\Tests\Models\CMS\CmsUser u inner join u.groups g");
|
|
|
|
$this->assertEquals(0, count($query->getResultList()));
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
2009-02-18 10:59:11 +03:00
|
|
|
}
|