samedi 19 décembre 2015

Twig_Environment in Service Class leads to DIC RuntimeExtension only when unittesting in Symfony 2.8

I use Twig_Environment to render html mails to be sended. I have an NotificationService Class which is used by other services to send the mails.

In normal usage all is working, but since updating to 2.8 the unittest fail with: Symfony\Component\DependencyInjection\Exception\RuntimeException: You have requested a synthetic service ("kernel"). The DIC does not know how to construct this service

I debuged the StackTrace and the problem seems to be Twig_Environment (which uses the file_locator which injects kernel)

/**
 * Notification Service Class
 *
 * @DI\Service("app.service.notification")
 */
class NotificationService extends Generic
{
    /**
     * @var \Twig_Environment
     */
    protected $twig;

    /**
     * @var \Swift_Mailer
     */
    protected $swiftMailer;

    /**
     * @var string
     */
    protected $mailTemplate = 'VendorAdminBundle:Email:system2.html.twig';

    /**
     * @param \Swift_Mailer     $swiftMailer
     * @param \Twig_Environment $twig
     *
     * @DI\InjectParams({
     *      "swiftMailer" = @DI\Inject("mailer"),
     *      "twig"   = @DI\Inject("twig")
     * })
     */
    public function __construct(\Swift_Mailer $swiftMailer, \Twig_Environment $twig)
    {
        $this->twig          = $twig;
        $this->swiftMailer   = $swiftMailer;
    }

    /**
     * Send notification mail to Manager
     * @param UserEntity $manager
     * @param array      $contacts
     */
    public function notifyManager(UserEntity $manager, array $contacts)
    {
        $subject = 'Lorem Ipsum';
        $templateFile = "AppBundle:Email:notifyManager.html.twig";
        $templateContent = $this->twig->loadTemplate($templateFile);
        $body = $templateContent->render(array(
            'user'      => $manager,
            'contacts'  => $contacts,
            'subject'   => $subject,
        ));

        $this->sendMail($body, $subject, $manager);
    }
}

Any pointers on how to solve this?

Aucun commentaire:

Enregistrer un commentaire