-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* changed from travis to git action * used new env setting method * fixed brackets * updated dependencies * updated unit tests * updated MandrillTest.php added runtime directory creation * fixed metadata class var, code style updates, added ext-fileinfo to composer.json * tests with key from secrets * tests with key from secrets 2 * tests with key from secrets 3 * fixed response and error handling * fixed logging * replaced sendRaw with send * set global merge vars and template content independent from template language fixed response handling * fixed test * fixed message test
- Loading branch information
Showing
7 changed files
with
210 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
/** | ||
* @package yii2-mandrill | ||
* @author Simon Karlen <simi.albi@gmail.com> | ||
*/ | ||
|
||
namespace yiiunit\extensions\mandrill; | ||
|
||
use nickcv\mandrill\Mailer; | ||
|
||
/** | ||
* Class MandrillSendTest | ||
* @package yiiunit\extensions\mandrill | ||
*/ | ||
class MandrillSendTest extends TestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $_apiKey; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $_fromAddress; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $_toAddress; | ||
|
||
public function testSendMessage() | ||
{ | ||
$mandrill = new Mailer(['apikey' => $this->_apiKey]); | ||
$result = $mandrill->compose('test') | ||
->setFrom($this->_fromAddress) | ||
->setTo($this->_toAddress) | ||
->setSubject('test email') | ||
->embed($this->getTestImagePath()) | ||
->attach($this->getTestPdfPath()) | ||
->send(); | ||
|
||
$this->assertInternalType('array', $mandrill->getLastTransaction()); | ||
$lastTransaction = $mandrill->getLastTransaction()[0]; | ||
$this->assertArrayHasKey('email', $lastTransaction); | ||
$this->assertEquals($this->_toAddress, $lastTransaction['email']); | ||
$this->assertArrayHasKey('status', $lastTransaction); | ||
$this->assertEquals('queued', $lastTransaction['status']); | ||
$this->assertArrayHasKey('_id', $lastTransaction); | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
public function testSendMessageUsingMandrillTemplate() | ||
{ | ||
$mandrill = new Mailer([ | ||
'apikey' => $this->_apiKey, | ||
'useMandrillTemplates' => true, | ||
]); | ||
$result = $mandrill->compose('testTemplate', ['WORD' => 'my word']) | ||
->setFrom($this->_fromAddress) | ||
->setTo($this->_toAddress) | ||
->setSubject('test template email') | ||
->embed($this->getTestImagePath()) | ||
->attach($this->getTestPdfPath()) | ||
->setGlobalMergeVars(['MERGEVAR' => 'prova']) | ||
->send(); | ||
|
||
$this->assertInternalType('array', $mandrill->getLastTransaction()); | ||
$lastTransaction = $mandrill->getLastTransaction()[0]; | ||
$this->assertArrayHasKey('email', $lastTransaction); | ||
$this->assertEquals($this->_toAddress, $lastTransaction['email']); | ||
$this->assertArrayHasKey('status', $lastTransaction); | ||
$this->assertEquals('queued', $lastTransaction['status']); | ||
$this->assertArrayHasKey('_id', $lastTransaction); | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
public function testSendMessageUsingMandrillTemplateHandlebars() | ||
{ | ||
$mandrill = new Mailer([ | ||
'apikey' => $this->_apiKey, | ||
'useMandrillTemplates' => true, | ||
'useTemplateDefaults' => false, | ||
'templateLanguage' => Mailer::LANGUAGE_HANDLEBARS, | ||
]); | ||
$result = $mandrill->compose('testTemplateHandlebars', ['variable' => 'test content']) | ||
->setFrom($this->_fromAddress) | ||
->setTo($this->_toAddress) | ||
->setSubject('test handlebars') | ||
->send(); | ||
|
||
$this->assertInternalType('array', $mandrill->getLastTransaction()); | ||
$lastTransaction = $mandrill->getLastTransaction()[0]; | ||
$this->assertArrayHasKey('email', $lastTransaction); | ||
$this->assertEquals($this->_toAddress, $lastTransaction['email']); | ||
$this->assertArrayHasKey('status', $lastTransaction); | ||
$this->assertArrayHasKey('_id', $lastTransaction); | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
public function testCannotSendIfMandrillTemplateNotFound() | ||
{ | ||
$mandrill = new Mailer([ | ||
'apikey' => $this->_apiKey, | ||
'useMandrillTemplates' => true, | ||
]); | ||
|
||
$result = $mandrill->compose('madeupTemplate', ['WORD' => 'my word']) | ||
->setFrom($this->_fromAddress) | ||
->setTo($this->_toAddress) | ||
->setSubject('test template email') | ||
->embed($this->getTestImagePath()) | ||
->attach($this->getTestPdfPath()) | ||
->send(); | ||
|
||
$this->assertInternalType('array', $mandrill->getLastTransaction()); | ||
$this->assertArrayHasKey('status', $mandrill->getLastTransaction()); | ||
$this->assertEquals('error', $mandrill->getLastTransaction()['status']); | ||
|
||
$this->assertFalse($result); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->_apiKey = getenv('MANDRILL_API_KEY'); | ||
$this->_fromAddress = getenv('MANDRILL_FROM_ADDRESS'); | ||
$this->_toAddress = getenv('MANDRILL_TO_ADDRESS'); | ||
|
||
if (!$this->_apiKey || !$this->_fromAddress || !$this->_toAddress) { | ||
$this->markTestSkipped('One of "API key", "from address" or "to address" not set in secrets. Test skipped.'); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function getTestImagePath(): string | ||
{ | ||
return __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'test.png'; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function getTestPdfPath(): string | ||
{ | ||
return __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'test.pdf'; | ||
} | ||
} |
Oops, something went wrong.