OrderedCollectionTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\Routing\RoutingRoute;
  4. use Doctrine\Tests\Models\Routing\RoutingLocation;
  5. use Doctrine\Tests\Models\Routing\RoutingLeg;
  6. use Doctrine\Tests\Models\Routing\RoutingRouteBooking;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. class OrderedCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
  9. {
  10. protected $locations = array();
  11. public function setUp()
  12. {
  13. $this->useModelSet('routing');
  14. parent::setUp();
  15. $locations = array("Berlin", "Bonn", "Brasilia", "Atlanta");
  16. foreach ($locations AS $locationName) {
  17. $location = new RoutingLocation();
  18. $location->name = $locationName;
  19. $this->_em->persist($location);
  20. $this->locations[$locationName] = $location;
  21. }
  22. $this->_em->flush();
  23. }
  24. public function createPersistedRouteWithLegs()
  25. {
  26. $route = new RoutingRoute();
  27. $leg1 = new RoutingLeg();
  28. $leg1->fromLocation = $this->locations['Berlin'];
  29. $leg1->toLocation = $this->locations['Bonn'];
  30. $leg1->departureDate = new \DateTime("now");
  31. $leg1->arrivalDate = new \DateTime("now +5 hours");
  32. $leg2 = new RoutingLeg();
  33. $leg2->fromLocation = $this->locations['Bonn'];
  34. $leg2->toLocation = $this->locations['Brasilia'];
  35. $leg2->departureDate = new \DateTime("now +6 hours");
  36. $leg2->arrivalDate = new \DateTime("now +24 hours");
  37. $route->legs[] = $leg2;
  38. $route->legs[] = $leg1;
  39. $this->_em->persist($route);
  40. $this->_em->flush();
  41. $routeId = $route->id;
  42. $this->_em->clear();
  43. return $routeId;
  44. }
  45. public function testLazyManyToManyCollection_IsRetrievedWithOrderByClause()
  46. {
  47. $routeId = $this->createPersistedRouteWithLegs();
  48. $route = $this->_em->find('Doctrine\Tests\Models\Routing\RoutingRoute', $routeId);
  49. $this->assertEquals(2, count($route->legs));
  50. $this->assertEquals("Berlin", $route->legs[0]->fromLocation->getName());
  51. $this->assertEquals("Bonn", $route->legs[1]->fromLocation->getName());
  52. }
  53. public function testLazyOneToManyCollection_IsRetrievedWithOrderByClause()
  54. {
  55. $route = new RoutingRoute();
  56. $this->_em->persist($route);
  57. $this->_em->flush();
  58. $routeId = $route->id;
  59. $booking1 = new RoutingRouteBooking();
  60. $booking1->passengerName = "Guilherme";
  61. $booking2 = new RoutingRouteBooking();
  62. $booking2->passengerName = "Benjamin";
  63. $route->bookings[] = $booking1;
  64. $booking1->route = $route;
  65. $route->bookings[] = $booking2;
  66. $booking2->route = $route;
  67. $this->_em->persist($booking1);
  68. $this->_em->persist($booking2);
  69. $this->_em->flush();
  70. $this->_em->clear();
  71. $route = $this->_em->find('Doctrine\Tests\Models\Routing\RoutingRoute', $routeId);
  72. $this->assertEquals(2, count($route->bookings));
  73. $this->assertEquals('Benjamin', $route->bookings[0]->getPassengerName());
  74. $this->assertEquals('Guilherme', $route->bookings[1]->getPassengerName());
  75. }
  76. public function testOrderedResultFromDqlQuery()
  77. {
  78. $routeId = $this->createPersistedRouteWithLegs();
  79. $route = $this->_em->createQuery("SELECT r, l FROM Doctrine\Tests\Models\Routing\RoutingRoute r JOIN r.legs l WHERE r.id = ?1")
  80. ->setParameter(1, $routeId)
  81. ->getSingleResult();
  82. $this->assertEquals(2, count($route->legs));
  83. $this->assertEquals("Berlin", $route->legs[0]->fromLocation->getName());
  84. $this->assertEquals("Bonn", $route->legs[1]->fromLocation->getName());
  85. }
  86. }