Skip to content

Commit

Permalink
Override factory + debug test
Browse files Browse the repository at this point in the history
  • Loading branch information
snoob committed Feb 24, 2015
1 parent 6d4f538 commit 5c1d23d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 25 deletions.
37 changes: 37 additions & 0 deletions Exception/ReferenceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Lexik\Bundle\MailerBundle\Exception;

/**
* @author Yannick Snobbert <[email protected]>
*/
class ReferenceNotFoundException extends \Exception
{
/**
* @var string
*/
private $reference;

/**
* @param string $reference
* @param string $message
* @param int $code
* @param \Exception
*/
public function __construct($reference, $message = null, \Exception $previous = null, $code = 0)
{
$this->reference = $reference;

parent::__construct($message, $code, $previous);
}

/**
* Get reference
*
* @return string
*/
public function getReference()
{
return $this->reference;
}
}
37 changes: 28 additions & 9 deletions Message/MessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Lexik\Bundle\MailerBundle\Message;

use Lexik\Bundle\MailerBundle\Entity\EmailTranslation;
use Doctrine\ORM\EntityManager;
use Lexik\Bundle\MailerBundle\Exception\ReferenceNotFoundException;
use Lexik\Bundle\MailerBundle\Model\EmailInterface;
use Lexik\Bundle\MailerBundle\Mapping\Driver\Annotation;
use Lexik\Bundle\MailerBundle\Exception\NoTranslationException;
Expand All @@ -12,8 +13,6 @@
use Lexik\Bundle\MailerBundle\Message\TwigErrorMessage;
use Lexik\Bundle\MailerBundle\Signer\SignerFactory;

use Doctrine\ORM\EntityManager;

/**
* Create some swift messages from email templates.
*
Expand All @@ -28,7 +27,7 @@ class MessageFactory
protected $em;

/**
* @var MessageRender
* @var MessageRenderer
*/
protected $renderer;

Expand Down Expand Up @@ -87,6 +86,30 @@ protected function getDefaultOptions()
);
}

/**
* Find an email from database
*
* @param string $reference
* @param bool $throwException
*
* @throws ReferenceNotFoundException
* @return EmailInterface|null
*/
public function getEmail($reference, $throwException = false)
{
if (!isset($this->emails[$reference])) {
$this->emails[$reference] = $this->em->getRepository($this->options['email_class'])->findOneByReference($reference);
}

$email = $this->emails[$reference];

if (true === $throwException) {
throw new ReferenceNotFoundException($reference, sprintf('Reference "%s" does not exist for email.', $reference));
}

return $email;
}

/**
* Find an email template and create a swift message.
*
Expand All @@ -100,11 +123,7 @@ protected function getDefaultOptions()
*/
public function get($reference, $to, array $parameters = array(), $locale = null)
{
if (!isset($this->emails[$reference])) {
$this->emails[$reference] = $this->em->getRepository($this->options['email_class'])->findOneByReference($reference);
}

$email = $this->emails[$reference];
$email = $this->getEmail($reference);

if ($email instanceof EmailInterface) {
return $this->generateMessage($email, $to, $parameters, $locale);
Expand Down
27 changes: 19 additions & 8 deletions Tests/Unit/Message/MessageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Lexik\Bundle\MailerBundle\Tests\Message;

use Lexik\Bundle\MailerBundle\Exception\ReferenceNotFoundException;
use Lexik\Bundle\MailerBundle\Twig\Loader\EmailLoader;

use Lexik\Bundle\MailerBundle\Message\MessageRenderer;
use Lexik\Bundle\MailerBundle\Message\MessageFactory;
use Lexik\Bundle\MailerBundle\Tests\Unit\BaseUnitTestCase;
Expand Down Expand Up @@ -53,16 +53,11 @@ public function testGetErrorMessages()
$file = __FILE__;
$body = <<<EOF
An error occurred while trying to send an email.
You tried to use a reference that does not exist : "this-reference-does-not-exixt"
You tried to use a reference that does not exist : "this-reference-does-not-exist"
in "{$file}" at line 60
EOF;

$message = $factory->get('this-reference-does-not-exixt', '[email protected]', array('name' => 'chuck'));
$this->assertInstanceOf('Swift_Message', $message);
$this->assertEquals(array('[email protected]' => null), $message->getTo());
$this->assertEquals('An exception occurred', $message->getSubject());
$this->assertEquals($body, $message->getBody());
$this->assertEquals(array('[email protected]' => null), $message->getFrom());
$message = $factory->get('this-reference-does-not-exist', '[email protected]', array('name' => 'chuck'));

// no translation found
$body = <<<EOF
Expand Down Expand Up @@ -125,4 +120,20 @@ protected function createMessageFactory()

return new MessageFactory($this->em, $renderer, $annotationDriver, $options, $signerFactory);
}

public function testGetEmail()
{
$factory = $this->createMessageFactory();

$email = $factory->getEmail('rabbids-template', false);
$this->assertInstanceOf('Lexik\Bundle\MailerBundle\Model\EmailInterface', $email);
$this->assertEquals($email->getReference(), 'rabbids-template');

$email = $factory->getEmail('this-reference-does-not-exist', false);
$this->assertNull($email);

$this->setExpectedException('Lexik\Bundle\MailerBundle\Exception\ReferenceNotFoundException');
$factory = $this->createMessageFactory();
$factory->getEmail('this-reference-does-not-exist', true);
}
}
10 changes: 2 additions & 8 deletions Tests/autoload.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ if (!$loader = include __DIR__.'/../vendor/autoload.php') {
'php composer.phar install'.$nl);
}

AnnotationRegistry::registerAutoloadNamespace(
'Symfony\Component\Validator\Constraints',
__DIR__.'/../vendor/symfony/validator'
);
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

AnnotationRegistry::registerAutoloadNamespace(
'Symfony\Bridge\Doctrine\Validator\Constraints',
__DIR__.'/../vendor/symfony/doctrine-bridge'
);
return $loader;
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"doctrine/orm": "~2.2",
"twig/twig": ">=1.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"doctrine/doctrine-fixtures-bundle": "~2.2"
},
"autoload": {
"psr-0": { "Lexik\\Bundle\\MailerBundle": "" }
},
Expand Down

0 comments on commit 5c1d23d

Please sign in to comment.