CompositePrimaryKeyTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\Navigation\NavCountry;
  4. use Doctrine\Tests\Models\Navigation\NavPointOfInterest;
  5. use Doctrine\Tests\Models\Navigation\NavTour;
  6. use Doctrine\Tests\Models\Navigation\NavPhotos;
  7. use Doctrine\Tests\Models\Navigation\NavUser;
  8. require_once __DIR__ . '/../../TestInit.php';
  9. class CompositePrimaryKeyTest extends \Doctrine\Tests\OrmFunctionalTestCase
  10. {
  11. public function setUp()
  12. {
  13. $this->useModelSet('navigation');
  14. parent::setUp();
  15. }
  16. public function putGermanysBrandenburderTor()
  17. {
  18. $country = new NavCountry("Germany");
  19. $this->_em->persist($country);
  20. $poi = new NavPointOfInterest(100, 200, "Brandenburger Tor", $country);
  21. $this->_em->persist($poi);
  22. $this->_em->flush();
  23. $this->_em->clear();
  24. }
  25. public function putTripAroundEurope()
  26. {
  27. $poi = $this->_em->find('Doctrine\Tests\Models\Navigation\NavPointOfInterest', array('lat' => 100, 'long' => 200));
  28. $tour = new NavTour("Trip around Europe");
  29. $tour->addPointOfInterest($poi);
  30. $this->_em->persist($tour);
  31. $this->_em->flush();
  32. $this->_em->clear();
  33. return $tour;
  34. }
  35. public function testPersistCompositePkEntity()
  36. {
  37. $this->putGermanysBrandenburderTor();
  38. $poi = $this->_em->find('Doctrine\Tests\Models\Navigation\NavPointOfInterest', array('lat' => 100, 'long' => 200));
  39. $this->assertInstanceOf('Doctrine\Tests\Models\Navigation\NavPointOfInterest', $poi);
  40. $this->assertEquals(100, $poi->getLat());
  41. $this->assertEquals(200, $poi->getLong());
  42. $this->assertEquals('Brandenburger Tor', $poi->getName());
  43. }
  44. /**
  45. * @group DDC-1651
  46. */
  47. public function testSetParameterCompositeKeyObject()
  48. {
  49. $this->putGermanysBrandenburderTor();
  50. $poi = $this->_em->find('Doctrine\Tests\Models\Navigation\NavPointOfInterest', array('lat' => 100, 'long' => 200));
  51. $photo = new NavPhotos($poi, "asdf");
  52. $this->_em->persist($photo);
  53. $this->_em->flush();
  54. $this->_em->clear();
  55. $dql = 'SELECT t FROM Doctrine\Tests\Models\Navigation\NavPhotos t WHERE t.poi = ?1';
  56. $this->setExpectedException('Doctrine\ORM\Query\QueryException', 'A single-valued association path expression to an entity with a composite primary key is not supported.');
  57. $sql = $this->_em->createQuery($dql)->getSQL();
  58. }
  59. public function testManyToManyCompositeRelation()
  60. {
  61. $this->putGermanysBrandenburderTor();
  62. $tour = $this->putTripAroundEurope();
  63. $tour = $this->_em->find('Doctrine\Tests\Models\Navigation\NavTour', $tour->getId());
  64. $this->assertEquals(1, count($tour->getPointOfInterests()));
  65. }
  66. public function testCompositeDqlEagerFetching()
  67. {
  68. $this->putGermanysBrandenburderTor();
  69. $this->putTripAroundEurope();
  70. $dql = 'SELECT t, p, c FROM Doctrine\Tests\Models\Navigation\NavTour t ' .
  71. 'INNER JOIN t.pois p INNER JOIN p.country c';
  72. $tours = $this->_em->createQuery($dql)->getResult();
  73. $this->assertEquals(1, count($tours));
  74. $pois = $tours[0]->getPointOfInterests();
  75. $this->assertEquals(1, count($pois));
  76. $this->assertEquals('Brandenburger Tor', $pois[0]->getName());
  77. }
  78. public function testCompositeCollectionMemberExpression()
  79. {
  80. $this->markTestSkipped('How to test this?');
  81. $this->putGermanysBrandenburderTor();
  82. $this->putTripAroundEurope();
  83. $dql = 'SELECT t FROM Doctrine\Tests\Models\Navigation\NavTour t, Doctrine\Tests\Models\Navigation\NavPointOfInterest p ' .
  84. 'WHERE p MEMBER OF t.pois';
  85. $tours = $this->_em->createQuery($dql)
  86. ->getResult();
  87. $this->assertEquals(1, count($tours));
  88. }
  89. public function testSpecifiyUnknownIdentifierPrimaryKeyFails()
  90. {
  91. $this->setExpectedException('Doctrine\ORM\ORMException', 'The identifier long is missing for a query of Doctrine\Tests\Models\Navigation\NavPointOfInterest');
  92. $poi = $this->_em->find('Doctrine\Tests\Models\Navigation\NavPointOfInterest', array('key1' => 100));
  93. }
  94. /**
  95. * @group DDC-1939
  96. */
  97. public function testDeleteCompositePersistentCollection()
  98. {
  99. $this->putGermanysBrandenburderTor();
  100. $poi = $this->_em->find('Doctrine\Tests\Models\Navigation\NavPointOfInterest', array('lat' => 100, 'long' => 200));
  101. $poi->addVisitor(new NavUser("test1"));
  102. $poi->addVisitor(new NavUser("test2"));
  103. $this->_em->flush();
  104. $poi->getVisitors()->clear();
  105. $this->_em->flush();
  106. $this->_em->clear();
  107. $poi = $this->_em->find('Doctrine\Tests\Models\Navigation\NavPointOfInterest', array('lat' => 100, 'long' => 200));
  108. $this->assertEquals(0, count($poi->getVisitors()));
  109. }
  110. }