From c3c174512a6732c86bf2bb85af9293a6eb8c5bc2 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 22 Dec 2011 21:10:13 +0100 Subject: [PATCH] Added tests for OneToMany associations and lazy collection to CTI entity --- .../Tests/Models/Company/CompanyAuction.php | 2 +- .../Models/Company/CompanyOrganization.php | 6 +- .../Tests/ORM/Functional/SQLFilterTest.php | 100 ++++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/tests/Doctrine/Tests/Models/Company/CompanyAuction.php b/tests/Doctrine/Tests/Models/Company/CompanyAuction.php index 5dc72e8c1..5743122bc 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyAuction.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyAuction.php @@ -14,4 +14,4 @@ class CompanyAuction extends CompanyEvent { public function getData() { return $this->data; } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php b/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php index 19463206b..ca9941062 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php @@ -11,9 +11,9 @@ class CompanyOrganization { private $id; /** - * @OneToMany(targetEntity="CompanyEvent", mappedBy="organization", cascade={"persist"}) + * @OneToMany(targetEntity="CompanyEvent", mappedBy="organization", cascade={"persist"}, fetch="EXTRA_LAZY") */ - private $events; + public $events; public function getId() { return $this->id; @@ -41,4 +41,4 @@ class CompanyOrganization { public function setMainEvent($event) { $this->mainevent = $event; } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php index 7d3affd0e..31ebb2d46 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php @@ -19,6 +19,8 @@ use Doctrine\Tests\Models\CMS\CmsComment; use Doctrine\Tests\Models\Company\CompanyPerson; use Doctrine\Tests\Models\Company\CompanyManager; use Doctrine\Tests\Models\Company\CompanyEmployee; +use Doctrine\Tests\Models\Company\CompanyOrganization; +use Doctrine\Tests\Models\Company\CompanyAuction; use Doctrine\Tests\Models\Company\CompanyFlexContract; use Doctrine\Tests\Models\Company\CompanyFlexUltraContract; @@ -35,6 +37,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase private $userId, $userId2, $articleId, $articleId2; private $groupId, $groupId2; private $managerId, $managerId2, $contractId1, $contractId2; + private $organizationId, $eventId1, $eventId2; public function setUp() { @@ -860,6 +863,91 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertFalse($manager->soldContracts->isInitialized()); $this->assertEquals(1, count($manager->soldContracts->slice(0, 10))); } + private function loadCompanyOrganizationEventJoinedSubclassFixtureData() + { + $organization = new CompanyOrganization; + + $event1 = new CompanyAuction; + $event1->setData('foo'); + + $event2 = new CompanyAuction; + $event2->setData('bar'); + + $organization->addEvent($event1); + $organization->addEvent($event2); + + $this->_em->persist($organization); + $this->_em->flush(); + $this->_em->clear(); + + $this->organizationId = $organization->getId(); + $this->eventId1 = $event1->getId(); + $this->eventId2 = $event2->getId(); + } + + private function useCompanyEventIdFilter() + { + // Enable the filter + $conf = $this->_em->getConfiguration(); + $conf->addFilter("event_id", "\Doctrine\Tests\ORM\Functional\CompanyEventFilter"); + $this->_em->getFilters() + ->enable("event_id") + ->setParameter("id", $this->eventId2); + } + + + public function testOneToMany_ExtraLazyCountWithFilterOnCTI() + { + $this->loadCompanyOrganizationEventJoinedSubclassFixtureData(); + + $organization = $this->_em->find('Doctrine\Tests\Models\Company\CompanyOrganization', $this->organizationId); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertEquals(2, count($organization->events)); + + // Enable the filter + $this->useCompanyEventIdFilter(); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertEquals(1, count($organization->events)); + } + + public function testOneToMany_ExtraLazyContainsWithFilterOnCTI() + { + $this->loadCompanyOrganizationEventJoinedSubclassFixtureData(); + + $organization = $this->_em->find('Doctrine\Tests\Models\Company\CompanyOrganization', $this->organizationId); + + $event1 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyEvent', $this->eventId1); + $event2 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyEvent', $this->eventId2); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertTrue($organization->events->contains($event1)); + $this->assertTrue($organization->events->contains($event2)); + + // Enable the filter + $this->useCompanyEventIdFilter(); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertFalse($organization->events->contains($event1)); + $this->assertTrue($organization->events->contains($event2)); + } + + public function testOneToMany_ExtraLazySliceWithFilterOnCTI() + { + $this->loadCompanyOrganizationEventJoinedSubclassFixtureData(); + + $organization = $this->_em->find('Doctrine\Tests\Models\Company\CompanyOrganization', $this->organizationId); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertEquals(2, count($organization->events->slice(0, 10))); + + // Enable the filter + $this->useCompanyEventIdFilter(); + + $this->assertFalse($organization->events->isInitialized()); + $this->assertEquals(1, count($organization->events->slice(0, 10))); + } } class MySoftDeleteFilter extends SQLFilter @@ -945,3 +1033,15 @@ class CompletedContractFilter extends SQLFilter return $targetTableAlias.'.completed = ' . $this->getParameter('completed'); } } + +class CompanyEventFilter extends SQLFilter +{ + public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias, $targetTable = '') + { + if ($targetEntity->name != "Doctrine\Tests\Models\Company\CompanyEvent") { + return ""; + } + + return $targetTableAlias.'.id = ' . $this->getParameter('id'); + } +}