Skip to content

Commit 1a70aea

Browse files
author
theUniC
committed
Symfony 4.4 support
1 parent 253ceef commit 1a70aea

14 files changed

Lines changed: 112 additions & 1819 deletions

File tree

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
phpunit.xml
22
vendor
3-
Tests/Functional/cache
4-
Tests/Functional/logs
3+
Tests/Functional/var
4+
.phpunit.result.cache
5+
bin/*
6+
composer.lock

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

Controller/DefaultController.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,33 @@
44

55
use FrequenceWeb\Bundle\ContactBundle\EventDispatcher\ContactEvents;
66
use FrequenceWeb\Bundle\ContactBundle\EventDispatcher\Event\MessageSubmitEvent;
7-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
8-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7+
use FrequenceWeb\Bundle\ContactBundle\Model\Contact;
8+
use Psr\Container\ContainerInterface;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
911
use Symfony\Component\HttpFoundation\RedirectResponse;
1012
use Symfony\Component\HttpFoundation\Request;
1113
use Symfony\Component\Form\FormInterface;
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
16+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
1218

1319
/**
1420
* Contact controller
1521
*
1622
* @author Yohan Giarelli <yohan@giarel.li>
1723
*/
18-
class DefaultController implements ContainerAwareInterface
24+
class DefaultController extends AbstractController
1925
{
20-
use ContainerAwareTrait;
21-
2226
/**
2327
* Action that displays the contact form
2428
*
2529
* @param Request $request
2630
*
27-
* @return \Symfony\Component\HttpFoundation\Response
31+
* @return Response
2832
*/
29-
public function indexAction(Request $request)
33+
public function indexAction(Request $request): Response
3034
{
3135
$this->container->get('session')->set('_fw_contact_referer', $request->getUri());
3236

@@ -38,17 +42,19 @@ public function indexAction(Request $request)
3842
*
3943
* @param Request $request
4044
*
41-
* @return \Symfony\Component\HttpFoundation\Response
45+
* @return Response
4246
*/
43-
public function submitAction(Request $request)
47+
public function submitAction(Request $request): Response
4448
{
4549
$form = $this->getForm();
4650
$form->handleRequest($request);
4751

48-
if ($form->isValid()) {
52+
if ($form->isSubmitted() && $form->isValid()) {
4953
// Send the event for message handling (send mail, add to DB, don't care)
5054
$event = new MessageSubmitEvent($form->getData());
51-
$this->container->get('event_dispatcher')->dispatch(ContactEvents::onMessageSubmit, $event);
55+
/** @var EventDispatcherInterface $eventDispatcher */
56+
$eventDispatcher = $this->container->get('event_dispatcher');
57+
$eventDispatcher->dispatch($event, ContactEvents::onMessageSubmit);
5258

5359
// Let say the user it's ok
5460
$message = $this->container->get('translator')->trans('contact.submit.success', [], 'FrequenceWebContactBundle');
@@ -71,12 +77,12 @@ public function submitAction(Request $request)
7177
*
7278
* @param FormInterface $form
7379
*
74-
* @return \Symfony\Component\HttpFoundation\Response
80+
* @return Response
7581
*/
7682
protected function renderFormResponse(FormInterface $form)
7783
{
78-
return $this->container->get('templating')->renderResponse(
79-
'FrequenceWebContactBundle:Default:index.html.twig',
84+
return $this->render(
85+
'@FrequenceWebContactBundle/Default/index.html.twig',
8086
['form' => $form->createView()]
8187
);
8288
}
@@ -86,11 +92,24 @@ protected function renderFormResponse(FormInterface $form)
8692
*
8793
* @return FormInterface
8894
*/
89-
protected function getForm()
95+
protected function getForm(): FormInterface
9096
{
9197
return $this->container->get('form.factory')->create(
9298
$this->container->getParameter('frequence_web_contact.type'),
9399
$this->container->get('frequence_web_contact.model')
94100
);
95101
}
102+
103+
/**
104+
* @inheritDoc
105+
*/
106+
public static function getSubscribedServices()
107+
{
108+
$dependentServices = parent::getSubscribedServices();
109+
110+
return array_merge($dependentServices, [
111+
'event_dispatcher' => EventDispatcherInterface::class,
112+
'translator' => TranslatorInterface::class,
113+
]);
114+
}
96115
}

DependencyInjection/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Configuration implements ConfigurationInterface
1919
*/
2020
public function getConfigTreeBuilder()
2121
{
22-
$treeBuilder = new TreeBuilder();
23-
$rootNode = $treeBuilder->root('frequence_web_contact');
22+
$treeBuilder = new TreeBuilder('frequence_web_contact');
23+
$rootNode = $treeBuilder->getRootNode();
2424

2525
$rootNode
2626
->addDefaultsIfNotSet()

EventDispatcher/Event/MessageSubmitEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace FrequenceWeb\Bundle\ContactBundle\EventDispatcher\Event;
44

55
use FrequenceWeb\Bundle\ContactBundle\Model\Contact;
6-
use Symfony\Component\EventDispatcher\Event;
6+
use Symfony\Contracts\EventDispatcher\Event;
77

88
/**
99
* This event is thrown each time an user send a message (Only and Only if validation pass)

EventDispatcher/Listener/EmailContactListener.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace FrequenceWeb\Bundle\ContactBundle\EventDispatcher\Listener;
44

55
use FrequenceWeb\Bundle\ContactBundle\EventDispatcher\Event\MessageSubmitEvent;
6-
use Symfony\Component\Templating\EngineInterface;
7-
use Symfony\Component\Translation\TranslatorInterface;
6+
use Swift_Mailer;
7+
use Symfony\Contracts\Translation\TranslatorInterface;
8+
use Twig\Environment;
89

910
/**
1011
* Listener for contact events, that sends emails
@@ -14,7 +15,7 @@
1415
class EmailContactListener
1516
{
1617
/**
17-
* @var \Swift_Mailer
18+
* @var Swift_Mailer
1819
*/
1920
protected $mailer;
2021

@@ -24,35 +25,24 @@ class EmailContactListener
2425
protected $translator;
2526

2627
/**
27-
* @var EngineInterface
28+
* @var Environment
2829
*/
29-
protected $templating;
30+
protected $twig;
3031

3132
/**
3233
* @var array
3334
*/
3435
protected $config;
3536

36-
/**
37-
* @param \Swift_Mailer $mailer
38-
* @param EngineInterface $templating
39-
* @param TranslatorInterface $translator
40-
* @param array<string> $config Configuration from DIC
41-
*/
42-
public function __construct(\Swift_Mailer $mailer, EngineInterface $templating, TranslatorInterface $translator, array $config)
37+
public function __construct(Swift_Mailer $mailer, Environment $twig, TranslatorInterface $translator, array $config)
4338
{
4439
$this->mailer = $mailer;
45-
$this->templating = $templating;
40+
$this->twig = $twig;
4641
$this->translator = $translator;
4742
$this->config = $config;
4843
}
4944

50-
/**
51-
* Called when onMessageSubmit event is fired
52-
*
53-
* @param MessageSubmitEvent $event
54-
*/
55-
public function onMessageSubmit(MessageSubmitEvent $event)
45+
public function onMessageSubmit(MessageSubmitEvent $event): void
5646
{
5747
$contact = $event->getContact();
5848

@@ -66,16 +56,16 @@ public function onMessageSubmit(MessageSubmitEvent $event)
6656
$message->addReplyTo($contact->getEmail(), $contact->getName());
6757
$message->addTo($this->config['to']);
6858
$message->addPart(
69-
$this->templating->render(
70-
'FrequenceWebContactBundle:Mails:mail.html.twig',
71-
array('contact' => $contact)
59+
$this->twig->render(
60+
'@FrequenceWebContactBundle/Mails/mail.html.twig',
61+
['contact' => $contact]
7262
),
7363
'text/html'
7464
);
7565
$message->addPart(
76-
$this->templating->render(
77-
'FrequenceWebContactBundle:Mails:mail.txt.twig',
78-
array('contact' => $contact)
66+
$this->twig->render(
67+
'@FrequenceWebContactBundle/Mails/mail.txt.twig',
68+
['contact' => $contact]
7969
),
8070
'text/plain'
8171
);

Resources/config/services.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
</parameters>
1212

1313
<services>
14-
<service id="frequence_web_contact.model" class="%frequence_web_contact.model.class%" />
14+
<service id="frequence_web_contact.model" class="%frequence_web_contact.model.class%" public="true" autowire="false" />
1515
<service id="frequence_web_contact.email_listener" class="%frequence_web_contact.email_listener.class%">
1616
<argument type="service" id="mailer" />
17-
<argument type="service" id="templating" />
17+
<argument type="service" id="twig" />
1818
<argument type="service" id="translator.default" />
1919
</service>
2020
</services>

Resources/views/Default/index.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends 'FrequenceWebContactBundle::layout.html.twig' %}
1+
{% extends '@FrequenceWebContactBundle/layout.html.twig' %}
22

33
{% block body %}
44
<form method="POST" action="{{ path('fw_contact_submit') }}">

Tests/Controller/DefaultControllerTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,42 @@
33
namespace FrequenceWeb\Bundle\ContactBundle\Tests\Controller;
44

55
use FrequenceWeb\Bundle\ContactBundle\Tests\Functional\WebTestCase;
6+
use Symfony\Component\HttpFoundation\RedirectResponse;
67

78
class DefaultControllerTest extends WebTestCase
89
{
9-
public function testIndex()
10+
public function testIndex(): void
1011
{
11-
/** @var $client \Symfony\Bundle\FrameworkBundle\Client */
1212
$client = static::createClient();
13-
/** @var $crawler \Symfony\Component\DomCrawler\Crawler */
13+
$client->enableProfiler();
1414
$crawler = $client->request('GET', '/contact.html');
1515

16+
file_put_contents(__DIR__ . '/../Functional/result.html', $client->getResponse()->getContent());
1617
$this->assertEquals(200, $client->getResponse()->getStatusCode());
1718

19+
1820
$form = $crawler->selectButton('contact_message_submit')->form();
19-
$form->setValues(array(
21+
$client->submit($form, [
2022
'contact[name]' => 'John Doe',
2123
'contact[subject]' => 'I have a message for you.',
2224
'contact[body]' => 'This is my message body.',
23-
));
24-
$client->submit($form);
25-
$collector = $client->getProfile()->getCollector('swiftmailer');
26-
$this->assertCount(0, $collector->getMessages());
25+
]);
2726

28-
$this->assertNotInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $client->getResponse());
27+
$this->assertNotInstanceOf(RedirectResponse::class, $client->getResponse());
2928

3029
$form = $crawler->selectButton('contact_message_submit')->form();
31-
$form->setValues(array(
30+
$client->submit($form, [
3231
'contact[name]' => 'John Doe',
3332
'contact[email]' => 'john.doe@gmail.com',
3433
'contact[subject]' => 'I have a message for you.',
3534
'contact[body]' => 'This is my message body.',
36-
));
35+
]);
3736
$client->submit($form);
3837

3938
$collector = $client->getProfile()->getCollector('swiftmailer');
4039
$this->assertCount(1, $collector->getMessages());
4140

42-
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $client->getResponse());
41+
$this->assertInstanceOf(RedirectResponse::class, $client->getResponse());
4342
$client->followRedirect();
4443
}
4544
}

Tests/Functional/AppKernel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ public function registerContainerConfiguration(LoaderInterface $loader)
3232
{
3333
$loader->load(__DIR__.'/config.yml');
3434
}
35+
36+
public function getProjectDir()
37+
{
38+
return __DIR__;
39+
}
3540
}

0 commit comments

Comments
 (0)