Commit 34ddab52 authored by Ricard Clau's avatar Ricard Clau

add a test for swiftmailerServiceProvider app->finish event

parent 5d599be3
......@@ -14,6 +14,9 @@ namespace Silex\Tests\Provider;
use Silex\Application;
use Silex\Provider\SwiftmailerServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
......@@ -32,4 +35,54 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('Swift_Mailer', $app['mailer']);
}
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());
});
$test = $this;
/**
* This gets executed before SwiftmailerServiceProvider $app->finish thanks to higher priority
* flushQueue should return 1 if spool has not been flushed
*/
$app->finish(function() use ($app, $test) {
$test->assertEquals(1, $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']));
/**
* We add a new message that should be flushed with $app->finish()
*/
$app['mailer']->send(\Swift_Message::newInstance());
}, 1);
/**
* This gets executed after SwiftmailerServiceProvider $app->finish thanks to higher priority
* flushQueue should return 0 even having added a message in method above
*/
$app->finish(function() use ($app, $test) {
$test->assertEquals(0, $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']));
}, -1);
$app->get('/', function() use ($app) {
$app['mailer']->send(\Swift_Message::newInstance());
return new SendMailsResponse('should send e-mails');
});
$request = Request::create('/');
$app->run($request);
}
}
class SendMailsResponse extends Response
{
public function send()
{
// do nothing
}
}
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