Skip to content

Commit d6c84b1

Browse files
wachterjohannesluca-rath
authored andcommitted
11 - Add a relation between events and locations
1 parent cde5ea5 commit d6c84b1

File tree

9 files changed

+102
-23
lines changed

9 files changed

+102
-23
lines changed

config/forms/event_details.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</meta>
1313
</property>
1414

15-
<property name="location" type="text_line" colspan="3">
15+
<property name="locationId" type="single_location_selection" mandatory="true" colspan="3">
1616
<meta>
1717
<title>app.location</title>
1818
</meta>

config/packages/sulu_admin.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,17 @@ sulu_admin:
3838
icon: fa-calendar
3939
label: 'app.events'
4040
overlay_title: 'app.events'
41+
42+
single_selection:
43+
single_location_selection:
44+
default_type: list_overlay
45+
resource_key: locations
46+
types:
47+
list_overlay:
48+
adapter: table
49+
list_key: locations
50+
display_properties:
51+
- name
52+
icon: fa-home
53+
empty_text: 'app.location.no_selections'
54+
overlay_title: 'app.locations'

src/Controller/Admin/EventController.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Common\DoctrineListRepresentationFactory;
88
use App\Entity\Event;
99
use App\Repository\EventRepository;
10+
use App\Repository\LocationRepository;
1011
use FOS\RestBundle\Controller\Annotations as Rest;
1112
use FOS\RestBundle\Routing\ClassResourceInterface;
1213
use FOS\RestBundle\View\ViewHandlerInterface;
@@ -18,24 +19,31 @@
1819

1920
class EventController extends AbstractRestController implements ClassResourceInterface
2021
{
22+
/**
23+
* @var DoctrineListRepresentationFactory
24+
*/
25+
private $doctrineListRepresentationFactory;
26+
2127
/**
2228
* @var EventRepository
2329
*/
2430
private $repository;
2531

2632
/**
27-
* @var DoctrineListRepresentationFactory
33+
* @var LocationRepository
2834
*/
29-
private $doctrineListRepresentationFactory;
35+
private $locationRepository;
3036

3137
public function __construct(
32-
EventRepository $repository,
3338
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
39+
EventRepository $repository,
40+
LocationRepository $locationRepository,
3441
ViewHandlerInterface $viewHandler,
3542
?TokenStorageInterface $tokenStorage = null
3643
) {
37-
$this->repository = $repository;
3844
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
45+
$this->repository = $repository;
46+
$this->locationRepository = $locationRepository;
3947

4048
parent::__construct($viewHandler, $tokenStorage);
4149
}
@@ -141,8 +149,10 @@ protected function mapDataToEntity(array $data, Event $entity): void
141149
$entity->setEndDate(new \DateTimeImmutable($endDate));
142150
}
143151

144-
if ($location = $data['location'] ?? null) {
145-
$entity->setLocation($location);
152+
if ($locationId = $data['locationId'] ?? null) {
153+
$entity->setLocation(
154+
$this->locationRepository->findById((int) $locationId)
155+
);
146156
}
147157
}
148158

src/DataFixtures/ORM/AppFixtures.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\DataFixtures\ORM;
66

77
use App\Entity\Event;
8+
use App\Entity\Location;
89
use Doctrine\Bundle\FixturesBundle\Fixture;
910
use Doctrine\Persistence\ObjectManager;
1011

@@ -14,7 +15,8 @@ class AppFixtures extends Fixture
1415

1516
public function load(ObjectManager $manager): void
1617
{
17-
$repository = $manager->getRepository(Event::class);
18+
$eventRepository = $manager->getRepository(Event::class);
19+
$locationRepository = $manager->getRepository(Location::class);
1820

1921
$data = [
2022
[
@@ -83,17 +85,29 @@ public function load(ObjectManager $manager): void
8385
];
8486

8587
foreach ($data as $item) {
86-
$event = $repository->create(self::LOCALE);
88+
$location = null;
89+
if ($item['location']) {
90+
$location = $locationRepository->create();
91+
$location->setName($item['location']);
92+
$location->setStreet('');
93+
$location->setNumber('');
94+
$location->setCity('');
95+
$location->setCountryCode('');
96+
$location->setPostalCode('');
97+
$locationRepository->save($location);
98+
}
99+
100+
$event = $eventRepository->create(self::LOCALE);
87101

88102
$event->setTitle($item['title']);
89-
$event->setLocation($item['location']);
103+
$event->setLocation($location);
90104
$event->setTeaser($item['teaser']);
91105
$event->setDescription('<p>' . $item['description'] . '</p>');
92106
$event->setStartDate(new \DateTimeImmutable($item['startDate']));
93107
$event->setEndDate(new \DateTimeImmutable($item['endDate']));
94108
$event->setEnabled($item['enabled']);
95109

96-
$repository->save($event);
110+
$eventRepository->save($event);
97111
}
98112

99113
$manager->flush();

src/Entity/Event.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ class Event
4747
private $endDate;
4848

4949
/**
50-
* @var string|null
50+
* @var Location|null
5151
*
52-
* @ORM\Column(type="string", nullable=true)
52+
* @ORM\ManyToOne(targetEntity="App\Entity\Location")
53+
* @ORM\JoinColumn(onDelete="SET NULL")
5354
*/
5455
private $location;
5556

@@ -114,18 +115,30 @@ public function setEndDate(?\DateTimeImmutable $endDate): self
114115
return $this;
115116
}
116117

117-
public function getLocation(): ?string
118+
public function getLocation(): ?Location
118119
{
119120
return $this->location;
120121
}
121122

122-
public function setLocation(?string $location): self
123+
public function setLocation(?Location $location): self
123124
{
124125
$this->location = $location;
125126

126127
return $this;
127128
}
128129

130+
/**
131+
* @Serializer\VirtualProperty
132+
*/
133+
public function getLocationId(): ?int
134+
{
135+
if (!$this->location) {
136+
return null;
137+
}
138+
139+
return $this->location->getId();
140+
}
141+
129142
/**
130143
* @Serializer\VirtualProperty(name="title")
131144
*/

tests/Functional/Controller/Admin/EventControllerTest.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
namespace App\Tests\Functional\Controller\Admin;
66

77
use App\Tests\Functional\Traits\EventTrait;
8+
use App\Tests\Functional\Traits\LocationTrait;
89
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
910
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1011
use Symfony\Component\HttpFoundation\Response;
1112

1213
class EventControllerTest extends SuluTestCase
1314
{
1415
use EventTrait;
16+
use LocationTrait;
1517

1618
/**
1719
* @var KernelBrowser
@@ -64,6 +66,8 @@ public function testGet(): void
6466

6567
public function testPost(): void
6668
{
69+
$location = $this->createLocation('Sulu HQ');
70+
6771
$this->client->request(
6872
'POST',
6973
'/admin/api/events?locale=de',
@@ -73,7 +77,7 @@ public function testPost(): void
7377
'startDate' => '2019-01-01 12:00',
7478
'endDate' => '2019-01-02 12:00',
7579
'description' => 'Sulu is really awesome',
76-
'location' => 'Dornbirn',
80+
'locationId' => $location->getId(),
7781
]
7882
);
7983

@@ -90,7 +94,7 @@ public function testPost(): void
9094
$this->assertSame('2019-01-01T12:00:00', $result['startDate']);
9195
$this->assertSame('2019-01-02T12:00:00', $result['endDate']);
9296
$this->assertSame('Sulu is really awesome', $result['description']);
93-
$this->assertSame('Dornbirn', $result['location']);
97+
$this->assertSame($location->getId(), $result['locationId']);
9498

9599
$result = $this->findEventById($result['id'], 'de');
96100

@@ -103,7 +107,7 @@ public function testPost(): void
103107
$this->assertNotNull($result->getEndDate());
104108
$this->assertSame('2019-01-02T12:00:00', $result->getEndDate()->format('Y-m-d\TH:i:s'));
105109
$this->assertSame('Sulu is really awesome', $result->getDescription());
106-
$this->assertSame('Dornbirn', $result->getLocation());
110+
$this->assertSame($location->getId(), $result->getLocationId());
107111
}
108112

109113
public function testPostNullValues(): void
@@ -146,6 +150,7 @@ public function testPostNullValues(): void
146150
public function testPut(): void
147151
{
148152
$event = $this->createEvent('Symfony', 'de');
153+
$location = $this->createLocation('Sulu HQ');
149154

150155
$this->client->request(
151156
'PUT',
@@ -156,7 +161,7 @@ public function testPut(): void
156161
'startDate' => '2019-01-01 12:00',
157162
'endDate' => '2019-01-02 12:00',
158163
'description' => 'Symfony Live is really awesome',
159-
'location' => 'Dornbirn',
164+
'locationId' => $location->getId(),
160165
]
161166
);
162167

@@ -173,7 +178,7 @@ public function testPut(): void
173178
$this->assertSame('2019-01-01T12:00:00', $result['startDate']);
174179
$this->assertSame('2019-01-02T12:00:00', $result['endDate']);
175180
$this->assertSame('Symfony Live is really awesome', $result['description']);
176-
$this->assertSame('Dornbirn', $result['location']);
181+
$this->assertSame($location->getId(), $result['locationId']);
177182

178183
$result = $this->findEventById($result['id'], 'de');
179184

@@ -186,7 +191,7 @@ public function testPut(): void
186191
$this->assertNotNull($result->getEndDate());
187192
$this->assertSame('2019-01-02T12:00:00', $result->getEndDate()->format('Y-m-d\TH:i:s'));
188193
$this->assertSame('Symfony Live is really awesome', $result->getDescription());
189-
$this->assertSame('Dornbirn', $result->getLocation());
194+
$this->assertSame($location->getId(), $result->getLocationId());
190195
}
191196

192197
public function testPutNullValues(): void

tests/Unit/Entity/EventTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@
66

77
use App\Entity\Event;
88
use App\Entity\EventTranslation;
9+
use App\Entity\Location;
910
use PHPUnit\Framework\TestCase;
11+
use Prophecy\Prophecy\ObjectProphecy;
1012

1113
class EventTest extends TestCase
1214
{
15+
/**
16+
* @var Location|ObjectProphecy
17+
*/
18+
private $location;
19+
1320
/**
1421
* @var Event
1522
*/
1623
private $event;
1724

1825
public function setUp(): void
1926
{
27+
$this->location = $this->prophesize(Location::class);
28+
2029
$this->event = new Event();
2130
$this->event->setLocale('de');
2231
}
@@ -87,4 +96,16 @@ public function testDescription(): void
8796
$this->assertSame('de', $this->event->getTranslations()['de']->getLocale());
8897
$this->assertSame('Sulu is awesome', $this->event->getTranslations()['de']->getDescription());
8998
}
99+
100+
public function testLocation(): void
101+
{
102+
$this->location->getId()->willReturn(42);
103+
104+
$this->assertNull($this->event->getLocation());
105+
$this->assertNull($this->event->getLocationId());
106+
$this->assertSame($this->event, $this->event->setLocation($this->location->reveal()));
107+
$this->assertNotNull($this->event->getLocation());
108+
$this->assertSame($this->location->reveal(), $this->event->getLocation());
109+
$this->assertSame(42, $this->event->getLocationId());
110+
}
90111
}

translations/admin.de.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"app.enable_event": "Veranstaltungen aktivieren",
88
"app.enabled": "Aktiviert",
99
"app.location": "Standort",
10-
"app.locations": "Standorte"
10+
"app.locations": "Standorte",
11+
"app.location.no_selections": "Kein Standord ausgewählt"
1112
}

translations/admin.en.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"app.enable_event": "Enable event",
88
"app.enabled": "Enabled",
99
"app.location": "Location",
10-
"app.locations": "Locations"
10+
"app.locations": "Locations",
11+
"app.location.no_selections": "No location selected"
1112
}

0 commit comments

Comments
 (0)