1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional;
use DateTime;
use Doctrine\Tests\Models\Routing\RoutingLeg;
use Doctrine\Tests\Models\Routing\RoutingLocation;
use Doctrine\Tests\Models\Routing\RoutingRoute;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;
/**
* Tests a bidirectional one-to-one association mapping (without inheritance).
*/
class OneToManyUnidirectionalAssociationTest extends OrmFunctionalTestCase
{
/** @phpstan-var array<string, RoutingLocation> */
protected $locations = [];
protected function setUp(): void
{
$this->useModelSet('routing');
parent::setUp();
$locations = ['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 testPersistOwningInverseCascade(): void
{
$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();
self::assertCount(1, $routes->legs);
self::assertEquals('Berlin', $routes->legs[0]->fromLocation->name);
self::assertEquals('Bonn', $routes->legs[0]->toLocation->name);
}
public function testLegsAreUniqueToRoutes(): void
{
$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);
// exception depending on the underlying Database Driver
$this->expectException(Exception::class);
$this->_em->flush();
}
}
|