Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

SymfonyMailerContext #112

Open
wants to merge 13 commits into
base: refactoring/new-extension
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/bin/
/vendor/
composer.lock
features/fixtures/tmp
7 changes: 5 additions & 2 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ default:
suites:
default:
path: %paths.base%/features
contexts:
contexts:
- Context\FeatureContext
- Knp\FriendlyExtension\Context\PageContext
- Knp\FriendlyExtension\Context\MinkContext
- Knp\FriendlyExtension\Context\ApiContext
- Knp\FriendlyExtension\Context\SymfonyMailerContext
extensions:
Knp\FriendlyExtension:
Knp\FriendlyExtension:
smart_tag: 'smart-step'
screenshot:
recipents:
- pierre.plazanet@knplabs.com
mailer:
username: smtp.knplabs@gmail.com
password: loremipsum
symfony_kernel:
bootstrap: features/fixtures/index.php
Behat\MinkExtension:
base_url: 'http://localhost:8080/'
goutte: ~
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"require-dev": {
"symfony/http-kernel": "~2.0",
"symfony/framework-bundle": "~2.0",
"doctrine/common": "~2.0",
"symfony/swiftmailer-bundle": "~2.0",
"symfony/web-profiler-bundle": "~2.0",
"symfony/twig-bundle": "~2.0",
"symfony/form": "~2.0",
"doctrine/doctrine-bundle": "~1.2",
"doctrine/data-fixtures": "~1.0",
"doctrine/orm": "~2.0",
"doctrine/inflector": "~1.0",
"behat/mink-extension": "~2.0",
"behat/mink-goutte-driver": "~1.0",
"behat/mink-selenium2-driver": "~1.0",
Expand Down
13 changes: 13 additions & 0 deletions features/Page/EmailPage.php
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';
}
}
13 changes: 13 additions & 0 deletions features/Page/NoEmailsPage.php
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';
}
}
9 changes: 9 additions & 0 deletions features/fixtures/App/App.php
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
{
}
14 changes: 14 additions & 0 deletions features/fixtures/App/Controller/DefaultController.php
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();
}
}
26 changes: 26 additions & 0 deletions features/fixtures/App/Controller/MailerController.php
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();
}
}
11 changes: 11 additions & 0 deletions features/fixtures/App/Resources/config/routing.yml
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' }
59 changes: 59 additions & 0 deletions features/fixtures/AppKernel.php
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
Copy link
Contributor

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 :) .

{
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';
}
}
14 changes: 14 additions & 0 deletions features/fixtures/index.php
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);
41 changes: 41 additions & 0 deletions features/mailer.feature
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 email

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's 0 emails. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 email

Copy link
Contributor

Choose a reason for hiding this comment

The 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 |
1 change: 0 additions & 1 deletion src/Knp/FriendlyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function getConfigKey()

public function initialize(ExtensionManager $extensionManager)
{

}

public function configure(ArrayNodeDefinition $builder)
Expand Down
44 changes: 44 additions & 0 deletions src/Knp/FriendlyExtension/Context/Helper/ProfilerHelper.php
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';
}
}
36 changes: 36 additions & 0 deletions src/Knp/FriendlyExtension/Context/Helper/SwiftMailerHelper.php
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';
}
}
Loading