Browse Source

Added unit tests

tags/2.0.4
Klimov Paul 10 years ago
parent
commit
123b6f9d41
  1. 30
      .gitignore
  2. 53
      .travis.yml
  3. 13
      phpunit.xml.dist
  4. 124
      tests/MailerTest.php
  5. 365
      tests/MessageTest.php
  6. 50
      tests/TestCase.php
  7. 15
      tests/bootstrap.php
  8. 3
      tests/runtime/.gitignore
  9. 0
      tests/runtime/coveralls/.gitkeep

30
.gitignore vendored

@ -0,0 +1,30 @@
# phpstorm project files
.idea
# netbeans project files
nbproject
# zend studio for eclipse project files
.buildpath
.project
.settings
# windows thumbnail cache
Thumbs.db
# composer vendor dir
/vendor
# composer itself is not needed
composer.phar
# Mac DS_Store Files
.DS_Store
# phpunit itself is not needed
phpunit.phar
# local phpunit config
/phpunit.xml
# local tests configuration
/tests/data/config.local.php

53
.travis.yml

@ -0,0 +1,53 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
- hhvm-nightly
# run build against hhvm but allow them to fail
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
matrix:
fast_finish: true
allow_failures:
- php: hhvm-nightly
- php: 7.0
services:
- mongodb
# faster builds on new travis setup not using sudo
sudo: false
# cache vendor dirs
cache:
directories:
- vendor
- $HOME/.composer/cache
install:
- travis_retry composer self-update && composer --version
- travis_retry composer global require "fxp/composer-asset-plugin:1.0.0"
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction
before_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
fi
script:
- vendor/bin/phpunit --verbose $PHPUNIT_FLAGS
after_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
cd ../../..
travis_retry wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
fi

13
phpunit.xml.dist

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Yii2-swiftmailer Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>

124
tests/MailerTest.php

@ -0,0 +1,124 @@
<?php
namespace yiiunit\extensions\swiftmailer;
use Yii;
use yii\swiftmailer\Mailer;
Yii::setAlias('@yii/swiftmailer', __DIR__ . '/../../../../extensions/swiftmailer');
/**
* @group vendor
* @group mail
* @group swiftmailer
*/
class MailerTest extends TestCase
{
public function setUp()
{
$this->mockApplication([
'components' => [
'email' => $this->createTestEmailComponent()
]
]);
}
/**
* @return Mailer test email component instance.
*/
protected function createTestEmailComponent()
{
$component = new Mailer();
return $component;
}
// Tests :
public function testSetupTransport()
{
$mailer = new Mailer();
$transport = \Swift_MailTransport::newInstance();
$mailer->setTransport($transport);
$this->assertEquals($transport, $mailer->getTransport(), 'Unable to setup transport!');
}
/**
* @depends testSetupTransport
*/
public function testConfigureTransport()
{
$mailer = new Mailer();
$transportConfig = [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
];
$mailer->setTransport($transportConfig);
$transport = $mailer->getTransport();
$this->assertTrue(is_object($transport), 'Unable to setup transport via config!');
$this->assertEquals($transportConfig['class'], get_class($transport), 'Invalid transport class!');
$this->assertEquals($transportConfig['host'], $transport->getHost(), 'Invalid transport host!');
}
/**
* @depends testConfigureTransport
*/
public function testConfigureTransportConstruct()
{
$mailer = new Mailer();
$class = 'Swift_SmtpTransport';
$host = 'some.test.host';
$port = 999;
$transportConfig = [
'class' => $class,
'constructArgs' => [
$host,
$port,
],
];
$mailer->setTransport($transportConfig);
$transport = $mailer->getTransport();
$this->assertTrue(is_object($transport), 'Unable to setup transport via config!');
$this->assertEquals($class, get_class($transport), 'Invalid transport class!');
$this->assertEquals($host, $transport->getHost(), 'Invalid transport host!');
$this->assertEquals($port, $transport->getPort(), 'Invalid transport host!');
}
/**
* @depends testConfigureTransportConstruct
*/
public function testConfigureTransportWithPlugins()
{
$mailer = new Mailer();
$pluginClass = 'Swift_Plugins_ThrottlerPlugin';
$rate = 10;
$transportConfig = [
'class' => 'Swift_SmtpTransport',
'plugins' => [
[
'class' => $pluginClass,
'constructArgs' => [
$rate,
],
],
],
];
$mailer->setTransport($transportConfig);
$transport = $mailer->getTransport();
$this->assertTrue(is_object($transport), 'Unable to setup transport via config!');
$this->assertContains(':' . $pluginClass . ':', print_r($transport, true), 'Plugin not added');
}
public function testGetSwiftMailer()
{
$mailer = new Mailer();
$this->assertTrue(is_object($mailer->getSwiftMailer()), 'Unable to get Swift mailer instance!');
}
}

365
tests/MessageTest.php

@ -0,0 +1,365 @@
<?php
namespace yiiunit\extensions\swiftmailer;
use Yii;
use yii\helpers\FileHelper;
use yii\swiftmailer\Mailer;
use yii\swiftmailer\Message;
Yii::setAlias('@yii/swiftmailer', __DIR__ . '/../../../../extensions/swiftmailer');
/**
* @group vendor
* @group mail
* @group swiftmailer
*/
class MessageTest extends TestCase
{
/**
* @var string test email address, which will be used as receiver for the messages.
*/
protected $testEmailReceiver = 'someuser@somedomain.com';
public function setUp()
{
$this->mockApplication([
'components' => [
'mailer' => $this->createTestEmailComponent()
]
]);
$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/extensions/swiftmailer/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid();
}
/**
* @return Mailer test email component instance.
*/
protected function createTestEmailComponent()
{
$component = new Mailer([
'useFileTransport' => true,
]);
return $component;
}
/**
* @return Message test message instance.
*/
protected function createTestMessage()
{
return Yii::$app->get('mailer')->compose();
}
/**
* Creates image file with given text.
* @param string $fileName file name.
* @param string $text text to be applied on image.
* @return string image file full name.
*/
protected function createImageFile($fileName = 'test.jpg', $text = 'Test Image')
{
if (!function_exists('imagecreatetruecolor')) {
$this->markTestSkipped('GD lib required.');
}
$fileFullName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $fileName;
$image = imagecreatetruecolor(120, 20);
$textColor = imagecolorallocate($image, 233, 14, 91);
imagestring($image, 1, 5, 5, $text, $textColor);
imagejpeg($image, $fileFullName);
imagedestroy($image);
return $fileFullName;
}
/**
* Finds the attachment object in the message.
* @param Message $message message instance
* @return null|\Swift_Mime_Attachment attachment instance.
*/
protected function getAttachment(Message $message)
{
$messageParts = $message->getSwiftMessage()->getChildren();
$attachment = null;
foreach ($messageParts as $part) {
if ($part instanceof \Swift_Mime_Attachment) {
$attachment = $part;
break;
}
}
return $attachment;
}
// Tests :
public function testGetSwiftMessage()
{
$message = new Message();
$this->assertTrue(is_object($message->getSwiftMessage()), 'Unable to get Swift message!');
}
/**
* @depends testGetSwiftMessage
*/
public function testSetGet()
{
$message = new Message();
$charset = 'utf-16';
$message->setCharset($charset);
$this->assertEquals($charset, $message->getCharset(), 'Unable to set charset!');
$subject = 'Test Subject';
$message->setSubject($subject);
$this->assertEquals($subject, $message->getSubject(), 'Unable to set subject!');
$from = 'from@somedomain.com';
$message->setFrom($from);
$this->assertContains($from, array_keys($message->getFrom()), 'Unable to set from!');
$replyTo = 'reply-to@somedomain.com';
$message->setReplyTo($replyTo);
$this->assertContains($replyTo, array_keys($message->getReplyTo()), 'Unable to set replyTo!');
$to = 'someuser@somedomain.com';
$message->setTo($to);
$this->assertContains($to, array_keys($message->getTo()), 'Unable to set to!');
$cc = 'ccuser@somedomain.com';
$message->setCc($cc);
$this->assertContains($cc, array_keys($message->getCc()), 'Unable to set cc!');
$bcc = 'bccuser@somedomain.com';
$message->setBcc($bcc);
$this->assertContains($bcc, array_keys($message->getBcc()), 'Unable to set bcc!');
}
/**
* @depends testGetSwiftMessage
*/
public function testSetupHeaders()
{
$charset = 'utf-16';
$subject = 'Test Subject';
$from = 'from@somedomain.com';
$replyTo = 'reply-to@somedomain.com';
$to = 'someuser@somedomain.com';
$cc = 'ccuser@somedomain.com';
$bcc = 'bccuser@somedomain.com';
$messageString = $this->createTestMessage()
->setCharset($charset)
->setSubject($subject)
->setFrom($from)
->setReplyTo($replyTo)
->setTo($to)
->setCc($cc)
->setBcc($bcc)
->toString();
$this->assertContains('charset=' . $charset, $messageString, 'Incorrect charset!');
$this->assertContains('Subject: ' . $subject, $messageString, 'Incorrect "Subject" header!');
$this->assertContains('From: ' . $from, $messageString, 'Incorrect "From" header!');
$this->assertContains('Reply-To: ' . $replyTo, $messageString, 'Incorrect "Reply-To" header!');
$this->assertContains('To: ' . $to, $messageString, 'Incorrect "To" header!');
$this->assertContains('Cc: ' . $cc, $messageString, 'Incorrect "Cc" header!');
$this->assertContains('Bcc: ' . $bcc, $messageString, 'Incorrect "Bcc" header!');
}
/**
* @depends testGetSwiftMessage
*/
public function testSend()
{
$message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Test');
$message->setTextBody('Yii Swift Test body');
$this->assertTrue($message->send());
}
/**
* @depends testSend
*/
public function testAttachFile()
{
$message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Attach File Test');
$message->setTextBody('Yii Swift Attach File Test body');
$fileName = __FILE__;
$message->attach($fileName);
$this->assertTrue($message->send());
$attachment = $this->getAttachment($message);
$this->assertTrue(is_object($attachment), 'No attachment found!');
$this->assertContains($attachment->getFilename(), $fileName, 'Invalid file name!');
}
/**
* @depends testSend
*/
public function testAttachContent()
{
$message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Create Attachment Test');
$message->setTextBody('Yii Swift Create Attachment Test body');
$fileName = 'test.txt';
$fileContent = 'Test attachment content';
$message->attachContent($fileContent, ['fileName' => $fileName]);
$this->assertTrue($message->send());
$attachment = $this->getAttachment($message);
$this->assertTrue(is_object($attachment), 'No attachment found!');
$this->assertEquals($fileName, $attachment->getFilename(), 'Invalid file name!');
}
/**
* @depends testSend
*/
public function testEmbedFile()
{
$fileName = $this->createImageFile('embed_file.jpg', 'Embed Image File');
$message = $this->createTestMessage();
$cid = $message->embed($fileName);
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Embed File Test');
$message->setHtmlBody('Embed image: <img src="' . $cid. '" alt="pic">');
$this->assertTrue($message->send());
$attachment = $this->getAttachment($message);
$this->assertTrue(is_object($attachment), 'No attachment found!');
$this->assertContains($attachment->getFilename(), $fileName, 'Invalid file name!');
}
/**
* @depends testSend
*/
public function testEmbedContent()
{
$fileFullName = $this->createImageFile('embed_file.jpg', 'Embed Image File');
$message = $this->createTestMessage();
$fileName = basename($fileFullName);
$contentType = 'image/jpeg';
$fileContent = file_get_contents($fileFullName);
$cid = $message->embedContent($fileContent, ['fileName' => $fileName, 'contentType' => $contentType]);
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Embed File Test');
$message->setHtmlBody('Embed image: <img src="' . $cid. '" alt="pic">');
$this->assertTrue($message->send());
$attachment = $this->getAttachment($message);
$this->assertTrue(is_object($attachment), 'No attachment found!');
$this->assertEquals($fileName, $attachment->getFilename(), 'Invalid file name!');
$this->assertEquals($contentType, $attachment->getContentType(), 'Invalid content type!');
}
/**
* @depends testSend
*/
public function testSendAlternativeBody()
{
$message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Alternative Body Test');
$message->setHtmlBody('<b>Yii Swift</b> test HTML body');
$message->setTextBody('Yii Swift test plain text body');
$this->assertTrue($message->send());
$messageParts = $message->getSwiftMessage()->getChildren();
$textPresent = false;
$htmlPresent = false;
foreach ($messageParts as $part) {
if (!($part instanceof \Swift_Mime_Attachment)) {
/* @var $part \Swift_Mime_MimePart */
if ($part->getContentType() == 'text/plain') {
$textPresent = true;
}
if ($part->getContentType() == 'text/html') {
$htmlPresent = true;
}
}
}
$this->assertTrue($textPresent, 'No text!');
$this->assertTrue($htmlPresent, 'No HTML!');
}
/**
* @depends testGetSwiftMessage
*/
public function testSerialize()
{
$message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Alternative Body Test');
$message->setTextBody('Yii Swift test plain text body');
$serializedMessage = serialize($message);
$this->assertNotEmpty($serializedMessage, 'Unable to serialize message!');
$unserializedMessaage = unserialize($serializedMessage);
$this->assertEquals($message, $unserializedMessaage, 'Unable to unserialize message!');
}
/**
* @depends testSendAlternativeBody
*/
public function testAlternativeBodyCharset()
{
$message = $this->createTestMessage();
$charset = 'windows-1251';
$message->setCharset($charset);
$message->setTextBody('some text');
$message->setHtmlBody('some html');
$content = $message->toString();
$this->assertEquals(2, substr_count($content, $charset), 'Wrong charset for alternative body.');
$message->setTextBody('some text override');
$content = $message->toString();
$this->assertEquals(2, substr_count($content, $charset), 'Wrong charset for alternative body override.');
}
}

50
tests/TestCase.php

@ -0,0 +1,50 @@
<?php
namespace yiiunit\extensions\swiftmailer;
use yii\helpers\ArrayHelper;
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* Clean up after test.
* By default the application created with [[mockApplication]] will be destroyed.
*/
protected function tearDown()
{
parent::tearDown();
$this->destroyApplication();
}
/**
* Populates Yii::$app with a new application
* The application will be destroyed on tearDown() automatically.
* @param array $config The application configuration, if needed
* @param string $appClass name of the application class to create
*/
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => $this->getVendorPath(),
], $config));
}
protected function getVendorPath()
{
$vendor = dirname(dirname(__DIR__)) . '/vendor';
if (!is_dir($vendor)) {
$vendor = dirname(dirname(dirname(dirname(__DIR__))));
}
return $vendor;
}
/**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{
\Yii::$app = null;
}
}

15
tests/bootstrap.php

@ -0,0 +1,15 @@
<?php
// ensure we get report on all possible php errors
error_reporting(-1);
define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
Yii::setAlias('@yiiunit/extensions/swiftmailer', __DIR__);
Yii::setAlias('@yii/swiftmailer', dirname(__DIR__));

3
tests/runtime/.gitignore vendored

@ -0,0 +1,3 @@
*
!.gitignore
!/coveralls/.gitkeep

0
tests/runtime/coveralls/.gitkeep

Loading…
Cancel
Save