-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
06 - Add an event registration to the website
- Loading branch information
1 parent
20d848e
commit 679f8e5
Showing
8 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* @ORM\Entity(repositoryClass="App\Repository\EventRegistrationRepository") | ||
*/ | ||
class EventRegistration | ||
{ | ||
/** | ||
* @ORM\Id() | ||
* @ORM\GeneratedValue() | ||
* @ORM\Column(type="integer") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var Event | ||
* | ||
* @ORM\ManyToOne(targetEntity="App\Entity\Event") | ||
* @ORM\JoinColumn(onDelete="CASCADE") | ||
*/ | ||
private $event; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255) | ||
* | ||
* @Assert\NotBlank | ||
*/ | ||
private $firstName; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255) | ||
* | ||
* @Assert\NotBlank | ||
*/ | ||
private $lastName; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255) | ||
* | ||
* @Assert\NotBlank | ||
* @Assert\Email() | ||
*/ | ||
private $email; | ||
|
||
/** | ||
* @ORM\Column(type="text", length=255) | ||
*/ | ||
private $message; | ||
|
||
public function __construct(Event $event) | ||
{ | ||
$this->event = $event; | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getEvent(): Event | ||
{ | ||
return $this->event; | ||
} | ||
|
||
public function getFirstName(): ?string | ||
{ | ||
return $this->firstName; | ||
} | ||
|
||
public function setFirstName(string $firstName): self | ||
{ | ||
$this->firstName = $firstName; | ||
|
||
return $this; | ||
} | ||
|
||
public function getLastName(): ?string | ||
{ | ||
return $this->lastName; | ||
} | ||
|
||
public function setLastName(string $lastName): self | ||
{ | ||
$this->lastName = $lastName; | ||
|
||
return $this; | ||
} | ||
|
||
public function getEmail(): ?string | ||
{ | ||
return $this->email; | ||
} | ||
|
||
public function setEmail(string $email): self | ||
{ | ||
$this->email = $email; | ||
|
||
return $this; | ||
} | ||
|
||
public function getMessage(): ?string | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function setMessage(string $message): self | ||
{ | ||
$this->message = $message; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Form; | ||
|
||
use App\Entity\EventRegistration; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class EventRegistrationType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add('firstName') | ||
->add('lastName') | ||
->add('email') | ||
->add('message'); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults( | ||
[ | ||
'data_class' => EventRegistration::class, | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Repository; | ||
|
||
use App\Entity\Event; | ||
use App\Entity\EventRegistration; | ||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
|
||
/** | ||
* @method EventRegistration|null find($id, $lockMode = null, $lockVersion = null) | ||
* @method EventRegistration|null findOneBy(array $criteria, array $orderBy = null) | ||
* @method EventRegistration[] findAll() | ||
* @method EventRegistration[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | ||
*/ | ||
class EventRegistrationRepository extends ServiceEntityRepository | ||
{ | ||
public function __construct(ManagerRegistry $registry) | ||
{ | ||
parent::__construct($registry, EventRegistration::class); | ||
} | ||
|
||
public function create(Event $event): EventRegistration | ||
{ | ||
$registration = new EventRegistration($event); | ||
|
||
return $registration; | ||
} | ||
|
||
public function save(EventRegistration $registration): void | ||
{ | ||
$this->getEntityManager()->persist($registration); | ||
$this->getEntityManager()->flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,15 @@ | |
|
||
namespace App\Tests\Functional\Controller; | ||
|
||
use App\Tests\Functional\Traits\EventRegistrationTrait; | ||
use App\Tests\Functional\Traits\EventTrait; | ||
use Sulu\Bundle\TestBundle\Testing\SuluTestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class EventWebsiteControllerTest extends SuluTestCase | ||
{ | ||
use EventTrait; | ||
use EventRegistrationTrait; | ||
|
||
public function setUp(): void | ||
{ | ||
|
@@ -34,4 +36,39 @@ public function testIndexAction(): void | |
|
||
$this->assertStringContainsString('Sulu is awesome', $crawler->filter('h1')->html()); | ||
} | ||
|
||
public function testRegister(): void | ||
{ | ||
$client = $this->createWebsiteClient(); | ||
|
||
$event = $this->createEvent('Sulu is awesome', 'en'); | ||
|
||
$crawler = $client->request('GET', '/en/event/' . $event->getId()); | ||
|
||
$response = $client->getResponse(); | ||
$this->assertInstanceOf(Response::class, $response); | ||
$this->assertHttpStatusCode(200, $response); | ||
|
||
$form = $crawler->filter('#event_registration_submit')->form( | ||
[ | ||
'event_registration[firstName]' => 'Max', | ||
'event_registration[lastName]' => 'Mustermann', | ||
'event_registration[email]' => '[email protected]', | ||
'event_registration[message]' => 'I would love to see this.', | ||
] | ||
); | ||
|
||
$client->submit($form); | ||
$crawler = $client->followRedirect(); | ||
|
||
$this->assertStringContainsString('Thanks for your registration', $crawler->filter('.success')->html()); | ||
|
||
$registrations = $this->findEventRegistrations($event); | ||
|
||
$this->assertCount(1, $registrations); | ||
$this->assertSame('Max', $registrations[0]->getFirstName()); | ||
$this->assertSame('Mustermann', $registrations[0]->getLastName()); | ||
$this->assertSame('[email protected]', $registrations[0]->getEmail()); | ||
$this->assertSame('I would love to see this.', $registrations[0]->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Functional\Traits; | ||
|
||
use App\Entity\Event; | ||
use App\Entity\EventRegistration; | ||
use App\Repository\EventRegistrationRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
trait EventRegistrationTrait | ||
{ | ||
/** | ||
* @return EventRegistration[] | ||
*/ | ||
public function findEventRegistrations(Event $event): array | ||
{ | ||
return $this->getEventRegistrationRepository()->findBy(['event' => $event]); | ||
} | ||
|
||
protected function getEventRegistrationRepository(): EventRegistrationRepository | ||
{ | ||
return $this->getEntityManager()->getRepository(EventRegistration::class); | ||
} | ||
|
||
abstract protected function getEntityManager(): EntityManagerInterface; | ||
} |
Oops, something went wrong.