Commit 8d607b1b authored by beberlei's avatar beberlei

[2.0] DDC-289 / DDC-264 - Added a Model and TestCase for OneToMany...

[2.0] DDC-289 / DDC-264 - Added a Model and TestCase for OneToMany Unidirectional Assocations using a JoinTable and tested its defining feature (uniqueness).
parent 3bec7689
<?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;
}
\ No newline at end of file
<?php
namespace Doctrine\Tests\Models\Routing;
/**
* @Entity
*/
class RoutingLocation
{
/**
* @Id
* @generatedValue(strategy="AUTO")
* @Column(type="integer")
*/
public $id;
/**
* @Column(type="string")
*/
public $name;
}
\ No newline at end of file
<?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();
}
}
\ No newline at end of file
...@@ -33,6 +33,7 @@ class AllTests ...@@ -33,6 +33,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneUnidirectionalAssociationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneUnidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToManyBidirectionalAssociationTest'); $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\ManyToManyBasicAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManyUnidirectionalAssociationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManyUnidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManyBidirectionalAssociationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManyBidirectionalAssociationTest');
......
<?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
}
}
\ No newline at end of file
...@@ -7,7 +7,7 @@ namespace Doctrine\Tests; ...@@ -7,7 +7,7 @@ namespace Doctrine\Tests;
* *
* @since 2.0 * @since 2.0
*/ */
class OrmFunctionalTestCase extends OrmTestCase abstract class OrmFunctionalTestCase extends OrmTestCase
{ {
/* The metadata cache shared between all functional tests. */ /* The metadata cache shared between all functional tests. */
private static $_metadataCacheImpl = null; private static $_metadataCacheImpl = null;
...@@ -68,7 +68,12 @@ class OrmFunctionalTestCase extends OrmTestCase ...@@ -68,7 +68,12 @@ class OrmFunctionalTestCase extends OrmTestCase
), ),
'generic' => array( 'generic' => array(
'Doctrine\Tests\Models\Generic\DateTimeModel' '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) protected function useModelSet($setName)
...@@ -213,11 +218,11 @@ class OrmFunctionalTestCase extends OrmTestCase ...@@ -213,11 +218,11 @@ class OrmFunctionalTestCase extends OrmTestCase
throw $e; throw $e;
} }
if($this->_sqlLoggerStack->queries !== null && count($this->_sqlLoggerStack->queries)) { if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
$queries = ""; $queries = "";
for($i = 0; $i < count($this->_sqlLoggerStack->queries); $i++) { for($i = 0; $i < count($this->_sqlLoggerStack->queries); $i++) {
$query = $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; $queries .= ($i+1).". SQL: '".$query['sql']."' Params: ".implode(", ", $params).PHP_EOL;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment