Skip to content

Commit 679f8e5

Browse files
06 - Add an event registration to the website
1 parent 20d848e commit 679f8e5

File tree

8 files changed

+350
-0
lines changed

8 files changed

+350
-0
lines changed

src/Controller/EventWebsiteController.php

+31
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace App\Controller;
66

7+
use App\Form\EventRegistrationType;
8+
use App\Repository\EventRegistrationRepository;
79
use App\Repository\EventRepository;
810
use Sulu\Bundle\WebsiteBundle\Resolver\TemplateAttributeResolverInterface;
911
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
1013
use Symfony\Component\HttpFoundation\Request;
1114
use Symfony\Component\HttpFoundation\Response;
1215
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -20,11 +23,38 @@ public function indexAction(int $id, Request $request): Response
2023
throw new NotFoundHttpException();
2124
}
2225

26+
$eventRegistrationRepository = $this->get(EventRegistrationRepository::class);
27+
$registration = $eventRegistrationRepository->create($event);
28+
$form = $this->createForm(EventRegistrationType::class, $registration);
29+
$form->add(
30+
'submit',
31+
SubmitType::class,
32+
[
33+
'label' => 'Create',
34+
]
35+
);
36+
37+
$form->handleRequest($request);
38+
39+
if ($form->isSubmitted() && $form->isValid()) {
40+
$eventRegistrationRepository->save($registration);
41+
42+
return $this->redirectToRoute(
43+
'app.event',
44+
[
45+
'id' => $event->getId(),
46+
'success' => true,
47+
]
48+
);
49+
}
50+
2351
return $this->render(
2452
'events/index.html.twig',
2553
$this->get(TemplateAttributeResolverInterface::class)->resolve(
2654
[
2755
'event' => $event,
56+
'success' => $request->query->get('success'),
57+
'form' => $form->createView(),
2858
'content' => ['title' => $event->getTitle()],
2959
]
3060
)
@@ -37,6 +67,7 @@ public static function getSubscribedServices(): array
3767
parent::getSubscribedServices(),
3868
[
3969
EventRepository::class,
70+
EventRegistrationRepository::class,
4071
TemplateAttributeResolverInterface::class,
4172
]
4273
);

src/Entity/EventRegistration.php

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Entity;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Symfony\Component\Validator\Constraints as Assert;
9+
10+
/**
11+
* @ORM\Entity(repositoryClass="App\Repository\EventRegistrationRepository")
12+
*/
13+
class EventRegistration
14+
{
15+
/**
16+
* @ORM\Id()
17+
* @ORM\GeneratedValue()
18+
* @ORM\Column(type="integer")
19+
*/
20+
private $id;
21+
22+
/**
23+
* @var Event
24+
*
25+
* @ORM\ManyToOne(targetEntity="App\Entity\Event")
26+
* @ORM\JoinColumn(onDelete="CASCADE")
27+
*/
28+
private $event;
29+
30+
/**
31+
* @ORM\Column(type="string", length=255)
32+
*
33+
* @Assert\NotBlank
34+
*/
35+
private $firstName;
36+
37+
/**
38+
* @ORM\Column(type="string", length=255)
39+
*
40+
* @Assert\NotBlank
41+
*/
42+
private $lastName;
43+
44+
/**
45+
* @ORM\Column(type="string", length=255)
46+
*
47+
* @Assert\NotBlank
48+
* @Assert\Email()
49+
*/
50+
private $email;
51+
52+
/**
53+
* @ORM\Column(type="text", length=255)
54+
*/
55+
private $message;
56+
57+
public function __construct(Event $event)
58+
{
59+
$this->event = $event;
60+
}
61+
62+
public function getId(): ?int
63+
{
64+
return $this->id;
65+
}
66+
67+
public function getEvent(): Event
68+
{
69+
return $this->event;
70+
}
71+
72+
public function getFirstName(): ?string
73+
{
74+
return $this->firstName;
75+
}
76+
77+
public function setFirstName(string $firstName): self
78+
{
79+
$this->firstName = $firstName;
80+
81+
return $this;
82+
}
83+
84+
public function getLastName(): ?string
85+
{
86+
return $this->lastName;
87+
}
88+
89+
public function setLastName(string $lastName): self
90+
{
91+
$this->lastName = $lastName;
92+
93+
return $this;
94+
}
95+
96+
public function getEmail(): ?string
97+
{
98+
return $this->email;
99+
}
100+
101+
public function setEmail(string $email): self
102+
{
103+
$this->email = $email;
104+
105+
return $this;
106+
}
107+
108+
public function getMessage(): ?string
109+
{
110+
return $this->message;
111+
}
112+
113+
public function setMessage(string $message): self
114+
{
115+
$this->message = $message;
116+
117+
return $this;
118+
}
119+
}

src/Form/EventRegistrationType.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Form;
6+
7+
use App\Entity\EventRegistration;
8+
use Symfony\Component\Form\AbstractType;
9+
use Symfony\Component\Form\FormBuilderInterface;
10+
use Symfony\Component\OptionsResolver\OptionsResolver;
11+
12+
class EventRegistrationType extends AbstractType
13+
{
14+
public function buildForm(FormBuilderInterface $builder, array $options)
15+
{
16+
$builder
17+
->add('firstName')
18+
->add('lastName')
19+
->add('email')
20+
->add('message');
21+
}
22+
23+
public function configureOptions(OptionsResolver $resolver)
24+
{
25+
$resolver->setDefaults(
26+
[
27+
'data_class' => EventRegistration::class,
28+
]
29+
);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Repository;
6+
7+
use App\Entity\Event;
8+
use App\Entity\EventRegistration;
9+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
10+
use Doctrine\Common\Persistence\ManagerRegistry;
11+
12+
/**
13+
* @method EventRegistration|null find($id, $lockMode = null, $lockVersion = null)
14+
* @method EventRegistration|null findOneBy(array $criteria, array $orderBy = null)
15+
* @method EventRegistration[] findAll()
16+
* @method EventRegistration[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17+
*/
18+
class EventRegistrationRepository extends ServiceEntityRepository
19+
{
20+
public function __construct(ManagerRegistry $registry)
21+
{
22+
parent::__construct($registry, EventRegistration::class);
23+
}
24+
25+
public function create(Event $event): EventRegistration
26+
{
27+
$registration = new EventRegistration($event);
28+
29+
return $registration;
30+
}
31+
32+
public function save(EventRegistration $registration): void
33+
{
34+
$this->getEntityManager()->persist($registration);
35+
$this->getEntityManager()->flush();
36+
}
37+
}

templates/events/index.html.twig

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{% extends "base.html.twig" %}
22

3+
{% form_theme form 'bootstrap_4_layout.html.twig' %}
4+
35
{% block content %}
46
<section class="jumbotron text-center">
57
<div class="container">
@@ -11,4 +13,16 @@
1113
<div class="container">
1214
{{ event.description|raw }}
1315
</div>
16+
17+
<div class="container">
18+
{% if success %}
19+
<div class="success">
20+
<b>Thanks for your registration.</b>
21+
</div>
22+
{% else %}
23+
<h2>Register for this event:</h2>
24+
25+
{{ form(form) }}
26+
{% endif %}
27+
</div>
1428
{% endblock %}

tests/Functional/Controller/EventWebsiteControllerTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace App\Tests\Functional\Controller;
66

7+
use App\Tests\Functional\Traits\EventRegistrationTrait;
78
use App\Tests\Functional\Traits\EventTrait;
89
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
910
use Symfony\Component\HttpFoundation\Response;
1011

1112
class EventWebsiteControllerTest extends SuluTestCase
1213
{
1314
use EventTrait;
15+
use EventRegistrationTrait;
1416

1517
public function setUp(): void
1618
{
@@ -34,4 +36,39 @@ public function testIndexAction(): void
3436

3537
$this->assertStringContainsString('Sulu is awesome', $crawler->filter('h1')->html());
3638
}
39+
40+
public function testRegister(): void
41+
{
42+
$client = $this->createWebsiteClient();
43+
44+
$event = $this->createEvent('Sulu is awesome', 'en');
45+
46+
$crawler = $client->request('GET', '/en/event/' . $event->getId());
47+
48+
$response = $client->getResponse();
49+
$this->assertInstanceOf(Response::class, $response);
50+
$this->assertHttpStatusCode(200, $response);
51+
52+
$form = $crawler->filter('#event_registration_submit')->form(
53+
[
54+
'event_registration[firstName]' => 'Max',
55+
'event_registration[lastName]' => 'Mustermann',
56+
'event_registration[email]' => '[email protected]',
57+
'event_registration[message]' => 'I would love to see this.',
58+
]
59+
);
60+
61+
$client->submit($form);
62+
$crawler = $client->followRedirect();
63+
64+
$this->assertStringContainsString('Thanks for your registration', $crawler->filter('.success')->html());
65+
66+
$registrations = $this->findEventRegistrations($event);
67+
68+
$this->assertCount(1, $registrations);
69+
$this->assertSame('Max', $registrations[0]->getFirstName());
70+
$this->assertSame('Mustermann', $registrations[0]->getLastName());
71+
$this->assertSame('[email protected]', $registrations[0]->getEmail());
72+
$this->assertSame('I would love to see this.', $registrations[0]->getMessage());
73+
}
3774
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Functional\Traits;
6+
7+
use App\Entity\Event;
8+
use App\Entity\EventRegistration;
9+
use App\Repository\EventRegistrationRepository;
10+
use Doctrine\ORM\EntityManagerInterface;
11+
12+
trait EventRegistrationTrait
13+
{
14+
/**
15+
* @return EventRegistration[]
16+
*/
17+
public function findEventRegistrations(Event $event): array
18+
{
19+
return $this->getEventRegistrationRepository()->findBy(['event' => $event]);
20+
}
21+
22+
protected function getEventRegistrationRepository(): EventRegistrationRepository
23+
{
24+
return $this->getEntityManager()->getRepository(EventRegistration::class);
25+
}
26+
27+
abstract protected function getEntityManager(): EntityManagerInterface;
28+
}

0 commit comments

Comments
 (0)