From b2951691e2c476a2c87212fdf9d0b8deec88d390 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 28 Jul 2011 10:46:38 +0200 Subject: [PATCH] [DDC-1301] Added tests for fetch="EXTRA_LAZY" count() on a "legacy" database --- .../Tests/Models/Legacy/LegacyArticle.php | 33 ++++ .../Tests/Models/Legacy/LegacyCar.php | 41 +++++ .../Tests/Models/Legacy/LegacyUser.php | 80 ++++++++++ .../Models/Legacy/LegacyUserReference.php | 65 ++++++++ .../ORM/Functional/Ticket/DDC1301Test.php | 148 ++++++++++++++++++ .../Doctrine/Tests/OrmFunctionalTestCase.php | 12 ++ 6 files changed, 379 insertions(+) create mode 100644 tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php create mode 100644 tests/Doctrine/Tests/Models/Legacy/LegacyCar.php create mode 100644 tests/Doctrine/Tests/Models/Legacy/LegacyUser.php create mode 100644 tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php diff --git a/tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php b/tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php new file mode 100644 index 000000000..e373c337d --- /dev/null +++ b/tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php @@ -0,0 +1,33 @@ +user = $author; + } +} diff --git a/tests/Doctrine/Tests/Models/Legacy/LegacyCar.php b/tests/Doctrine/Tests/Models/Legacy/LegacyCar.php new file mode 100644 index 000000000..a27d3c34c --- /dev/null +++ b/tests/Doctrine/Tests/Models/Legacy/LegacyCar.php @@ -0,0 +1,41 @@ +description; + } + + public function addUser(LegacyUser $user) { + $this->users[] = $user; + } + + public function getUsers() { + return $this->users; + } +} diff --git a/tests/Doctrine/Tests/Models/Legacy/LegacyUser.php b/tests/Doctrine/Tests/Models/Legacy/LegacyUser.php new file mode 100644 index 000000000..fb06263a2 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Legacy/LegacyUser.php @@ -0,0 +1,80 @@ +articles = new ArrayCollection; + $this->references = new ArrayCollection; + $this->cars = new ArrayCollection; + } + + public function getId() { + return $this->id; + } + + public function getUsername() { + return $this->username; + } + + public function addArticle(LegacyArticle $article) { + $this->articles[] = $article; + $article->setAuthor($this); + } + + public function addReference($reference) + { + $this->references[] = $reference; + } + + public function references() + { + return $this->references; + } + + public function addCar(LegacyCar $car) { + $this->cars[] = $car; + $car->addUser($this); + } + + public function getCars() { + return $this->cars; + } +} diff --git a/tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php b/tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php new file mode 100644 index 000000000..8b1ee9407 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php @@ -0,0 +1,65 @@ +addReference($this); + $target->addReference($this); + + $this->source = $source; + $this->target = $target; + $this->description = $description; + $this->created = new \DateTime("now"); + } + + public function source() + { + return $this->source; + } + + public function target() + { + return $this->target; + } + + public function setDescription($desc) + { + $this->description = $desc; + } + + public function getDescription() + { + return $this->description; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php new file mode 100644 index 000000000..f1dd02498 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php @@ -0,0 +1,148 @@ +useModelSet('legacy'); + parent::setUp(); + + $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser'); + $class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY; + $class->associationMappings['references']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY; + $class->associationMappings['cars']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY; + + $this->loadFixture(); + } + + public function tearDown() + { + parent::tearDown(); + + $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser'); + $class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_LAZY; + $class->associationMappings['references']['fetch'] = ClassMetadataInfo::FETCH_LAZY; + $class->associationMappings['cars']['fetch'] = ClassMetadataInfo::FETCH_LAZY; + } + + public function testCountNotInitializesLegacyCollection() + { + $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId); + $queryCount = $this->getCurrentQueryCount(); + + $this->assertFalse($user->articles->isInitialized()); + $this->assertEquals(2, count($user->articles)); + $this->assertFalse($user->articles->isInitialized()); + + foreach ($user->articles AS $article) { } + + $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration."); + } + + public function testCountNotInitializesLegacyCollectionWithForeignIdentifier() + { + $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId); + $queryCount = $this->getCurrentQueryCount(); + + $this->assertFalse($user->references->isInitialized()); + $this->assertEquals(2, count($user->references)); + $this->assertFalse($user->references->isInitialized()); + + foreach ($user->references AS $reference) { } + + $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration."); + } + + public function testCountNotInitializesLegacyManyToManyCollection() + { + $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId); + $queryCount = $this->getCurrentQueryCount(); + + $this->assertFalse($user->cars->isInitialized()); + $this->assertEquals(3, count($user->cars)); + $this->assertFalse($user->cars->isInitialized()); + + foreach ($user->cars AS $reference) { } + + $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration."); + } + + public function loadFixture() + { + $user1 = new \Doctrine\Tests\Models\Legacy\LegacyUser(); + $user1->username = "beberlei"; + $user1->name = "Benjamin"; + $user1->status = "active"; + + $user2 = new \Doctrine\Tests\Models\Legacy\LegacyUser(); + $user2->username = "jwage"; + $user2->name = "Jonathan"; + $user2->status = "active"; + + $user3 = new \Doctrine\Tests\Models\Legacy\LegacyUser(); + $user3->username = "romanb"; + $user3->name = "Roman"; + $user3->status = "active"; + + $this->_em->persist($user1); + $this->_em->persist($user2); + $this->_em->persist($user3); + + $article1 = new \Doctrine\Tests\Models\Legacy\LegacyArticle(); + $article1->topic = "Test"; + $article1->text = "Test"; + $article1->setAuthor($user1); + + $article2 = new \Doctrine\Tests\Models\Legacy\LegacyArticle(); + $article2->topic = "Test"; + $article2->text = "Test"; + $article2->setAuthor($user1); + + $this->_em->persist($article1); + $this->_em->persist($article2); + + $car1 = new \Doctrine\Tests\Models\Legacy\LegacyCar(); + $car1->description = "Test1"; + + $car2 = new \Doctrine\Tests\Models\Legacy\LegacyCar(); + $car2->description = "Test2"; + + $car3 = new \Doctrine\Tests\Models\Legacy\LegacyCar(); + $car3->description = "Test3"; + + $user1->addCar($car1); + $user1->addCar($car2); + $user1->addCar($car3); + + $user2->addCar($car1); + $user3->addCar($car1); + + $this->_em->persist($car1); + $this->_em->persist($car2); + $this->_em->persist($car3); + + $this->_em->flush(); + + $detail1 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user2, "foo"); + $detail2 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user3, "bar"); + + $this->_em->persist($detail1); + $this->_em->persist($detail2); + + $this->_em->flush(); + $this->_em->clear(); + + $this->userId = $user1->getId(); + } +} diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 1f8032aeb..66d20b38f 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -104,6 +104,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\StockExchange\Stock', 'Doctrine\Tests\Models\StockExchange\Market', ), + 'legacy' => array( + 'Doctrine\Tests\Models\Legacy\LegacyUser', + 'Doctrine\Tests\Models\Legacy\LegacyUserReference', + 'Doctrine\Tests\Models\Legacy\LegacyArticle', + 'Doctrine\Tests\Models\Legacy\LegacyCar', + ), ); protected function useModelSet($setName) @@ -202,6 +208,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $conn->executeUpdate('DELETE FROM exchange_stocks'); $conn->executeUpdate('DELETE FROM exchange_markets'); } + if (isset($this->_usedModelSets['legacy'])) { + $conn->executeUpdate('DELETE FROM legacy_articles'); + $conn->executeUpdate('DELETE FROM legacy_cars'); + $conn->executeUpdate('DELETE FROM legacy_users'); + $conn->executeUpdate('DELETE FROM legacy_users_reference'); + } $this->_em->clear(); }