DDC1301Test.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  4. require_once __DIR__ . '/../../../TestInit.php';
  5. /**
  6. * @author asm89
  7. */
  8. class DDC1301Test extends \Doctrine\Tests\OrmFunctionalTestCase
  9. {
  10. private $userId;
  11. public function setUp()
  12. {
  13. $this->useModelSet('legacy');
  14. parent::setUp();
  15. $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser');
  16. $class->associationMappings['_articles']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
  17. $class->associationMappings['_references']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
  18. $class->associationMappings['_cars']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
  19. $this->loadFixture();
  20. }
  21. public function tearDown()
  22. {
  23. parent::tearDown();
  24. $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser');
  25. $class->associationMappings['_articles']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
  26. $class->associationMappings['_references']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
  27. $class->associationMappings['_cars']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
  28. }
  29. public function testCountNotInitializesLegacyCollection()
  30. {
  31. $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
  32. $queryCount = $this->getCurrentQueryCount();
  33. $this->assertFalse($user->_articles->isInitialized());
  34. $this->assertEquals(2, count($user->_articles));
  35. $this->assertFalse($user->_articles->isInitialized());
  36. foreach ($user->_articles AS $article) { }
  37. $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
  38. }
  39. public function testCountNotInitializesLegacyCollectionWithForeignIdentifier()
  40. {
  41. $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
  42. $queryCount = $this->getCurrentQueryCount();
  43. $this->assertFalse($user->_references->isInitialized());
  44. $this->assertEquals(2, count($user->_references));
  45. $this->assertFalse($user->_references->isInitialized());
  46. foreach ($user->_references AS $reference) { }
  47. $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
  48. }
  49. public function testCountNotInitializesLegacyManyToManyCollection()
  50. {
  51. $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
  52. $queryCount = $this->getCurrentQueryCount();
  53. $this->assertFalse($user->_cars->isInitialized());
  54. $this->assertEquals(3, count($user->_cars));
  55. $this->assertFalse($user->_cars->isInitialized());
  56. foreach ($user->_cars AS $reference) { }
  57. $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
  58. }
  59. public function loadFixture()
  60. {
  61. $user1 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
  62. $user1->_username = "beberlei";
  63. $user1->_name = "Benjamin";
  64. $user1->_status = "active";
  65. $user2 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
  66. $user2->_username = "jwage";
  67. $user2->_name = "Jonathan";
  68. $user2->_status = "active";
  69. $user3 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
  70. $user3->_username = "romanb";
  71. $user3->_name = "Roman";
  72. $user3->_status = "active";
  73. $this->_em->persist($user1);
  74. $this->_em->persist($user2);
  75. $this->_em->persist($user3);
  76. $article1 = new \Doctrine\Tests\Models\Legacy\LegacyArticle();
  77. $article1->_topic = "Test";
  78. $article1->_text = "Test";
  79. $article1->setAuthor($user1);
  80. $article2 = new \Doctrine\Tests\Models\Legacy\LegacyArticle();
  81. $article2->_topic = "Test";
  82. $article2->_text = "Test";
  83. $article2->setAuthor($user1);
  84. $this->_em->persist($article1);
  85. $this->_em->persist($article2);
  86. $car1 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
  87. $car1->_description = "Test1";
  88. $car2 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
  89. $car2->_description = "Test2";
  90. $car3 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
  91. $car3->_description = "Test3";
  92. $user1->addCar($car1);
  93. $user1->addCar($car2);
  94. $user1->addCar($car3);
  95. $user2->addCar($car1);
  96. $user3->addCar($car1);
  97. $this->_em->persist($car1);
  98. $this->_em->persist($car2);
  99. $this->_em->persist($car3);
  100. $this->_em->flush();
  101. $detail1 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user2, "foo");
  102. $detail2 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user3, "bar");
  103. $this->_em->persist($detail1);
  104. $this->_em->persist($detail2);
  105. $this->_em->flush();
  106. $this->_em->clear();
  107. $this->userId = $user1->getId();
  108. }
  109. }