/tests/Doctrine/Tests/ORM/Functional/OneToManySelfReferentialAssociationTest.php

https://github.com/jaikdean/doctrine2 · PHP · 127 lines · 100 code · 24 blank · 3 comment · 0 complexity · 2e8f173e0959ca8196f645448339a413 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Tests\ORM\Functional;
  4. use Doctrine\ORM\Mapping\FetchMode;
  5. use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
  6. use Doctrine\Tests\OrmFunctionalTestCase;
  7. use function strstr;
  8. /**
  9. * Tests a bidirectional one-to-one association mapping (without inheritance).
  10. */
  11. class OneToManySelfReferentialAssociationTest extends OrmFunctionalTestCase
  12. {
  13. private $parent;
  14. private $firstChild;
  15. private $secondChild;
  16. protected function setUp() : void
  17. {
  18. $this->useModelSet('ecommerce');
  19. parent::setUp();
  20. $this->parent = new ECommerceCategory();
  21. $this->parent->setName('Programming languages books');
  22. $this->firstChild = new ECommerceCategory();
  23. $this->firstChild->setName('Java books');
  24. $this->secondChild = new ECommerceCategory();
  25. $this->secondChild->setName('Php books');
  26. }
  27. public function testSavesAOneToManyAssociationWithCascadeSaveSet() : void
  28. {
  29. $this->parent->addChild($this->firstChild);
  30. $this->parent->addChild($this->secondChild);
  31. $this->em->persist($this->parent);
  32. $this->em->flush();
  33. self::assertForeignKeyIs($this->parent->getId(), $this->firstChild);
  34. self::assertForeignKeyIs($this->parent->getId(), $this->secondChild);
  35. }
  36. public function testSavesAnEmptyCollection() : void
  37. {
  38. $this->em->persist($this->parent);
  39. $this->em->flush();
  40. self::assertCount(0, $this->parent->getChildren());
  41. }
  42. public function testDoesNotSaveAnInverseSideSet() : void
  43. {
  44. $this->parent->brokenAddChild($this->firstChild);
  45. $this->em->persist($this->parent);
  46. $this->em->flush();
  47. self::assertForeignKeyIs(null, $this->firstChild);
  48. }
  49. public function testRemovesOneToManyAssociation() : void
  50. {
  51. $this->parent->addChild($this->firstChild);
  52. $this->parent->addChild($this->secondChild);
  53. $this->em->persist($this->parent);
  54. $this->parent->removeChild($this->firstChild);
  55. $this->em->flush();
  56. self::assertForeignKeyIs(null, $this->firstChild);
  57. self::assertForeignKeyIs($this->parent->getId(), $this->secondChild);
  58. }
  59. public function testEagerLoadsOneToManyAssociation() : void
  60. {
  61. $this->createFixture();
  62. $query = $this->em->createQuery('select c1, c2 from Doctrine\Tests\Models\ECommerce\ECommerceCategory c1 join c1.children c2');
  63. $result = $query->getResult();
  64. self::assertCount(1, $result);
  65. $parent = $result[0];
  66. $children = $parent->getChildren();
  67. self::assertInstanceOf(ECommerceCategory::class, $children[0]);
  68. self::assertSame($parent, $children[0]->getParent());
  69. self::assertEquals(' books', strstr($children[0]->getName(), ' books'));
  70. self::assertInstanceOf(ECommerceCategory::class, $children[1]);
  71. self::assertSame($parent, $children[1]->getParent());
  72. self::assertEquals(' books', strstr($children[1]->getName(), ' books'));
  73. }
  74. public function testLazyLoadsOneToManyAssociation() : void
  75. {
  76. $this->createFixture();
  77. $metadata = $this->em->getClassMetadata(ECommerceCategory::class);
  78. $metadata->getProperty('children')->setFetchMode(FetchMode::LAZY);
  79. $query = $this->em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
  80. $result = $query->getResult();
  81. $parent = $result[0];
  82. $children = $parent->getChildren();
  83. self::assertInstanceOf(ECommerceCategory::class, $children[0]);
  84. self::assertSame($parent, $children[0]->getParent());
  85. self::assertEquals(' books', strstr($children[0]->getName(), ' books'));
  86. self::assertInstanceOf(ECommerceCategory::class, $children[1]);
  87. self::assertSame($parent, $children[1]->getParent());
  88. self::assertEquals(' books', strstr($children[1]->getName(), ' books'));
  89. }
  90. private function createFixture()
  91. {
  92. $this->parent->addChild($this->firstChild);
  93. $this->parent->addChild($this->secondChild);
  94. $this->em->persist($this->parent);
  95. $this->em->flush();
  96. $this->em->clear();
  97. }
  98. public function assertForeignKeyIs($value, ECommerceCategory $child)
  99. {
  100. $foreignKey = $this->em->getConnection()->executeQuery('SELECT parent_id FROM ecommerce_categories WHERE id=?', [$child->getId()])->fetchColumn();
  101. self::assertEquals($value, $foreignKey);
  102. }
  103. }