OneToManyUnidirectionalAssociationTest.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * Tests a bidirectional one-to-one association mapping (without inheritance).
  9. */
  10. class OneToManyUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. protected $locations = array();
  13. public function setUp()
  14. {
  15. $this->useModelSet('routing');
  16. parent::setUp();
  17. $locations = array("Berlin", "Bonn", "Brasilia", "Atlanta");
  18. foreach ($locations AS $locationName) {
  19. $location = new RoutingLocation();
  20. $location->name = $locationName;
  21. $this->_em->persist($location);
  22. $this->locations[$locationName] = $location;
  23. }
  24. $this->_em->flush();
  25. }
  26. public function testPersistOwning_InverseCascade()
  27. {
  28. $leg = new RoutingLeg();
  29. $leg->fromLocation = $this->locations['Berlin'];
  30. $leg->toLocation = $this->locations['Bonn'];
  31. $leg->departureDate = new \DateTime("now");
  32. $leg->arrivalDate = new \DateTime("now +5 hours");
  33. $route = new RoutingRoute();
  34. $route->legs[] = $leg;
  35. $this->_em->persist($route);
  36. $this->_em->flush();
  37. $this->_em->clear();
  38. $routes = $this->_em->createQuery(
  39. "SELECT r, l, f, t FROM Doctrine\Tests\Models\Routing\RoutingRoute r ".
  40. "JOIN r.legs l JOIN l.fromLocation f JOIN l.toLocation t"
  41. )->getSingleResult();
  42. $this->assertEquals(1, count($routes->legs));
  43. $this->assertEquals("Berlin", $routes->legs[0]->fromLocation->name);
  44. $this->assertEquals("Bonn", $routes->legs[0]->toLocation->name);
  45. }
  46. public function testLegsAreUniqueToRoutes()
  47. {
  48. $leg = new RoutingLeg();
  49. $leg->fromLocation = $this->locations['Berlin'];
  50. $leg->toLocation = $this->locations['Bonn'];
  51. $leg->departureDate = new \DateTime("now");
  52. $leg->arrivalDate = new \DateTime("now +5 hours");
  53. $routeA = new RoutingRoute();
  54. $routeA->legs[] = $leg;
  55. $routeB = new RoutingRoute();
  56. $routeB->legs[] = $leg;
  57. $this->_em->persist($routeA);
  58. $this->_em->persist($routeB);
  59. $this->setExpectedException('Exception'); // depends on the underyling Database Driver
  60. $this->_em->flush(); // Exception
  61. }
  62. }