1
0
mirror of synced 2025-01-18 06:21:40 +03:00

Added tests for ManyToMany associations and lazy collection to CTI entity

This commit is contained in:
Alexander 2011-12-22 00:19:35 +01:00
parent 62be27b295
commit 223c47069e
3 changed files with 78 additions and 13 deletions

View File

@ -19,13 +19,13 @@ class CompanyFlexContract extends CompanyContract
private $pricePerHour = 0;
/**
* @ManyToMany(targetEntity="CompanyManager", inversedBy="managedContracts")
* @ManyToMany(targetEntity="CompanyManager", inversedBy="managedContracts", fetch="EXTRA_LAZY")
* @JoinTable(name="company_contract_managers",
* joinColumns={@JoinColumn(name="contract_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@JoinColumn(name="employee_id", referencedColumnName="id")}
* )
*/
private $managers;
public $managers;
public function calculatePrice()
{

View File

@ -34,7 +34,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $userId, $userId2, $articleId, $articleId2;
private $groupId, $groupId2;
private $managerId, $contractId1, $contractId2;
private $managerId, $managerId2, $contractId1, $contractId2;
public function setUp()
{
@ -553,11 +553,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals(2, count($this->_em->createQuery("SELECT cm FROM Doctrine\Tests\Models\Company\CompanyManager cm")->getResult()));
// Enable the filter
$conf = $this->_em->getConfiguration();
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
$this->_em->getFilters()
->enable("person_name")
->setParameter("name", "Guilh%", DBALType::STRING);
$this->usePersonNameFilter('Guilh%');
$managers = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyManager')->findAll();
$this->assertEquals(1, count($managers));
@ -573,11 +569,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals(3, count($this->_em->createQuery("SELECT cp FROM Doctrine\Tests\Models\Company\CompanyPerson cp")->getResult()));
// Enable the filter
$conf = $this->_em->getConfiguration();
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
$this->_em->getFilters()
->enable("person_name")
->setParameter("name", "Guilh%", DBALType::STRING);
$this->usePersonNameFilter('Guilh%');
$persons = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyPerson')->findAll();
$this->assertEquals(1, count($persons));
@ -662,12 +654,21 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
$manager->setDepartment('Doctrine');
$manager->setTitle('Filterer');
$manager2 = new CompanyManager;
$manager2->setName('Benjamin');
$manager2->setSalary(1337);
$manager2->setDepartment('Doctrine');
$manager2->setTitle('Maintainer');
$contract1->addManager($manager);
$contract2->addManager($manager);
$contract3->addManager($manager);
$contract4->addManager($manager);
$contract1->addManager($manager2);
$this->_em->persist($manager);
$this->_em->persist($manager2);
$this->_em->persist($contract1);
$this->_em->persist($contract2);
$this->_em->persist($contract3);
@ -676,6 +677,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->clear();
$this->managerId = $manager->getId();
$this->managerId2 = $manager2->getId();
$this->contractId1 = $contract1->getId();
$this->contractId2 = $contract2->getId();
}
@ -740,6 +742,68 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertFalse($manager->managedContracts->isInitialized());
$this->assertEquals(2, count($manager->managedContracts->slice(0, 10)));
}
private function usePersonNameFilter($name)
{
// Enable the filter
$conf = $this->_em->getConfiguration();
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
$this->_em->getFilters()
->enable("person_name")
->setParameter("name", $name, DBALType::STRING);
}
public function testManyToMany_ExtraLazyCountWithFilterOnCTI()
{
$this->loadCompanySingleTableInheritanceFixtureData();
$contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexUltraContract', $this->contractId1);
$this->assertFalse($contract->managers->isInitialized());
$this->assertEquals(2, count($contract->managers));
// Enable the filter
$this->usePersonNameFilter('Benjamin');
$this->assertFalse($contract->managers->isInitialized());
$this->assertEquals(1, count($contract->managers));
}
public function testManyToMany_ExtraLazyContainsWithFilterOnCTI()
{
$this->loadCompanySingleTableInheritanceFixtureData();
$contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexUltraContract', $this->contractId1);
$manager1 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId);
$manager2 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId2);
$this->assertFalse($contract->managers->isInitialized());
$this->assertTrue($contract->managers->contains($manager1));
$this->assertTrue($contract->managers->contains($manager2));
// Enable the filter
$this->usePersonNameFilter('Benjamin');
$this->assertFalse($contract->managers->isInitialized());
$this->assertFalse($contract->managers->contains($manager1));
$this->assertTrue($contract->managers->contains($manager2));
}
public function testManyToMany_ExtraLazySliceWithFilterOnCTI()
{
$this->loadCompanySingleTableInheritanceFixtureData();
$contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexUltraContract', $this->contractId1);
$this->assertFalse($contract->managers->isInitialized());
$this->assertEquals(2, count($contract->managers->slice(0, 10)));
// Enable the filter
$this->usePersonNameFilter('Benjamin');
$this->assertFalse($contract->managers->isInitialized());
$this->assertEquals(1, count($contract->managers->slice(0, 10)));
}
}
class MySoftDeleteFilter extends SQLFilter

View File

@ -158,6 +158,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
if (isset($this->_usedModelSets['company'])) {
$conn->executeUpdate('DELETE FROM company_contract_employees');
$conn->executeUpdate('DELETE FROM company_contract_managers');
$conn->executeUpdate('DELETE FROM company_contracts');
$conn->executeUpdate('DELETE FROM company_persons_friends');
$conn->executeUpdate('DELETE FROM company_managers');