Commit f43dd5a5 authored by Igor Wiedler's avatar Igor Wiedler

Fix SwiftmailerServiceProviderTest (stub the spool)

parent 1798d08f
......@@ -9,7 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Silex Test Suite">
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Provider;
class SpoolStub implements \Swift_Spool
{
private $messages = array();
public function getMessages()
{
return $this->messages;
}
public function start()
{
}
public function stop()
{
}
public function isStarted()
{
return count($this->messages) > 0;
}
public function queueMessage(\Swift_Mime_Message $message)
{
$this->messages[] = $message;
}
public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null)
{
$this->messages = array();
}
}
......@@ -29,6 +29,7 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
public function testSwiftMailerServiceIsSwiftMailer()
{
$app = new Application();
$app->register(new SwiftmailerServiceProvider(), array(
'swiftmailer.class_path' => __DIR__.'/../../../../vendor/swiftmailer/swiftmailer/lib/classes',
));
......@@ -39,43 +40,25 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
public function testSwiftMailerSendsMailsOnFinish()
{
$app = new Application();
$app->register(new SwiftmailerServiceProvider(), array(
'swiftmailer.class_path' => __DIR__.'/../../../../vendor/swiftmailer/swiftmailer/lib/classes',
));
$app['swiftmailer.transport'] = $app->share(function () use ($app) {
return new \Swift_Transport_SpoolTransport($app['swiftmailer.transport.eventdispatcher'], new \Swift_MemorySpool());
});
$spool = new SpoolStub();
$app['swiftmailer.spooltransport'] = new \Swift_SpoolTransport($spool);
$app->get('/', function() use ($app) {
$app['mailer']->send(\Swift_Message::newInstance());
});
/**
* Checks spool is empty before process
*/
$this->assertEquals(0, $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']));
$this->assertCount(0, $spool->getMessages());
$request = Request::create('/');
$app->handle($request);
/**
* Checks spool has the message that is sent in controller and regenerates it
*/
$this->assertEquals(1, $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']));
$app['mailer']->send(\Swift_Message::newInstance());
/**
* Terminates app and checks that spool is empty
*/
$app->terminate($request, new SendMailsResponse('should send e-mails'));
$this->assertEquals(0, $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']));
}
}
$response = $app->handle($request);
$this->assertCount(1, $spool->getMessages());
class SendMailsResponse extends Response
{
public function send()
{
// do nothing
$app->terminate($request, $response);
$this->assertCount(0, $spool->getMessages());
}
}
<?php
$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add('Silex\Tests', __DIR__);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment