<?php

require_once dirname(__FILE__) . '/../../bootstrap/functional.php';
# load fixtures of this plugin
$propelData->loadData(sfConfig::get('sf_plugins_dir') . '/sfNewsletterPlugin/data/fixtures');
$browser = new sfTestFunctional(new sfBrowser());
$limeTest = $browser->test();
$limeTest->plan += 1;
$newsletter = NewsletterPeer::retrieveByName('second newsletter');
$limeTest->isa_ok($newsletter, 'Newsletter', 'Newsletter found.');
$browser->getAndCheck('sfNewsletter', 'read', '/newsletter/read/' . 0, 404);
$browser->getAndCheck('sfNewsletter', 'read', '/newsletter/read/' . $newsletter->getId(), 200);
$browser->responseContains($newsletter->getContent());
require_once dirname(__FILE__) . '/../bootstrap/task.php';
# load fixtures of this plugin
$propelData->loadData(sfConfig::get('sf_plugins_dir') . '/sfNewsletterPlugin/data/fixtures');
$limeTest = new lime_test(6, new lime_output_color());
sfConfig::set('sf_newsletterplugin_from', '');
$task = new SendScheduledNewsletterTask($dispatcher, $formatter);
try {
    $task->run(array(), array());
    $limeTest->fail('InvalidArgumentException not thrown.');
} catch (InvalidArgumentException $e) {
    $limeTest->is($e->getMessage(), sfNewsletterPluginConfiguration::EXCEPTION_NO_SENDER, 'Caught correct Exception.');
}
sfConfig::set('sf_newsletterplugin_from', 'invalid-email');
try {
    $task->run(array(), array());
    $limeTest->fail('InvalidArgumentException not thrown.');
} catch (InvalidArgumentException $e) {
    $limeTest->like($e->getMessage(), '/' . sfNewsletterPluginConfiguration::EXCEPTION_INVALID_SENDER . '/', 'Caught correct Exception.');
}
sfConfig::set('sf_newsletterplugin_from', '*****@*****.**');
$task->run(array(), array());
$logs = $logger->getLogEntries();
$limeTest->like($logs[2], '/There are no newsletters on schedule./', 'Task exits while no newsletter are given.');
$newsletter = NewsletterPeer::retrieveByName('first newsletter');
$limeTest->ok($newsletter->setScheduledAt(new DateTime('-7 hours'))->save(), 'Scheduled Newsletter.');
$limeTest->is(count(NewsletterPeer::retrieveScheduled(new DateTime('-6 hours'))), 1, 'Found scheduled Newsletter.');
$task->run(array(), array('schedule="-6 hours"'));
$logs = $logger->getLogEntries();
$limeTest->unlike($logs[3], '/' . sfNewsletterPluginConfiguration::EXCEPTION_SWIFT_ERROR . '/', 'Email sent successfully.');
// @todo Add test checking POP3 to verify the email really got there!