<commit_before><?php

namespace Test\Synapse\Email;

use PHPUnit_Framework_TestCase;
use Synapse\Email\MandrillSender;
use Synapse\Email\EmailEntity;
use stdClass;

class MandrillSenderTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->captured = new stdClass();

        $this->setUpMockMandrill();
        $this->setUpMockEmailMapper();

        $this->sender = new MandrillSender(
            $this->mockMandrill,
            $this->mockEmailMapper
        );

        $this->sender->setConfig($this->getTestConfig());
    }

    public function setUpMockMandrill()
    {
        $this->mockMandrill = $this->getMockBuilder('Mandrill')
            ->disableOriginalConstructor()
            ->getMock();

        $this->mockMandrillMessages = $this->getMockBuilder('Mandrill_Messages')
            ->disableOriginalConstructor()
            ->getMock();

        $this->mockMandrill->messages = $this->mockMandrillMessages;
    }

    public function setUpMockEmailMapper()
    {
        $this->mockEmailMapper = $this->getMockBuilder('Synapse\Email\EmailMapper')
            ->disableOriginalConstructor()
            ->getMock();
    }

    public function createEmailEntity($sender = '', $recipient = '')
    {
        return new EmailEntity([
            'sender_email'    => $sender,
            'recipient_email' => $recipient,
        ]);
    }

    public function captureEmailAddresses()
    {
        $this->mockMandrillMessages->expects($this->any())
            ->method('send')
            ->will($this->returnCallback(function($data) {
                $this->captured->recipientEmail = $data['to'][0]['email'];
                $this->captured->senderEmail    = $data['from_email'];

                return [['status' => 1]];
            }));
    }

    public function getTestConfig()
    {
        return [
            'whitelist' => [
                'whitelist' => ['<EMAIL>', 'domain.com', 'test.com'],
                'trap'      => '<EMAIL>',
            ],
        ];
    }

    public function provideSendersAndRecipientsWithExpectedFilteredEmails()
    {
        return [
            ['<EMAIL>', '<EMAIL>', '<EMAIL>'],
            ['<EMAIL>', '<EMAIL>', '<EMAIL>'],
            ['<EMAIL>', '<EMAIL>', '<EMAIL>'],
            ['<EMAIL>', '<EMAIL>', '<EMAIL>'],
        ];
    }

    /**
     * @dataProvider provideSendersAndRecipientsWithExpectedFilteredEmails
     */
    public function testSendOnlyRunsSenderEmailAddressThroughWhitelistFilter($sender, $recipient, $expectedRecipient)
    {
        $this->captureEmailAddresses();

        $email = $this->createEmailEntity();

        $email->setSenderEmail($sender);
        $email->setRecipientEmail($recipient);

        $this->sender->send($email);

        $this->assertEquals(
            $sender,
            $this->captured->senderEmail<commit_msg>Refs #102 - Make config variable name more succinct in test.<commit_after><?php

namespace Test\Synapse\Email;

use PHPUnit_Framework_TestCase;
use Synapse\Email\MandrillSender;
use Synapse\Email\EmailEntity;
use stdClass;

class MandrillSenderTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->captured = new stdClass();

        $this->setUpMockMandrill();
        $this->setUpMockEmailMapper();

        $this->sender = new MandrillSender(
            $this->mockMandrill,
            $this->mockEmailMapper
        );

        $this->sender->setConfig($this->getTestConfig());
    }

    public function setUpMockMandrill()
    {
        $this->mockMandrill = $this->getMockBuilder('Mandrill')
            ->disableOriginalConstructor()
            ->getMock();

        $this->mockMandrillMessages = $this->getMockBuilder('Mandrill_Messages')
            ->disableOriginalConstructor()
            ->getMock();

        $this->mockMandrill->messages = $this->mockMandrillMessages;
    }

    public function setUpMockEmailMapper()
    {
        $this->mockEmailMapper = $this->getMockBuilder('Synapse\Email\EmailMapper')
            ->disableOriginalConstructor()
            ->getMock();
    }

    public function createEmailEntity($sender = '', $recipient = '')
    {
        return new EmailEntity([
            'sender_email'    => $sender,
            'recipient_email' => $recipient,
        ]);
    }

    public function captureEmailAddresses()
    {
        $this->mockMandrillMessages->expects($this->any())
            ->method('send')
            ->will($this->returnCallback(function($data) {
                $this->captured->recipientEmail = $data['to'][0]['email'];
                $this->captured->senderEmail    = $data['from_email'];

                return [['status' => 1]];
            }));
    }

    public function getTestConfig()
    {
        return [
            'whitelist' => [
                'list' => ['<EMAIL>', 'domain.com', 'test.com'],
                'trap' => '<EMAIL>',
            ],
        ];
    }

    public function provideSendersAndRecipientsWithExpectedFilteredEmails()
    {
        return [
            ['<EMAIL>', '<EMAIL>', '<EMAIL>'],
            ['<EMAIL>', '<EMAIL>', '<EMAIL>'],
            ['<EMAIL>', '<EMAIL>', '<EMAIL>'],
            ['<EMAIL>', '<EMAIL>', '<EMAIL>'],
        ];
    }

    /**
     * @dataProvider provideSendersAndRecipientsWithExpectedFilteredEmails
     */
    public function testSendOnlyRunsSenderEmailAddressThroughWhitelistFilter($sender, $recipient, $expectedRecipient)
    {
        $this->captureEmailAddresses();

        $email = $this->createEmailEntity();

        $email->setSenderEmail($sender);
        $email->setRecipientEmail($recipient);

        $this->sender->send($email);

        $this->assertEquals(
            $sender,
            $this->captured->senderEmail
{'fraction_non_alphanumeric': 0.1501641610506307, 'fraction_numerical': 0.0012096077414895455, 'mean_word_length': 2.3074285714285714, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6337966', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1618, 'n_words': 240}