Skip to content

Commit

Permalink
[2.0] DDC-289 / DDC-264 - Added a Model and TestCase for OneToMany Un…
Browse files Browse the repository at this point in the history
…idirectional Assocations using a JoinTable and tested its defining feature (uniqueness).
  • Loading branch information
beberlei committed Jan 31, 2010
1 parent 3bec768 commit 8d607b1
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 4 deletions.
38 changes: 38 additions & 0 deletions tests/Doctrine/Tests/Models/Routing/RoutingLeg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Doctrine\Tests\Models\Routing;

/**
* @Entity
*/
class RoutingLeg
{
/**
* @Id
* @generatedValue(strategy="AUTO")
* @column(type="integer")
*/
public $id;

/**
* @ManyToOne(targetEntity="Doctrine\Tests\Models\Routing\RoutingLocation")
* @JoinColumn(name="from_id", referencedColumnName="id")
*/
public $fromLocation;

/**
* @ManyToOne(targetEntity="Doctrine\Tests\Models\Routing\RoutingLocation")
* @JoinColumn(name="to_id", referencedColumnName="id")
*/
public $toLocation;

/**
* @Column(type="datetime")
*/
public $departureDate;

/**
* @Column(type="datetime")
*/
public $arrivalDate;
}
21 changes: 21 additions & 0 deletions tests/Doctrine/Tests/Models/Routing/RoutingLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Doctrine\Tests\Models\Routing;

/**
* @Entity
*/
class RoutingLocation
{
/**
* @Id
* @generatedValue(strategy="AUTO")
* @Column(type="integer")
*/
public $id;

/**
* @Column(type="string")
*/
public $name;
}
32 changes: 32 additions & 0 deletions tests/Doctrine/Tests/Models/Routing/RoutingRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Doctrine\Tests\Models\Routing;

use Doctrine\Common\Collections\ArrayCollection;

/**
* @Entity
*/
class RoutingRoute
{
/**
* @Id
* @generatedValue(strategy="AUTO")
* @column(type="integer")
*/
public $id;

/**
* @ManyToMany(targetEntity="Doctrine\Tests\Models\Routing\RoutingLeg", cascade={"all"})
* @JoinTable(name="RoutingRouteLegs",
* joinColumns={@JoinColumn(name="route_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="leg_id", referencedColumnName="id", unique=true)}
* )
*/
public $legs;

public function __construct()
{
$this->legs = new ArrayCollection();
}
}
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/ORM/Functional/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function suite()
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneUnidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToManyBidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToManyUnidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManyBasicAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManyUnidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManyBidirectionalAssociationTest');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\Routing\RoutingRoute;
use Doctrine\Tests\Models\Routing\RoutingLocation;
use Doctrine\Tests\Models\Routing\RoutingLeg;

require_once __DIR__ . '/../../TestInit.php';

/**
* Tests a bidirectional one-to-one association mapping (without inheritance).
*/
class OneToManyUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected $locations = array();

public function setUp()
{
$this->useModelSet('routing');
parent::setUp();

$locations = array("Berlin", "Bonn", "Brasilia", "Atlanta");

foreach ($locations AS $locationName) {
$location = new RoutingLocation();
$location->name = $locationName;
$this->_em->persist($location);
$this->locations[$locationName] = $location;
}
$this->_em->flush();
}

public function testPersistOwning_InverseCascade()
{
$leg = new RoutingLeg();
$leg->fromLocation = $this->locations['Berlin'];
$leg->toLocation = $this->locations['Bonn'];
$leg->departureDate = new \DateTime("now");
$leg->arrivalDate = new \DateTime("now +5 hours");

$route = new RoutingRoute();
$route->legs[] = $leg;

$this->_em->persist($route);
$this->_em->flush();
$this->_em->clear();

$routes = $this->_em->createQuery(
"SELECT r, l, f, t FROM Doctrine\Tests\Models\Routing\RoutingRoute r ".
"JOIN r.legs l JOIN l.fromLocation f JOIN l.toLocation t"
)->getSingleResult();

$this->assertEquals(1, count($routes->legs));
$this->assertEquals("Berlin", $routes->legs[0]->fromLocation->name);
$this->assertEquals("Bonn", $routes->legs[0]->toLocation->name);
}

public function testLegsAreUniqueToRoutes()
{
$leg = new RoutingLeg();
$leg->fromLocation = $this->locations['Berlin'];
$leg->toLocation = $this->locations['Bonn'];
$leg->departureDate = new \DateTime("now");
$leg->arrivalDate = new \DateTime("now +5 hours");

$routeA = new RoutingRoute();
$routeA->legs[] = $leg;

$routeB = new RoutingRoute();
$routeB->legs[] = $leg;

$this->_em->persist($routeA);
$this->_em->persist($routeB);

$this->setExpectedException('Exception'); // depends on the underyling Database Driver
$this->_em->flush(); // Exception
}
}
13 changes: 9 additions & 4 deletions tests/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @since 2.0
*/
class OrmFunctionalTestCase extends OrmTestCase
abstract class OrmFunctionalTestCase extends OrmTestCase
{
/* The metadata cache shared between all functional tests. */
private static $_metadataCacheImpl = null;
Expand Down Expand Up @@ -68,7 +68,12 @@ class OrmFunctionalTestCase extends OrmTestCase
),
'generic' => array(
'Doctrine\Tests\Models\Generic\DateTimeModel'
)
),
'routing' => array(
'Doctrine\Tests\Models\Routing\RoutingLeg',
'Doctrine\Tests\Models\Routing\RoutingLocation',
'Doctrine\Tests\Models\Routing\RoutingRoute',
),
);

protected function useModelSet($setName)
Expand Down Expand Up @@ -213,11 +218,11 @@ protected function onNotSuccessfulTest(\Exception $e)
throw $e;
}

if($this->_sqlLoggerStack->queries !== null && count($this->_sqlLoggerStack->queries)) {
if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
$queries = "";
for($i = 0; $i < count($this->_sqlLoggerStack->queries); $i++) {
$query = $this->_sqlLoggerStack->queries[$i];
$params = array_map(function($p) { return "'".$p."'"; }, $query['params']);
$params = array_map(function($p) { return "'".$p."'"; }, $query['params'] ?: array());
$queries .= ($i+1).". SQL: '".$query['sql']."' Params: ".implode(", ", $params).PHP_EOL;
}

Expand Down

0 comments on commit 8d607b1

Please sign in to comment.