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 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 42
MandrillHttpTransportTest
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 5
30
0.00% covered (danger)
0.00%
0 / 42
 testToString
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getTransportData
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 testSend
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 19
 testSendThrowsForErrorResponse
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 8
 testTagAndMetadataHeaders
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 13
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Mailer\Bridge\Mailchimp\Tests\Transport;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\HttpClient\MockHttpClient;
16use Symfony\Component\HttpClient\Response\MockResponse;
17use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillHttpTransport;
18use Symfony\Component\Mailer\Exception\HttpTransportException;
19use Symfony\Component\Mailer\Header\MetadataHeader;
20use Symfony\Component\Mailer\Header\TagHeader;
21use Symfony\Component\Mime\Address;
22use Symfony\Component\Mime\Email;
23use Symfony\Contracts\HttpClient\ResponseInterface;
24
25class MandrillHttpTransportTest extends TestCase
26{
27    /**
28     * @dataProvider getTransportData
29     */
30    public function testToString(MandrillHttpTransport $transport, string $expected)
31    {
32        $this->assertSame($expected, (string) $transport);
33    }
34
35    public function getTransportData()
36    {
37        return [
38            [
39                new MandrillHttpTransport('KEY'),
40                'mandrill+https://mandrillapp.com',
41            ],
42            [
43                (new MandrillHttpTransport('KEY'))->setHost('example.com'),
44                'mandrill+https://example.com',
45            ],
46            [
47                (new MandrillHttpTransport('KEY'))->setHost('example.com')->setPort(99),
48                'mandrill+https://example.com:99',
49            ],
50        ];
51    }
52
53    public function testSend()
54    {
55        $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
56            $this->assertSame('POST', $method);
57            $this->assertSame('https://mandrillapp.com/api/1.0/messages/send-raw.json', $url);
58
59            $body = json_decode($options['body'], true);
60            $message = $body['raw_message'];
61            $this->assertSame('KEY', $body['key']);
62            $this->assertSame('Fabien', $body['from_name']);
63            $this->assertSame('fabpot@symfony.com', $body['from_email']);
64            $this->assertSame('saif.gmati@symfony.com', $body['to'][0]);
65
66            $this->assertStringContainsString('Subject: Hello!', $message);
67            $this->assertStringContainsString('To: Saif Eddin <saif.gmati@symfony.com>', $message);
68            $this->assertStringContainsString('From: Fabien <fabpot@symfony.com>', $message);
69            $this->assertStringContainsString('Hello There!', $message);
70
71            return new MockResponse(json_encode([['_id' => 'foobar']]), [
72                'http_code' => 200,
73            ]);
74        });
75
76        $transport = new MandrillHttpTransport('KEY', $client);
77
78        $mail = new Email();
79        $mail->subject('Hello!')
80            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
81            ->from(new Address('fabpot@symfony.com', 'Fabien'))
82            ->text('Hello There!');
83
84        $message = $transport->send($mail);
85
86        $this->assertSame('foobar', $message->getMessageId());
87    }
88
89    public function testSendThrowsForErrorResponse()
90    {
91        $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
92            return new MockResponse(json_encode(['status' => 'error', 'message' => 'i\'m a teapot', 'code' => 418]), [
93                'http_code' => 418,
94            ]);
95        });
96
97        $transport = new MandrillHttpTransport('KEY', $client);
98
99        $mail = new Email();
100        $mail->subject('Hello!')
101            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
102            ->from(new Address('fabpot@symfony.com', 'Fabien'))
103            ->text('Hello There!');
104
105        $this->expectException(HttpTransportException::class);
106        $this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
107        $transport->send($mail);
108    }
109
110    public function testTagAndMetadataHeaders()
111    {
112        $email = new Email();
113        $email->getHeaders()->addTextHeader('foo', 'bar');
114        $email->getHeaders()->add(new TagHeader('password-reset,user'));
115        $email->getHeaders()->add(new MetadataHeader('Color', 'blue'));
116        $email->getHeaders()->add(new MetadataHeader('Client-ID', '12345'));
117
118        $transport = new MandrillHttpTransport('key');
119        $method = new \ReflectionMethod(MandrillHttpTransport::class, 'addMandrillHeaders');
120        $method->setAccessible(true);
121        $method->invoke($transport, $email);
122
123        $this->assertCount(3, $email->getHeaders()->toArray());
124        $this->assertSame('foo: bar', $email->getHeaders()->get('FOO')->toString());
125        $this->assertSame('X-MC-Tags: password-reset,user', $email->getHeaders()->get('X-MC-Tags')->toString());
126        $this->assertSame('X-MC-Metadata: '.json_encode(['Color' => 'blue', 'Client-ID' => '12345']), $email->getHeaders()->get('X-MC-Metadata')->toString());
127    }
128}