Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / OneToManyUnidirectionalAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\Routing\RoutingRoute;
6 use Doctrine\Tests\Models\Routing\RoutingLocation;
7 use Doctrine\Tests\Models\Routing\RoutingLeg;
8
9 require_once __DIR__ . '/../../TestInit.php';
10
11 /**
12  * Tests a bidirectional one-to-one association mapping (without inheritance).
13  */
14 class OneToManyUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
15 {
16     protected $locations = array();
17
18     public function setUp()
19     {
20         $this->useModelSet('routing');
21         parent::setUp();
22
23         $locations = array("Berlin", "Bonn", "Brasilia", "Atlanta");
24
25         foreach ($locations AS $locationName) {
26             $location = new RoutingLocation();
27             $location->name = $locationName;
28             $this->_em->persist($location);
29             $this->locations[$locationName] = $location;
30         }
31         $this->_em->flush();
32     }
33
34     public function testPersistOwning_InverseCascade()
35     {
36         $leg = new RoutingLeg();
37         $leg->fromLocation = $this->locations['Berlin'];
38         $leg->toLocation   = $this->locations['Bonn'];
39         $leg->departureDate = new \DateTime("now");
40         $leg->arrivalDate = new \DateTime("now +5 hours");
41
42         $route = new RoutingRoute();
43         $route->legs[] = $leg;
44
45         $this->_em->persist($route);
46         $this->_em->flush();
47         $this->_em->clear();
48
49         $routes = $this->_em->createQuery(
50             "SELECT r, l, f, t FROM Doctrine\Tests\Models\Routing\RoutingRoute r ".
51             "JOIN r.legs l JOIN l.fromLocation f JOIN l.toLocation t"
52         )->getSingleResult();
53
54         $this->assertEquals(1, count($routes->legs));
55         $this->assertEquals("Berlin", $routes->legs[0]->fromLocation->name);
56         $this->assertEquals("Bonn", $routes->legs[0]->toLocation->name);
57     }
58
59     public function testLegsAreUniqueToRoutes()
60     {
61         $leg = new RoutingLeg();
62         $leg->fromLocation = $this->locations['Berlin'];
63         $leg->toLocation   = $this->locations['Bonn'];
64         $leg->departureDate = new \DateTime("now");
65         $leg->arrivalDate = new \DateTime("now +5 hours");
66
67         $routeA = new RoutingRoute();
68         $routeA->legs[] = $leg;
69
70         $routeB = new RoutingRoute();
71         $routeB->legs[] = $leg;
72
73         $this->_em->persist($routeA);
74         $this->_em->persist($routeB);
75
76         $exceptionThrown = false;
77         try {
78             // exception depending on the underyling Database Driver
79             $this->_em->flush();
80         } catch(\Exception $e) {
81             $exceptionThrown = true;
82         }
83
84         $this->assertTrue($exceptionThrown, "The underlying database driver throws an exception.");
85     }
86 }