File: OrderedCollectionTest.php

package info (click to toggle)
doctrine 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,552 kB
  • sloc: php: 108,302; xml: 1,340; makefile: 35; sh: 14
file content (118 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download | duplicates (3)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?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\Models\Routing\RoutingRouteBooking;
use Doctrine\Tests\OrmFunctionalTestCase;

class OrderedCollectionTest 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 createPersistedRouteWithLegs(): int
    {
        $route = new RoutingRoute();

        $leg1                = new RoutingLeg();
        $leg1->fromLocation  = $this->locations['Berlin'];
        $leg1->toLocation    = $this->locations['Bonn'];
        $leg1->departureDate = new DateTime('now');
        $leg1->arrivalDate   = new DateTime('now +5 hours');

        $leg2                = new RoutingLeg();
        $leg2->fromLocation  = $this->locations['Bonn'];
        $leg2->toLocation    = $this->locations['Brasilia'];
        $leg2->departureDate = new DateTime('now +6 hours');
        $leg2->arrivalDate   = new DateTime('now +24 hours');

        $route->legs[] = $leg2;
        $route->legs[] = $leg1;

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

        return $routeId;
    }

    public function testLazyManyToManyCollectionIsRetrievedWithOrderByClause(): void
    {
        $routeId = $this->createPersistedRouteWithLegs();

        $route = $this->_em->find(RoutingRoute::class, $routeId);

        self::assertCount(2, $route->legs);
        self::assertEquals('Berlin', $route->legs[0]->fromLocation->getName());
        self::assertEquals('Bonn', $route->legs[1]->fromLocation->getName());
    }

    public function testLazyOneToManyCollectionIsRetrievedWithOrderByClause(): void
    {
        $route = new RoutingRoute();

        $this->_em->persist($route);
        $this->_em->flush();
        $routeId = $route->id;

        $booking1                = new RoutingRouteBooking();
        $booking1->passengerName = 'Guilherme';
        $booking2                = new RoutingRouteBooking();
        $booking2->passengerName = 'Benjamin';

        $route->bookings[] = $booking1;
        $booking1->route   = $route;
        $route->bookings[] = $booking2;
        $booking2->route   = $route;

        $this->_em->persist($booking1);
        $this->_em->persist($booking2);

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

        $route = $this->_em->find(RoutingRoute::class, $routeId);

        self::assertCount(2, $route->bookings);
        self::assertEquals('Benjamin', $route->bookings[0]->getPassengerName());
        self::assertEquals('Guilherme', $route->bookings[1]->getPassengerName());
    }

    public function testOrderedResultFromDqlQuery(): void
    {
        $routeId = $this->createPersistedRouteWithLegs();

        $route = $this->_em->createQuery('SELECT r, l FROM Doctrine\Tests\Models\Routing\RoutingRoute r JOIN r.legs l WHERE r.id = ?1')
                           ->setParameter(1, $routeId)
                           ->getSingleResult();

        self::assertCount(2, $route->legs);
        self::assertEquals('Berlin', $route->legs[0]->fromLocation->getName());
        self::assertEquals('Bonn', $route->legs[1]->fromLocation->getName());
    }
}