OneToManyUnidirectionalAssociationTest.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. $exceptionThrown = false;
  60. try {
  61. // exception depending on the underyling Database Driver
  62. $this->_em->flush();
  63. } catch(\Exception $e) {
  64. $exceptionThrown = true;
  65. }
  66. $this->assertTrue($exceptionThrown, "The underlying database driver throws an exception.");
  67. }
  68. }