Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 59
MandrillHttpTransportTest
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
20
0.00% covered (danger)
0.00%
0 / 59
 testToString
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 getTransportData
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 16
 testSend
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 25
 testSendThrowsForErrorResponse
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 15
<?php
/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillHttpTransport;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
class MandrillHttpTransportTest extends TestCase
{
    /**
     * @dataProvider getTransportData
     */
    public function testToString(MandrillHttpTransport $transport, string $expected)
    {
        $this->assertSame($expected, (string) $transport);
    }
    public function getTransportData()
    {
        return [
            [
                new MandrillHttpTransport('KEY'),
                'mandrill+https://mandrillapp.com',
            ],
            [
                (new MandrillHttpTransport('KEY'))->setHost('example.com'),
                'mandrill+https://example.com',
            ],
            [
                (new MandrillHttpTransport('KEY'))->setHost('example.com')->setPort(99),
                'mandrill+https://example.com:99',
            ],
        ];
    }
    public function testSend()
    {
        $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
            $this->assertSame('POST', $method);
            $this->assertSame('https://mandrillapp.com/api/1.0/messages/send-raw.json', $url);
            $body = json_decode($options['body'], true);
            $message = $body['raw_message'];
            $this->assertSame('KEY', $body['key']);
            $this->assertSame('saif.gmati@symfony.com', $body['to'][0]);
            $this->assertSame('Fabien <fabpot@symfony.com>', $body['from_email']);
            $this->assertStringContainsString('Subject: Hello!', $message);
            $this->assertStringContainsString('To: Saif Eddin <saif.gmati@symfony.com>', $message);
            $this->assertStringContainsString('From: Fabien <fabpot@symfony.com>', $message);
            $this->assertStringContainsString('Hello There!', $message);
            return new MockResponse(json_encode([['_id' => 'foobar']]), [
                'http_code' => 200,
            ]);
        });
        $transport = new MandrillHttpTransport('KEY', $client);
        $mail = new Email();
        $mail->subject('Hello!')
            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
            ->from(new Address('fabpot@symfony.com', 'Fabien'))
            ->text('Hello There!');
        $message = $transport->send($mail);
        $this->assertSame('foobar', $message->getMessageId());
    }
    public function testSendThrowsForErrorResponse()
    {
        $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
            return new MockResponse(json_encode(['status' => 'error', 'message' => 'i\'m a teapot', 'code' => 418]), [
                'http_code' => 418,
            ]);
        });
        $transport = new MandrillHttpTransport('KEY', $client);
        $mail = new Email();
        $mail->subject('Hello!')
            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
            ->from(new Address('fabpot@symfony.com', 'Fabien'))
            ->text('Hello There!');
        $this->expectException(HttpTransportException::class);
        $this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
        $transport->send($mail);
    }
}