[2.0] DDC-289 / DDC-264 - Added a Model and TestCase for OneToMany Unidirectional Assocations using a JoinTable and tested its defining feature (uniqueness).
This commit is contained in:
parent
3bec7689a7
commit
8d607b1b78
6 changed files with 180 additions and 4 deletions
38
tests/Doctrine/Tests/Models/Routing/RoutingLeg.php
Normal file
38
tests/Doctrine/Tests/Models/Routing/RoutingLeg.php
Normal file
|
@ -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
tests/Doctrine/Tests/Models/Routing/RoutingLocation.php
Normal file
21
tests/Doctrine/Tests/Models/Routing/RoutingLocation.php
Normal file
|
@ -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
tests/Doctrine/Tests/Models/Routing/RoutingRoute.php
Normal file
32
tests/Doctrine/Tests/Models/Routing/RoutingRoute.php
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -33,6 +33,7 @@ class AllTests
|
|||
$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');
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ namespace Doctrine\Tests;
|
|||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
class OrmFunctionalTestCase extends OrmTestCase
|
||||
abstract class OrmFunctionalTestCase extends OrmTestCase
|
||||
{
|
||||
/* The metadata cache shared between all functional tests. */
|
||||
private static $_metadataCacheImpl = null;
|
||||
|
@ -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)
|
||||
|
@ -213,11 +218,11 @@ class OrmFunctionalTestCase extends OrmTestCase
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue