You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
4.3 KiB
191 lines
4.3 KiB
11 years ago
|
<?php
|
||
|
|
||
11 years ago
|
namespace yiiunit\framework\mail;
|
||
11 years ago
|
|
||
|
use Yii;
|
||
|
use yii\base\View;
|
||
11 years ago
|
use yii\mail\BaseMailer;
|
||
|
use yii\mail\BaseMessage;
|
||
|
use yii\mail\ViewResolver;
|
||
11 years ago
|
use yii\helpers\FileHelper;
|
||
11 years ago
|
use yiiunit\TestCase;
|
||
|
|
||
|
/**
|
||
|
* @group email
|
||
|
*/
|
||
|
class BaseMailerTest extends TestCase
|
||
|
{
|
||
|
public function setUp()
|
||
|
{
|
||
|
$this->mockApplication();
|
||
|
Yii::$app->setComponent('email', $this->createTestEmailComponent());
|
||
11 years ago
|
$filePath = $this->getTestFilePath();
|
||
|
if (!file_exists($filePath)) {
|
||
|
FileHelper::createDirectory($filePath);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function tearDown()
|
||
|
{
|
||
|
$filePath = $this->getTestFilePath();
|
||
|
if (file_exists($filePath)) {
|
||
|
FileHelper::removeDirectory($filePath);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return string test file path.
|
||
|
*/
|
||
|
protected function getTestFilePath()
|
||
|
{
|
||
|
return Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid();
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* @return Mailer test email component instance.
|
||
|
*/
|
||
|
protected function createTestEmailComponent()
|
||
|
{
|
||
|
$component = new Mailer();
|
||
|
return $component;
|
||
|
}
|
||
|
|
||
|
// Tests :
|
||
|
|
||
|
public function testSetupView()
|
||
|
{
|
||
|
$mailer = new Mailer();
|
||
|
|
||
|
$view = new View();
|
||
|
$mailer->setView($view);
|
||
|
$this->assertEquals($view, $mailer->getView(), 'Unable to setup view!');
|
||
11 years ago
|
|
||
|
$viewConfig = [
|
||
|
'params' => [
|
||
|
'param1' => 'value1',
|
||
|
'param2' => 'value2',
|
||
|
]
|
||
|
];
|
||
|
$mailer->setView($viewConfig);
|
||
|
$view = $mailer->getView();
|
||
|
$this->assertTrue(is_object($view), 'Unable to setup view via config!');
|
||
|
$this->assertEquals($viewConfig['params'], $view->params, 'Unable to configure view via config array!');
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* @depends testSetupView
|
||
|
*/
|
||
11 years ago
|
public function testGetDefaultView()
|
||
|
{
|
||
|
$mailer = new Mailer();
|
||
|
$view = $mailer->getView();
|
||
|
$this->assertTrue(is_object($view), 'Unable to get default view!');
|
||
|
}
|
||
|
|
||
11 years ago
|
public function testSetupViewResolver()
|
||
|
{
|
||
|
$mailer = new Mailer();
|
||
|
|
||
|
$viewResolver = new ViewResolver();
|
||
|
$mailer->setViewResolver($viewResolver);
|
||
|
$this->assertEquals($viewResolver, $mailer->getViewResolver(), 'Unable to setup view resolver!');
|
||
|
|
||
|
$viewResolverConfig = [
|
||
|
'viewPath' => '/test/view/path',
|
||
|
];
|
||
|
$mailer->setViewResolver($viewResolverConfig);
|
||
|
$viewResolver = $mailer->getViewResolver();
|
||
|
$this->assertTrue(is_object($viewResolver), 'Unable to setup view resolver via config!');
|
||
|
$this->assertEquals($viewResolverConfig['viewPath'], $viewResolver->viewPath, 'Unable to configure view resolver via config array!');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testSetupViewResolver
|
||
|
*/
|
||
|
public function testGetDefaultViewResolver()
|
||
|
{
|
||
|
$mailer = new Mailer();
|
||
|
$viewResolver = $mailer->getViewResolver();
|
||
|
$this->assertTrue(is_object($viewResolver), 'Unable to get default view resolver!');
|
||
|
}
|
||
|
|
||
11 years ago
|
public function testDefaultMessageConfig()
|
||
|
{
|
||
|
$defaultMessageConfig = array(
|
||
|
'id' => 'test-id',
|
||
|
'encoding' => 'test-encoding',
|
||
|
);
|
||
|
Yii::$app->getComponent('email')->setDefaultMessageConfig($defaultMessageConfig);
|
||
|
|
||
|
$message = new Message();
|
||
|
|
||
|
foreach ($defaultMessageConfig as $name => $value) {
|
||
|
$this->assertEquals($value, $message->$name);
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
|
* @depends testGetDefaultView
|
||
|
* @depends testGetDefaultViewResolver
|
||
|
*/
|
||
|
public function testRender()
|
||
|
{
|
||
|
$mailer = new Mailer();
|
||
|
|
||
|
$filePath = $this->getTestFilePath();
|
||
|
$mailer->getViewResolver()->viewPath = $filePath;
|
||
|
|
||
|
$viewName = 'test_view';
|
||
|
$fileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php';
|
||
|
$fileContent = '<?php echo $testParam; ?>';
|
||
|
file_put_contents($fileName, $fileContent);
|
||
|
|
||
|
$params = [
|
||
|
'testParam' => 'test output'
|
||
|
];
|
||
|
$renderResult = $mailer->render($viewName, $params);
|
||
|
$this->assertEquals($params['testParam'], $renderResult);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Test Mailer class
|
||
|
*/
|
||
|
class Mailer extends BaseMailer
|
||
|
{
|
||
|
public $sentMessages = array();
|
||
|
|
||
|
public function send($message)
|
||
|
{
|
||
|
$this->sentMessages[] = $message;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test Message class
|
||
|
*/
|
||
|
class Message extends BaseMessage
|
||
|
{
|
||
|
public $id;
|
||
|
public $encoding;
|
||
|
|
||
|
public function setFrom($from) {}
|
||
|
|
||
|
public function setTo($to) {}
|
||
|
|
||
11 years ago
|
public function setCc($cc) {}
|
||
|
|
||
|
public function setBcc($bcc) {}
|
||
|
|
||
11 years ago
|
public function setSubject($subject) {}
|
||
|
|
||
|
public function setText($text) {}
|
||
|
|
||
|
public function setHtml($html) {}
|
||
|
|
||
11 years ago
|
public function addText($text) {}
|
||
|
|
||
|
public function addHtml($html) {}
|
||
|
|
||
11 years ago
|
public function createAttachment($content, $fileName, $contentType = 'application/octet-stream') {}
|
||
|
}
|