<?php

namespace Tricks\CampaignMonitor\Test\Unit;

use CS_REST_Transactional_SmartEmail;
use CS_REST_Wrapper_Result;
use Mockery;
use PHPUnit\Framework\TestCase;
use Tricks\CampaignMonitor\CampaignMonitor;

class CampaignMonitorTest extends TestCase
{
    /** @var Mockery\Mock */
    protected $campaignMonitorTransactional;

    /** @var CampaignMonitor */
    protected $campaignMonitor;

    public function setUp(): void
    {
        parent::setup();

        $this->campaignMonitorTransactional = Mockery::mock(CS_REST_Transactional_SmartEmail::class);

        $this->campaignMonitor = new CampaignMonitor($this->campaignMonitorTransactional);
    }


    public function tearDown(): void
    {
        parent::tearDown();

        if ($container = Mockery::getContainer()) {
            $this->addToAssertionCount($container->mockery_getExpectationCount());
        }

        Mockery::close();
    }

    /**
     * @test
     */
    public function it_can_send_transactional_mail()
    {
        $recipient = 'v.rose@tricks.de';
        $smartEmailId = '12345';

        $this->campaignMonitorTransactional->shouldReceive('set_smartemail_id')->withArgs([$smartEmailId]);
        $this->campaignMonitorTransactional
            ->shouldReceive('send')
            ->withArgs(
                [
                    [
                        "To" => [$recipient],
                        "Data" => []
                    ],
                    'no'
                ]
            )
            ->andReturn(Mockery::mock(CS_REST_Wrapper_Result::class));

        $this->campaignMonitor->sendTransactional($smartEmailId, $recipient, []);
    }

    /**
     * @test
     */
    public function it_can_send_transactional_mail_to_several_recipients()
    {
        $recipient = 'v.rose@tricks.de; foo@dev.tricks.de';
        $smartEmailId = '12345';

        $this->campaignMonitorTransactional->shouldReceive('set_smartemail_id')->withArgs([$smartEmailId]);
        $this->campaignMonitorTransactional
            ->shouldReceive('send')
            ->withArgs(
                [
                    [
                        "To" => [
                            'v.rose@tricks.de',
                            'foo@dev.tricks.de'
                        ],
                        "Data" => []
                    ],
                    'no'
                ]
            )
            ->andReturn(Mockery::mock(CS_REST_Wrapper_Result::class));

        $this->campaignMonitor->sendTransactional($smartEmailId, $recipient, []);
    }

    /**
     * @test
     */
    public function it_can_send_transactional_mail_with_attachments()
    {
        $recipient = 'v.rose@tricks.de';
        $smartEmailId = '12345';

        $data = [
            'foo' => 'bar'
        ];

        $fileName = __DIR__ . '/./../files/dummy.pdf';
        $pdfAsBinary = fread(fopen($fileName, "r"), filesize($fileName));

        $attachments = [
            [
                'Name' => 'foo',
                'Type' => 'application/pdf',
                'Content' => base64_encode($pdfAsBinary)
            ]
        ];

        $this->campaignMonitorTransactional->shouldReceive('set_smartemail_id')->withArgs([$smartEmailId]);
        $this->campaignMonitorTransactional
            ->shouldReceive('send')
            ->withArgs(
                [
                    [
                        "To" => [$recipient],
                        "Data" => $data,
                        "Attachments" => $attachments
                    ],
                    'no'
                ]
            )
            ->andReturn(Mockery::mock(CS_REST_Wrapper_Result::class));

        $this->campaignMonitor->sendTransactional($smartEmailId, $recipient, $data, $attachments);
    }

}
