Source for file Email.php

Documentation is available at Email.php

  1. <?php
  2. /**
  3.  *  Magentron EmailImages Extension
  4.  *
  5.  *  @category   Magentron
  6.  *  @package    Magentron_EmailImages
  7.  *  @author     Jeroen Derks
  8.  *  @copyright  Copyright (c) 2011 Jeroen Derks http://www.magentron.com
  9.  *  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  10.  */
  11. class Magentron_EmailImages_Test_Model_Email extends EcomDev_PHPUnit_Test_Case
  12. {
  13.     /** Email type: text or html
  14.      *  @var    string 
  15.      */
  16.     protected $_type = null;
  17.  
  18.     /** Mail mock object instance.
  19.      *  @var    Magentron_EmailImages_Model_Mail 
  20.      */
  21.     protected $_mail = null;
  22.  
  23.  
  24.     /**
  25.      *  Test send()
  26.      *
  27.      *  @test
  28.      *  @loadFixture
  29.      *  @doNotIndexAll
  30.      *
  31.      *  @see    $_type,
  32.      *           send(),
  33.      *           Magentron_EmailImages_Model_Mail_send()
  34.      */
  35.     public function testSend()
  36.     {
  37.         /** @var    $email  Magentron_EmailImages_Model_Email */
  38.         $email          Mage::getModel('core/email');
  39.         $this->assertEquals('Magentron_EmailImages_Model_Email'get_class($email));
  40.         
  41.         foreach array('html''text'as $type )
  42.         {
  43.             // prevent mail from actually being sent and call assert functions
  44.             /** @var    $mailMock   PHPUnit_Framework_MockObject_MockObject */
  45.             $this->_mail    = // continue below
  46.             $mock           $this->getModelMock('emailimages/mail'array('send'));
  47.     
  48.             $mock->expects($this->any())
  49.                     ->method('send')
  50.                     ->will($this->returnCallback(array($this'Magentron_EmailImages_Model_Mail_send')));
  51.     
  52.             $this->replaceByMock('model''emailimages/mail'$mock);
  53.             
  54.             $email Mage::getModel('emailimages/email');
  55.             $email->setFromEmail(   Mage::getStoreConfig('trans_email/ident_general/email'))
  56.                     ->setFromName(  Mage::getStoreConfig('trans_email/ident_general/name'))
  57.                     ->setToEmail(   Mage::getStoreConfig('trans_email/ident_support/email'))
  58.                     ->setToName(    Mage::getStoreConfig('trans_email/ident_support/name'))
  59.                     ->setSubject('Test subject');
  60.  
  61.             $this->_type = $type;
  62.             
  63.             $email->setType($type);
  64.             
  65.             switch $type )
  66.             {
  67.                 case 'html':
  68.                     $email->setBody('<html><body><img src="' Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN'frontend/default/default/images/logo.gif" /></body></html>');
  69.                     break;
  70.                     
  71.                 case 'text':
  72.                     $email->setBody('This is a test email.');
  73.                     break;
  74.             }
  75.                     
  76.             $email->send();
  77.         }
  78.  
  79.     }
  80.  
  81.  
  82.     /**
  83.      *  Test send() with sending mail disabled
  84.      *
  85.      *  @test
  86.      *  @loadFixture
  87.      *  @doNotIndexAll
  88.      */
  89.     public function testSendDisabled()
  90.     {
  91.         /** @see: http://stackoverflow.com/a/18701405/832620 */
  92.         Mage::app()->getStore(0)
  93.             ->setConfig('system/smtp/disable'1);
  94.  
  95.         $this->testSend();  
  96.     }
  97.  
  98.  
  99.     /**
  100.      *  Magetron_EmailImages_Model_Mail function send() replacement function.
  101.      *
  102.      *  @see    testSend(), send(),
  103.      *           Magentron_EmailImages_Model_Mail::send()
  104.      */
  105.     public function Magentron_EmailImages_Model_Mail_send$transport null )
  106.     {
  107.         // setup mail instance similar to original one in $this->_mail
  108.         $mail new Magentron_EmailImages_Model_Mail($this->_mail->getCharset());
  109.         $mail->setHeaderEncoding($this->_mail->getHeaderEncoding())
  110.                 ->setMimeBoundary($this->_mail->getMimeBoundary())
  111.                 ->setDate($this->_mail->getDate())
  112.                 ->setFrom($this->_mail->getFrom()$this->_mail->getFrom())
  113.                 ->setReplyTo($this->_mail->getReplyTo()$this->_mail->getReplyTo())
  114.                 ->addTo(array_slice($this->_mail->getRecipients()01));
  115.                 
  116.         switch $this->_type )
  117.         {
  118.             case 'html':
  119.                 $mail->setBodyHtml(quoted_printable_decode($this->_mail->getBodyHtml(true)));
  120.                 break;
  121.                 
  122.             case 'text':
  123.                 $mail->setBodyText($this->_mail->getBodyText(true));
  124.                 break;
  125.         }
  126.  
  127.         return $mail->send($this);
  128.     }
  129.     
  130.     /**
  131.      *  Zend_Mail_Transport function send()
  132.      *
  133.      *  @see    $_type,
  134.      *           Magentron_EmailImages_Model_Mail_send(),
  135.      *           Zend_Mail::send()
  136.      */
  137.     public function send$mail )
  138.     {
  139.         if Mage::getStoreConfig('system/smtp/disable') )
  140.         {
  141.             $this->fail('send() should not be called when the sending mail has been disabled');
  142.         }
  143.         else
  144.         {
  145.             switch $this->_type )
  146.             {
  147.                 case 'html':
  148.                     $this->assertEquals(1$mail->getPartCount()'HTML email should have 1 attachment #');
  149.                     break;
  150.                 
  151.                 case 'text':
  152.                     $this->assertEquals(0$mail->getPartCount()'Text email should have no attachments #');
  153.                     break;
  154.             }
  155.         }
  156.     }
  157. }

Documentation generated on Fri, 09 Oct 2015 03:37:10 +0200 by phpDocumentor 1.4.4