-
Notifications
You must be signed in to change notification settings - Fork 67
SymfonyMailerContext #112
base: refactoring/new-extension
Are you sure you want to change the base?
SymfonyMailerContext #112
Changes from all commits
aba9e3e
6ddcdc7
d9f8377
c5737ab
b28a1c3
3395ffc
b42e9f3
ec57260
3a01b71
0c58d77
18d358e
39bd46e
8511969
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/bin/ | ||
/vendor/ | ||
composer.lock | ||
features/fixtures/tmp |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Page; | ||
|
||
use Knp\FriendlyExtension\Page\Page; | ||
|
||
class EmailPage extends Page | ||
{ | ||
public function getPath() | ||
{ | ||
return '/mailer/email'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Page; | ||
|
||
use Knp\FriendlyExtension\Page\Page; | ||
|
||
class NoEmailsPage extends Page | ||
{ | ||
public function getPath() | ||
{ | ||
return '/mailer/no-emails'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class App extends Bundle | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class DefaultController extends Controller | ||
{ | ||
public function indexAction() | ||
{ | ||
return new Response(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class MailerController extends Controller | ||
{ | ||
public function noEmailsAction() | ||
{ | ||
return new Response(); | ||
} | ||
|
||
public function emailAction() | ||
{ | ||
$this->get('mailer')->send(\Swift_Message::newInstance() | ||
->setSubject('Hello Subject') | ||
->setFrom('send@example.com') | ||
->setTo('recipient@example.com') | ||
->setBody('Hello Body.') | ||
); | ||
|
||
return new Response(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
homepage: | ||
pattern: / | ||
defaults: { _controller: 'App:Default:index' } | ||
|
||
mailer_no_emails: | ||
pattern: /mailer/no-emails | ||
defaults: { _controller: 'App:Mailer:noEmails' } | ||
|
||
mailer_email_with_subject: | ||
pattern: /mailer/email | ||
defaults: { _controller: 'App:Mailer:email' } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpKernel\Kernel; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
|
||
class AppKernel extends Kernel | ||
{ | ||
public function registerBundles() | ||
{ | ||
return array( | ||
new Symfony\Bundle\FrameworkBundle\FrameworkBundle, | ||
new Symfony\Bundle\TwigBundle\TwigBundle, | ||
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle, | ||
new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle, | ||
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle, | ||
new App\App | ||
); | ||
} | ||
|
||
public function registerContainerConfiguration(LoaderInterface $loader) | ||
{ | ||
$loader->load(function($container) { | ||
$container->loadFromExtension('framework', array( | ||
'router' => array('resource' => __DIR__.'/App/Resources/config/routing.yml'), | ||
'templating' => array( | ||
'engines' => array('twig'), | ||
), | ||
'profiler' => array('enabled' => true), | ||
)); | ||
|
||
$container->loadFromExtension('doctrine', array( | ||
'orm' => array(), | ||
'dbal' => array(), | ||
)); | ||
|
||
$container->loadFromExtension('swiftmailer', array( | ||
'disable_delivery' => true, | ||
)); | ||
}); | ||
} | ||
|
||
protected function getKernelParameters() | ||
{ | ||
$parameters = parent::getKernelParameters(); | ||
$parameters['kernel.secret'] = 'secret!'; | ||
|
||
return $parameters; | ||
} | ||
|
||
public function getCacheDir() | ||
{ | ||
return $this->rootDir.'/tmp/cache/'.$this->name.$this->environment; | ||
} | ||
|
||
public function getLogDir() | ||
{ | ||
return $this->rootDir.'/tmp/logs'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
$loader = require __DIR__.'/../../vendor/autoload.php'; | ||
$loader->add('App', __DIR__); | ||
|
||
require __DIR__.'/AppKernel.php'; | ||
|
||
$kernel = new AppKernel('test', true); | ||
$request = Request::createFromGlobals(); | ||
$response = $kernel->handle($request); | ||
$response->send(); | ||
$kernel->terminate($request, $response); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
Feature: I am able to describe sending emails | ||
|
||
Scenario: No emails sent | ||
When I go to the noEmails page | ||
Then no email should have been sent | ||
And 0 emails should have been sent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0 email There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's 0 emails. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cursedcoder I think your link is about saying "zeros" or "zeroes" and not how to accord the noun after zero. But maybe I'm missing something. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anyway, @umpirsky seems right on this one ;) http://english.stackexchange.com/a/13075 |
||
|
||
Scenario: Email with subject sent | ||
When I go to the email page | ||
Then email with subject "Hello Subject" should have been sent | ||
And 1 emails should have been sent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 email There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree |
||
|
||
Scenario: Email with subject not sent | ||
When I go to the email page | ||
And email with subject "Not sent" should not be sent | ||
|
||
Scenario: Email sent to recipeient | ||
When I go to the email page | ||
Then email should have been sent to "recipient@example.com" | ||
|
||
Scenario: Email not sent to recipeient | ||
When I go to the email page | ||
Then email should not be sent to "umpirsky@gmail.com" | ||
|
||
Scenario: Email with subject sent to recipeient | ||
When I go to the email page | ||
Then email with subject "Hello Subject" should have been sent to "recipient@example.com" | ||
And the following emails should have been sent: | ||
| subject | recipient | | ||
| Hello Subject | recipient@example.com | | ||
|
||
Scenario: Email with subject not sent to recipeient | ||
When I go to the email page | ||
Then email with subject "Not sent" should not be sent to "recipient@example.com" | ||
And email with subject "Hello Subject" should not be sent to "umpirsky@gmail.com" | ||
And email with subject "Not sent" should not be sent to "umpirsky@gmail.com" | ||
And the following emails should not be sent: | ||
| subject | recipient | | ||
| Not sent | recipient@example.com | | ||
| Hello Subject | umpirsky@gmail.com | | ||
| Not sent | umpirsky@gmail.com | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Knp\FriendlyExtension\Context\Helper; | ||
|
||
use Knp\FriendlyExtension\Context\Helper\AbstractHelper; | ||
use Symfony\Component\HttpKernel\Profiler\Profiler; | ||
|
||
class ProfilerHelper extends AbstractHelper | ||
{ | ||
private $profiler; | ||
|
||
public function __construct(Profiler $profiler) | ||
{ | ||
$this->profiler = $profiler; | ||
} | ||
|
||
public function getProfile($token = null) | ||
{ | ||
if (null === $token) { | ||
$headers = $this->get('mink')->getSession()->getResponseHeaders(); | ||
|
||
if (!isset($headers['X-Debug-Token']) && !isset($headers['x-debug-token'])) { | ||
throw new \RuntimeException('Debug-Token not found in response headers. Have you turned on the debug flag?'); | ||
} | ||
|
||
$token = isset($headers['X-Debug-Token']) ? $headers['X-Debug-Token'] : $headers['x-debug-token']; | ||
if (is_array($token)) { | ||
$token = end($token); | ||
} | ||
} | ||
|
||
return $this->profiler->loadProfile($token); | ||
} | ||
|
||
public function getCollector($name, $token = null) | ||
{ | ||
return $this->getProfile($token)->getCollector($name); | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'profiler'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Knp\FriendlyExtension\Context\Helper; | ||
|
||
use Knp\FriendlyExtension\Context\Helper\AbstractHelper; | ||
|
||
class SwiftMailerHelper extends AbstractHelper | ||
{ | ||
public function countEmailsSent() | ||
{ | ||
return $this->getCollector()->getMessageCount(); | ||
} | ||
|
||
public function isEmailSent($subject = null, $to = null) | ||
{ | ||
foreach ($this->getCollector()->getMessages('default') as $message) { | ||
if ((null === $subject || $subject === trim($message->getSubject())) && | ||
(null === $to || array_key_exists($to, $message->getTo())) | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function getCollector() | ||
{ | ||
return $this->get('profiler')->getCollector('swiftmailer'); | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'swiftmailer'; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done with the micro kernel :) .