<commit_before><?php
namespace AcMailerTest\Service;

use AcMailer\Exception\InvalidArgumentException;
use AcMailerTest\Event\MailListenerMock;
use Zend\Mail\Message;
use AcMailerTest\Mail\Transport\MockTransport;
use Zend\View\Renderer\PhpRenderer;
use AcMailer\Service\MailService;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;
use AcMailer\Result\MailResult;

/**
 * Mail service test case
 * @author <NAME>
 * @link http://www.alejandrocelaya.com
 */
class MailServiceTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \AcMailerTest\Mail\Transport\MockTransport
     */
    private $transport;
    /**
     * @var \AcMailer\Service\MailService
     */
    private $mailService;
    
    public function setUp()
    {
        $this->transport    = new MockTransport();
        $this->mailService  = new MailService(new Message(), $this->transport, new PhpRenderer());
    }
    
    public function testMimePartBodyCasting()
    {
        $this->mailService->setBody(new MimePart("Foo"));
        $this->assertTrue($this->mailService->getMessage()->getBody() instanceof MimeMessage);
    }
    
    public function testHtmlBodyCasting()
    {
        $this->mailService->setBody("<div>Html body</div>");
        $this->assertTrue($this->mailService->getMessage()->getBody() instanceof MimeMessage);
    }
    
    public function testStringBodyRemainsUnchanged()
    {
        $expected = "String body";
        $this->mailService->setBody($expected);
        
        $this->assertTrue(is_string($this->mailService->getMessage()->getBody()));
        $this->assertEquals($expected, $this->mailService->getMessage()->getBody());
    }
    
    public function testMimeMessageBodyRemainsUnchanged()
    {
        $part       = new MimePart("Foo");
        $message    = new MimeMessage();
        $message->addPart($part);
        $this->mailService->setBody($message);
        
        $this->assertTrue($this->mailService->getMessage()->getBody() instanceof MimeMessage);
        $this->assertEquals($message, $this->mailService->getMessage()->getBody());
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testInvalidBodyThrowsException()
    {
        $this->mailService->setBody(new \stdClass());
    }
    
    public function testSetSubject()
    {
        $expected = "This is the subject";
        
        $this->assertEquals($this->mailService, $this->mailService->setSubject($expected));
        $this->assertEquals($expected, $this->mailService->getMessage()->getSubject());
    }
    
    public function testSuccessfulSending()
    {
        $result = $this->mailService->send();
        
        $this->assertTrue($result->isValid());
        $this->assertEquals(MailResult::DEFAULT_MESSAGE, $result->getMessage());
    }
    
    public function testSendingWithError()
    {
        $this->transport->setForceError(true);
        $result = $this->mailService->send();
        
        $this->assertFalse($result->isValid());
        $this->assertEquals(MockTransport::ERROR_MESSAGE, $result->getMessage());
    }

    public function testSuccesfulMailEvent()
    {
        $mailListener = new MailListenerMock();
        $this->mailService->attachMailListener($mailListener);
        $result = $this->mailService->send();

        $this->assertTrue($mailListener->isOnPreSendCalled());
        $this->assertTrue($mailListener->isOnPostSendCalled());
        $this->assertFalse($mailListener->isOnSendErrorCalled());
    }

    public function testMailEventWithError()
    {
        $mailListener = new MailListenerMock();
        $this->transport->setForceError(true);
        $this->mailService->attachMailListener($mailListener);
        $result = $this->mailService->send();

        $this->assertTrue($mailListener->isOnPreSendCalled());
        $this->assertFalse($mailListener->isOnPostSendCalled());
        $this->assertTrue($mailListener->isOnSendErrorCalled());
    }
}<commit_msg>Enhanced MailServiceTest to get greater code coverage<commit_after><?php
namespace AcMailerTest\Service;

use AcMailer\Exception\InvalidArgumentException;
use AcMailerTest\Event\MailListenerMock;
use Zend\Mail\Message;
use AcMailerTest\Mail\Transport\MockTransport;
use Zend\View\Renderer\PhpRenderer;
use AcMailer\Service\MailService;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;
use AcMailer\Result\MailResult;
use Zend\View\Resolver\TemplatePathStack;

/**
 * Mail service test case
 * @author <NAME>
 * @link http://www.alejandrocelaya.com
 */
class MailServiceTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \AcMailerTest\Mail\Transport\MockTransport
     */
    private $transport;
    /**
     * @var \AcMailer\Service\MailService
     */
    private $mailService;
    
    public function setUp()
    {
        $this->transport    = new MockTransport();
        $this->mailService  = new MailService(new Message(), $this->transport, new PhpRenderer());
    }
    
    public function testMimePartBodyCasting()
    {
        $this->mailService->setBody(new MimePart("Foo"));
        $this->assertTrue($this->mailService->getMessage()->getBody() instanceof MimeMessage);
    }
    
    public function testHtmlBodyCasting()
    {
        $this->mailService->setBody("<div>Html body</div>");
        $this->assertTrue($this->mailService->getMessage()->getBody() instanceof MimeMessage);
    }
    
    public function testStringBodyRemainsUnchanged()
    {
        $expected = "String body";
        $this->mailService->setBody($expected);
        
        $this->assertTrue(is_string($this->mailService->getMessage()->getBody()));
        $this->assertEquals($expected, $this->mailService->getMessage()->getBody());
    }
    
    public function testMimeMessageBodyRemainsUnchanged()
    {
        $part       = new MimePart("Foo");
        $message    = new MimeMessage();
        $message->addPart($part);
        $this->mailService->setBody($message);
        
        $this->assertTrue($this->mailService->getMessage()->getBody() instanceof MimeMessage);
        $this->assertEquals($message, $this->mailService->getMessage()->getBody());
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testInvalidBodyThrowsException()
    {
        $this->mailService->setBody(new \stdClass());
    }
    
    public function testSetSubject()
    {
        $expected = "This is the subject";
        
        $this->assertEquals($this->mailService, $this->mailService->setSubject($expected));
        $this->assertEquals($expected, $this->mailService->getMessage()->getSubject());
    }
    
    public function testSuccessfulSending()
    {
        $result = $this->mailService->send();
        
        $this->assertTrue($result->isValid());
        $this->assertEquals(MailResult::DEFAULT_MESSAGE, $result->getMessage());
    }
    
    public function testSendingWithError()
    {
        $this->transport->setForceError(true);
        $result = $this->mailService->send();
        
        $this->assertFalse($result->isValid());
        $this->assertEquals(MockTransport::ERROR_MESSAGE, $result->getMessage());
    }

    public function testSuccesfulMailEvent()
    {
        $mailListener = new MailListenerMock();
        $this->mailService->attachMailListener($mailListener);
        $result = $this->mailService->send();

        $this->assertTrue($result->isValid());
        $this->assertTrue($mailListener->isOnPreSendCalled());
        $this->assertTrue($mailListener->isOnPostSendCalled());
        $this->assertFalse($mailListener->isOnSendErrorCalled());
    }

    public function testMailEventWithError()
    {
        $mailListener = new MailListenerMock();
        $this->transport->setForceError(true);
        $this->mailService->attachMailListener($mailListener);
        $result = $this->mailService->send();

        $this->assertFalse($result->isValid());
        $this->assertTrue($mailListener->isOnPreSendCalled());
        $this->assertFalse($mailListener->isOnPostSendCalled());
        $this->assertTrue($mailListener->isOnSendErrorCalled());
    }

    public function testDetachedMailListenerIsNotTriggered()
    {
        $mailListener = new MailListenerMock();
        $this->mailService->attachMailListener($mailListener);
        $this->mailService->detachMailListener($mailListener);
        $result = $this->mailService->send();

        $this->assertTrue($result->isValid());
        $this->assertFalse($mailListener->isOnPreSendCalled());
        $this->assertFalse($mailListener->isOnPostSendCalled());
        $this->assertFalse($mailListener->isOnSendErrorCalled());
    }

    public function testValidTemplateMakesBodyToBeMimeMessage()
    {
        $resolver = new TemplatePathStack();
        $resolver->addPath(__DIR__ . '/../../../view');
        $this->mailService->getRenderer()->setResolver($resolver);
        $this->mailService->setTemplate('ac-mailer/mail-templates/mail.phtml');

        $this->assertInstanceOf('Zend\Mime\Message', $this->mailService->getMessage()->getBody());
    }

    /**
     * @expectedException \Zend\View\Exception\RuntimeException
     */
    public function testInvalidTemplateThrowsException()
    {
        $this->mailService->setTemplate('foo/bar');
    }

    public function testAttachmentsTotal()
    {
        $this->assertCount(0, $this->mailService->getAttachments());

        $this->mailService->setAttachments(array('one', 'two', 'three'));
        $this->mailService->addAttachments(array('four', 'five', 'six'));
        $this->mailService->addAttachment('seven');
        $this->mailService->addAttachment('eight');
        $this->assertCount(8, $this->mailService->getAttachments());

        $this->mailService->setAttachments(array('one', 'two'));
        $this->assertCount(2, $this->mailService->getAttachments());

        $this->mailService->addAttachments(array('three', 'four'));
        $this->assertCount(4, $this->mailService->getAttachments());
    }

    public function testAttachmentsAreAddedAsMimeParts()
    {
        $this->mailService->setAttachments(array(
            'attachments/file1',
            'attachments/file2',
            'attachments/dir/file3',
            'invalid/attachment'
        ));
        $this->mailService->setBody('Body as string');
        $result = $this->mailService->send();
        $this->assertTrue($result->isValid());

        /* @var MimeMessage $body */
        $body = $this->mailService->getMessage()->getBody();
        $this->assertInstanceOf('Zend\Mime\Message', $body);
        // The body and the three attached files make it a total of 4 parts
        $this->assertCount(4, $body->getParts());
    }
}
{'fraction_non_alphanumeric': 0.1521273624429755, 'fraction_numerical': 0.0008379108090494367, 'mean_word_length': 3.219167321288295, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 241, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4033853', 'n_tokens_mistral': 2968, 'n_tokens_neox': 2995, 'n_words': 424}