diff options
author | Côme Chilliet <come.chilliet@nextcloud.com> | 2021-12-20 11:35:06 +0100 |
---|---|---|
committer | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2023-02-02 10:30:06 +0100 |
commit | dde5c46a3eb7c6dcee47795590619ccd393577d2 (patch) | |
tree | 5351227347b92e5d69a49b632ea138e314eea013 /tests | |
parent | fc4e87a2dfc5ff53bc9f15da13f355dd285769a9 (diff) | |
download | nextcloud-server-dde5c46a3eb7c6dcee47795590619ccd393577d2.tar.gz nextcloud-server-dde5c46a3eb7c6dcee47795590619ccd393577d2.zip |
Migrate to Symfony Mailer
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/Mail/MailerTest.php | 128 | ||||
-rw-r--r-- | tests/lib/Mail/MessageTest.php | 366 |
2 files changed, 236 insertions, 258 deletions
diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php index 74c4f2a62ab..39e4d689a37 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -22,19 +22,23 @@ use OCP\IURLGenerator; use OCP\L10N\IFactory; use OCP\Mail\Events\BeforeMessageSent; use Psr\Log\LoggerInterface; -use Swift_SwiftException; use Test\TestCase; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Mailer\Mailer as SymfonyMailer; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; +use Symfony\Component\Mailer\Transport\SendmailTransport; class MailerTest extends TestCase { - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IConfig|MockObject */ private $config; - /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Defaults|MockObject */ private $defaults; - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ + /** @var LoggerInterface|MockObject */ private $logger; - /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IURLGenerator|MockObject */ private $urlGenerator; - /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IL10N|MockObject */ private $l10n; /** @var Mailer */ private $mailer; @@ -91,7 +95,7 @@ class MailerTest extends TestCase { $path = '/usr/sbin/sendmail'; } - $expected = new \Swift_SendmailTransport($path . $binaryParam); + $expected = new SendmailTransport($path . $binaryParam, null, $this->logger); $this->assertEquals($expected, self::invokePrivate($this->mailer, 'getSendMailInstance')); } @@ -109,13 +113,22 @@ class MailerTest extends TestCase { ['mail_sendmailmode', 'smtp', $sendmailMode], ]); - $this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam), self::invokePrivate($this->mailer, 'getSendMailInstance')); + $sendmail = new SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam, null, $this->logger); + $this->assertEquals($sendmail, self::invokePrivate($this->mailer, 'getSendMailInstance')); } public function testGetInstanceDefault() { + $this->config + ->method('getSystemValue') + ->willReturnMap([ + ['mail_smtphost', '127.0.0.1', '127.0.0.1'], + ['mail_smtpport', 25, 25], + ['mail_smtptimeout', 10, 10], + ]); $mailer = self::invokePrivate($this->mailer, 'getInstance'); - $this->assertInstanceOf(\Swift_Mailer::class, $mailer); - $this->assertInstanceOf(\Swift_SmtpTransport::class, $mailer->getTransport()); + $this->assertInstanceOf(SymfonyMailer::class, $mailer); + $transport = self::invokePrivate($mailer, 'transport'); + $this->assertInstanceOf(EsmtpTransport::class, $transport); } public function testGetInstanceSendmail() { @@ -127,11 +140,33 @@ class MailerTest extends TestCase { ]); $mailer = self::invokePrivate($this->mailer, 'getInstance'); - $this->assertInstanceOf(\Swift_Mailer::class, $mailer); - $this->assertInstanceOf(\Swift_SendmailTransport::class, $mailer->getTransport()); + $this->assertInstanceOf(SymfonyMailer::class, $mailer); + $transport = self::invokePrivate($mailer, 'transport'); + $this->assertInstanceOf(SendmailTransport::class, $transport); } public function testEvents() { + $this->config + ->method('getSystemValue') + ->willReturnMap([ + ['mail_smtphost', '127.0.0.1', '127.0.0.1'], + ['mail_smtpport', 25, 25], + ]); + $this->mailer = $this->getMockBuilder(Mailer::class) + ->setMethods(['getInstance']) + ->setConstructorArgs( + [ + $this->config, + $this->logger, + $this->defaults, + $this->urlGenerator, + $this->l10n, + $this->dispatcher, + $this->createMock(IFactory::class) + ] + ) + ->getMock(); + $message = $this->createMock(Message::class); $event = new BeforeMessageSent($message); @@ -139,11 +174,7 @@ class MailerTest extends TestCase { ->method('dispatchTyped') ->with($this->equalTo($event)); - # We do not care at this point about errors in Swiftmailer - try { - $this->mailer->send($message); - } catch (Swift_SwiftException $e) { - } + $this->mailer->send($message); } public function testCreateMessage() { @@ -157,13 +188,20 @@ class MailerTest extends TestCase { public function testSendInvalidMailException() { + $this->config + ->method('getSystemValue') + ->willReturnMap([ + ['mail_smtphost', '127.0.0.1', '127.0.0.1'], + ['mail_smtpport', 25, 25], + ['mail_smtptimeout', 10, 10], + ]); $this->expectException(\Exception::class); $message = $this->getMockBuilder('\OC\Mail\Message') ->disableOriginalConstructor()->getMock(); $message->expects($this->once()) - ->method('getSwiftMessage') - ->willReturn(new \Swift_Message()); + ->method('getSymfonyEmail') + ->willReturn(new Email()); $this->mailer->send($message); } @@ -202,58 +240,76 @@ class MailerTest extends TestCase { $this->config->method('getSystemValue') ->willReturnMap([ ['mail_smtpmode', 'smtp', 'smtp'], - ['mail_smtpstreamoptions', [], ['foo' => 1]] + ['mail_smtpstreamoptions', [], ['foo' => 1]], + ['mail_smtphost', '127.0.0.1', '127.0.0.1'], + ['mail_smtpport', 25, 25], + ['mail_smtptimeout', 10, 10], ]); $mailer = self::invokePrivate($this->mailer, 'getInstance'); - $this->assertEquals(1, count($mailer->getTransport()->getStreamOptions())); - $this->assertTrue(isset($mailer->getTransport()->getStreamOptions()['foo'])); + /** @var EsmtpTransport $transport */ + $transport = self::invokePrivate($mailer, 'transport'); + $this->assertInstanceOf(EsmtpTransport::class, $transport); + $this->assertEquals(1, count($transport->getStream()->getStreamOptions())); + $this->assertTrue(isset($transport->getStream()->getStreamOptions()['foo'])); } public function testStreamingOptionsWrongType() { $this->config->method('getSystemValue') ->willReturnMap([ ['mail_smtpmode', 'smtp', 'smtp'], - ['mail_smtpstreamoptions', [], 'bar'] + ['mail_smtpstreamoptions', [], 'bar'], + ['mail_smtphost', '127.0.0.1', '127.0.0.1'], + ['mail_smtpport', 25, 25], + ['mail_smtptimeout', 10, 10], ]); $mailer = self::invokePrivate($this->mailer, 'getInstance'); - $this->assertEquals(0, count($mailer->getTransport()->getStreamOptions())); + /** @var EsmtpTransport $transport */ + $transport = self::invokePrivate($mailer, 'transport'); + $this->assertInstanceOf(EsmtpTransport::class, $transport); + $this->assertEquals(0, count($transport->getStream()->getStreamOptions())); } public function testLocalDomain(): void { $this->config->method('getSystemValue') ->willReturnMap([ - ['mail_smtpmode', 'smtp', 'smtp'] + ['mail_smtpmode', 'smtp', 'smtp'], + ['mail_smtphost', '127.0.0.1', '127.0.0.1'], + ['mail_smtpport', 25, 25], + ['mail_smtptimeout', 10, 10], ]); $this->config->method('getSystemValueString') ->with('overwrite.cli.url', '') ->willReturn('https://some.valid.url.com:8080'); - /** @var \Swift_Mailer $mailer */ + /** @var SymfonyMailer $mailer */ $mailer = self::invokePrivate($this->mailer, 'getInstance'); - self::assertInstanceOf(\Swift_Mailer::class, $mailer); + self::assertInstanceOf(SymfonyMailer::class, $mailer); - /** @var \Swift_Transport_EsmtpTransport $transport */ - $transport = $mailer->getTransport(); - self::assertInstanceOf(\Swift_Transport_EsmtpTransport::class, $transport); + /** @var EsmtpTransport $transport */ + $transport = self::invokePrivate($mailer, 'transport'); + self::assertInstanceOf(EsmtpTransport::class, $transport); self::assertEquals('some.valid.url.com', $transport->getLocalDomain()); } public function testLocalDomainInvalidUrl(): void { $this->config->method('getSystemValue') ->willReturnMap([ - ['mail_smtpmode', 'smtp', 'smtp'] + ['mail_smtpmode', 'smtp', 'smtp'], + ['mail_smtphost', '127.0.0.1', '127.0.0.1'], + ['mail_smtpport', 25, 25], + ['mail_smtptimeout', 10, 10], ]); $this->config->method('getSystemValueString') ->with('overwrite.cli.url', '') ->willReturn('https:only.slash.does.not.work:8080'); - /** @var \Swift_Mailer $mailer */ + /** @var SymfonyMailer $mailer */ $mailer = self::invokePrivate($this->mailer, 'getInstance'); - self::assertInstanceOf(\Swift_Mailer::class, $mailer); + self::assertInstanceOf(SymfonyMailer::class, $mailer); - /** @var \Swift_Transport_EsmtpTransport $transport */ - $transport = $mailer->getTransport(); - self::assertInstanceOf(\Swift_Transport_EsmtpTransport::class, $transport); + /** @var EsmtpTransport $transport */ + $transport = self::invokePrivate($mailer, 'transport'); + self::assertInstanceOf(EsmtpTransport::class, $transport); self::assertEquals('[127.0.0.1]', $transport->getLocalDomain()); } } diff --git a/tests/lib/Mail/MessageTest.php b/tests/lib/Mail/MessageTest.php index b97240d1336..2becc4d2081 100644 --- a/tests/lib/Mail/MessageTest.php +++ b/tests/lib/Mail/MessageTest.php @@ -11,12 +11,17 @@ namespace Test\Mail; use OC\Mail\Message; use OCP\Mail\Headers\AutoSubmitted; use OCP\Mail\IEMailTemplate; -use Swift_Message; +use Symfony\Component\Mime\Address; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Exception\RfcComplianceException; +use Symfony\Component\Mime\Header\HeaderInterface; +use Symfony\Component\Mime\Header\Headers; use Test\TestCase; +use PHPUnit\Framework\MockObject\MockObject; class MessageTest extends TestCase { - /** @var Swift_Message */ - private $swiftMessage; + /** @var Email */ + private $symfonyEmail; /** @var Message */ private $message; @@ -25,10 +30,26 @@ class MessageTest extends TestCase { */ public function mailAddressProvider() { return [ - [['lukas@owncloud.com' => 'Lukas Reschke'], ['lukas@owncloud.com' => 'Lukas Reschke']], - [['lukas@owncloud.com' => 'Lukas Reschke', 'lukas@öwnclöüd.com', 'lukäs@owncloud.örg' => 'Lükäs Réschke'], - ['lukas@owncloud.com' => 'Lukas Reschke', 'lukas@xn--wncld-iuae2c.com', 'lukäs@owncloud.xn--rg-eka' => 'Lükäs Réschke']], - [['lukas@öwnclöüd.com'], ['lukas@xn--wncld-iuae2c.com']], + [ + ['lukas@owncloud.com' => 'Lukas Reschke'], + [new Address('lukas@owncloud.com', 'Lukas Reschke')] + ], + [ + [ + 'lukas@owncloud.com' => 'Lukas Reschke', + 'lukas@öwnclöüd.com', + 'lukäs@owncloud.örg' => 'Lükäs Réschke' + ], + [ + new Address('lukas@owncloud.com', 'Lukas Reschke'), + new Address('lukas@öwnclöüd.com'), + new Address('lukäs@owncloud.örg', 'Lükäs Réschke') + ] + ], + [ + ['lukas@öwnclöüd.com'], + [new Address('lukas@öwnclöüd.com')] + ], ]; } @@ -37,7 +58,7 @@ class MessageTest extends TestCase { */ public function getMailAddressProvider() { return [ - [null, []], + [[], []], [['lukas@owncloud.com' => 'Lukas Reschke'], ['lukas@owncloud.com' => 'Lukas Reschke']], ]; } @@ -45,189 +66,158 @@ class MessageTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->swiftMessage = $this->getMockBuilder('\Swift_Message') + $this->symfonyEmail = $this->getMockBuilder(Email::class) ->disableOriginalConstructor()->getMock(); - $this->message = new Message($this->swiftMessage, false); + $this->message = new Message($this->symfonyEmail, false); } /** - * @requires function idn_to_ascii * @dataProvider mailAddressProvider * * @param string $unconverted * @param string $expected */ public function testConvertAddresses($unconverted, $expected) { - $this->assertSame($expected, self::invokePrivate($this->message, 'convertAddresses', [$unconverted])); - } - - public function testSetFrom() { - $this->swiftMessage - ->expects($this->once()) - ->method('setFrom') - ->with(['lukas@owncloud.com']); - $this->message->setFrom(['lukas@owncloud.com']); + $this->assertEquals($expected, self::invokePrivate($this->message, 'convertAddresses', [$unconverted])); } + public function testSetRecipients(): void { + $this->message = $this->message->setFrom(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']); + $this->message = $this->message->setTo(['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]); + $this->message = $this->message->setReplyTo(['penny@stardewvalley-library.co.edu' => 'Penny']); + $this->message = $this->message->setCc(['gunther@stardewvalley-library.co.edu' => 'Gunther']); + $this->message = $this->message->setBcc(['pam@stardewvalley-bus.com' => 'Pam']); - /** - * @dataProvider getMailAddressProvider - * - * @param $swiftresult - * @param $return - */ - public function testGetFrom($swiftresult, $return) { - $this->swiftMessage + $this->symfonyEmail + ->expects($this->once()) + ->method('from') + ->willReturn(new Address('pierres-general-store@stardewvalley.com', 'Pierres General Store')); + $this->symfonyEmail + ->expects($this->once()) + ->method('to') + ->willReturn(new Address('lewis-tent@stardewvalley.com', "Lewis' Tent Life")); + $this->symfonyEmail + ->expects($this->once()) + ->method('replyTo') + ->willReturn(new Address('penny@stardewvalley-library.co.edu', 'Penny')); + $this->symfonyEmail + ->expects($this->once()) + ->method('cc') + ->willReturn(new Address('gunther@stardewvalley-library.co.edu', 'Gunther')); + $this->symfonyEmail ->expects($this->once()) - ->method('getFrom') - ->willReturn($swiftresult); + ->method('bcc') + ->willReturn(new Address('pam@stardewvalley-bus.com', 'Pam')); - $this->assertSame($return, $this->message->getFrom()); + $this->message->setRecipients(); } - public function testSetReplyTo() { - $this->swiftMessage - ->expects($this->once()) - ->method('setReplyTo') - ->with(['lukas@owncloud.com']); - $this->message->setReplyTo(['lukas@owncloud.com']); - } + public function testSetTo() { + $expected = ['pierres-general-store@stardewvalley.com' => 'Pierres General Store']; - public function testGetReplyTo() { - $this->swiftMessage - ->expects($this->once()) - ->method('getReplyTo') - ->willReturn('lukas@owncloud.com'); + $message = $this->message->setTo(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']); - $this->assertSame('lukas@owncloud.com', $this->message->getReplyTo()); + $this->assertEquals($expected, $message->getTo()); } + public function testSetRecipientsException(): void { + $message = $this->message->setTo(['lewis-tent@~~~~.com' => "Lewis' Tent Life"]); - /** @dataProvider dataSetTo */ - public function testSetTo(array $to, array $expected) { - $this->swiftMessage + $this->symfonyEmail ->expects($this->once()) - ->method('setTo') - ->with($expected); - $this->message->setTo($to); - } + ->method('to') + ->willThrowException(new RfcComplianceException()); - public function dataSetTo(): array { - return [ - [['robot@example.com'], ['robot@example.com']], - [['robot'], ['robot' => 'robot']], - [['robot' => 'robot display name'], ['robot' => 'robot display name']], - [['example@🤖.com'], ['example@xn--yp9h.com']], - [['example@🤖.com' => 'A robot'], ['example@xn--yp9h.com' => 'A robot']], - ]; + $this->expectException(RfcComplianceException::class); + $message->setRecipients(); } - /** - * @dataProvider getMailAddressProvider - */ - public function testGetTo($swiftresult, $return) { - $this->swiftMessage + public function testSetRecipientsEmptyValues(): void { + $message = $this->message->setTo([]); + + $this->symfonyEmail ->expects($this->once()) - ->method('getTo') - ->willReturn($swiftresult); + ->method('to'); - $this->assertSame($return, $this->message->getTo()); + $message->setRecipients(); } - public function testSetCc() { - $this->swiftMessage - ->expects($this->once()) - ->method('setCc') - ->with(['lukas@owncloud.com']); - $this->message->setCc(['lukas@owncloud.com']); - } + public function testSetGetFrom() { + $expected = ['pierres-general-store@stardewvalley.com' => 'Pierres General Store']; - /** - * @dataProvider getMailAddressProvider - */ - public function testGetCc($swiftresult, $return) { - $this->swiftMessage - ->expects($this->once()) - ->method('getCc') - ->willReturn($swiftresult); + $message = $this->message->setFrom(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']); - $this->assertSame($return, $this->message->getCc()); + $this->assertEquals($expected, $message->getFrom()); } - public function testSetBcc() { - $this->swiftMessage - ->expects($this->once()) - ->method('setBcc') - ->with(['lukas@owncloud.com']); - $this->message->setBcc(['lukas@owncloud.com']); + public function testSetGetTo() { + $expected = ['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]; + + $message = $this->message->setTo(['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]); + + $this->assertEquals($expected, $message->getTo()); } - /** - * @dataProvider getMailAddressProvider - */ - public function testGetBcc($swiftresult, $return) { - $this->swiftMessage - ->expects($this->once()) - ->method('getBcc') - ->willReturn($swiftresult); + public function testSetGetReplyTo() { + $expected = ['penny@stardewvalley-library.co.edu' => 'Penny']; - $this->assertSame($return, $this->message->getBcc()); + $message = $this->message->setReplyTo(['penny@stardewvalley-library.co.edu' => 'Penny']); + + $this->assertEquals($expected, $message->getReplyTo()); } - public function testSetSubject() { - $this->swiftMessage - ->expects($this->once()) - ->method('setSubject') - ->with('Fancy Subject'); + public function testSetGetCC() { + $expected = ['gunther@stardewvalley-library.co.edu' => 'Gunther']; - $this->message->setSubject('Fancy Subject'); + $message = $this->message->setCc(['gunther@stardewvalley-library.co.edu' => 'Gunther']); + + $this->assertEquals($expected, $message->getCc()); } - public function testGetSubject() { - $this->swiftMessage - ->expects($this->once()) - ->method('getSubject') - ->willReturn('Fancy Subject'); + public function testSetGetBCC() { + $expected = ['pam@stardewvalley-bus.com' => 'Pam']; + + $message = $this->message->setBcc(['pam@stardewvalley-bus.com' => 'Pam']); - $this->assertSame('Fancy Subject', $this->message->getSubject()); + $this->assertEquals($expected, $message->getBcc()); } public function testSetPlainBody() { - $this->swiftMessage + $this->symfonyEmail ->expects($this->once()) - ->method('setBody') + ->method('text') ->with('Fancy Body'); $this->message->setPlainBody('Fancy Body'); } public function testGetPlainBody() { - $this->swiftMessage + $this->symfonyEmail ->expects($this->once()) - ->method('getBody') + ->method('getTextBody') ->willReturn('Fancy Body'); $this->assertSame('Fancy Body', $this->message->getPlainBody()); } public function testSetHtmlBody() { - $this->swiftMessage + $this->symfonyEmail ->expects($this->once()) - ->method('addPart') - ->with('<blink>Fancy Body</blink>', 'text/html'); + ->method('html') + ->with('<blink>Fancy Body</blink>', 'utf-8'); $this->message->setHtmlBody('<blink>Fancy Body</blink>'); } public function testPlainTextRenderOption() { - /** @var \PHPUnit\Framework\MockObject\MockObject|Swift_Message $swiftMessage */ - $swiftMessage = $this->getMockBuilder('\Swift_Message') + /** @var MockObject|Email $symfonyEmail */ + $symfonyEmail = $this->getMockBuilder(Email::class) ->disableOriginalConstructor()->getMock(); - /** @var \PHPUnit\Framework\MockObject\MockObject|IEMailTemplate $template */ - $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate') + /** @var MockObject|IEMailTemplate $template */ + $template = $this->getMockBuilder(IEMailTemplate::class) ->disableOriginalConstructor()->getMock(); - $message = new Message($swiftMessage, true); + $message = new Message($symfonyEmail, true); $template ->expects($this->never()) @@ -243,14 +233,14 @@ class MessageTest extends TestCase { } public function testBothRenderingOptions() { - /** @var \PHPUnit\Framework\MockObject\MockObject|Swift_Message $swiftMessage */ - $swiftMessage = $this->getMockBuilder('\Swift_Message') + /** @var MockObject|Email $symfonyEmail */ + $symfonyEmail = $this->getMockBuilder(Email::class) ->disableOriginalConstructor()->getMock(); - /** @var \PHPUnit\Framework\MockObject\MockObject|IEMailTemplate $template */ - $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate') + /** @var MockObject|IEMailTemplate $template */ + $template = $this->getMockBuilder(IEMailTemplate::class) ->disableOriginalConstructor()->getMock(); - $message = new Message($swiftMessage, false); + $message = new Message($symfonyEmail, false); $template ->expects($this->once()) @@ -266,108 +256,40 @@ class MessageTest extends TestCase { } public function testSetAutoSubmitted1() { - $swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet') - ->disableOriginalConstructor() - ->getMock(); - $swiftMessage = $this->getMockBuilder('\Swift_Message') - ->disableOriginalConstructor() - ->disableOriginalClone() - ->disableArgumentCloning() - ->disallowMockingUnknownTypes() - ->getMock(); - - $swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet); - - $swiftMimeSimpleHeaderSet->expects($this->once()) - ->method('has') - ->with('Auto-Submitted'); - $swiftMimeSimpleHeaderSet->expects($this->never()) - ->method('remove'); - $swiftMimeSimpleHeaderSet->expects($this->once()) - ->method('addTextHeader') - ->with('Auto-Submitted', AutoSubmitted::VALUE_AUTO_GENERATED); - - $message = new Message($swiftMessage, false); + $headers = new Headers($this->createMock(HeaderInterface::class)); + $headers->addTextHeader(AutoSubmitted::HEADER, "yes"); + $symfonyEmail = $this->createMock(Email::class); + + $symfonyEmail->method('getHeaders') + ->willReturn($headers); + + $message = new Message($symfonyEmail, false); $message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED); + $this->assertNotSame('no', $message->getAutoSubmitted()); } public function testSetAutoSubmitted2() { - $swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet') - ->disableOriginalConstructor() - ->getMock(); - $swiftMessage = $this->getMockBuilder('\Swift_Message') - ->disableOriginalConstructor() - ->disableOriginalClone() - ->disableArgumentCloning() - ->disallowMockingUnknownTypes() - ->getMock(); - - $swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet); - - $swiftMimeSimpleHeaderSet->expects($this->once()) - ->method('has') - ->with('Auto-Submitted') - ->willReturn(true); - $swiftMimeSimpleHeaderSet->expects($this->once()) - ->method('remove') - ->with('Auto-Submitted'); - $swiftMimeSimpleHeaderSet->expects($this->once()) - ->method('addTextHeader') - ->with('Auto-Submitted', AutoSubmitted::VALUE_AUTO_GENERATED); - - $message = new Message($swiftMessage, false); + $headers = new Headers($this->createMock(HeaderInterface::class)); + $headers->addTextHeader(AutoSubmitted::HEADER, 'no'); + $symfonyEmail = $this->createMock(Email::class); + + $symfonyEmail->method('getHeaders') + ->willReturn($headers); + + $message = new Message($symfonyEmail, false); $message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED); + $this->assertSame('auto-generated', $message->getAutoSubmitted()); } - public function testGetAutoSubmitted1() { - $swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet') - ->disableOriginalConstructor() - ->getMock(); - $swiftMessage = $this->getMockBuilder('\Swift_Message') - ->disableOriginalConstructor() - ->disableOriginalClone() - ->disableArgumentCloning() - ->disallowMockingUnknownTypes() - ->getMock(); - - $swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet); - - $swiftMimeSimpleHeaderSet->expects($this->once()) - ->method('has') - ->with('Auto-Submitted'); - $swiftMimeSimpleHeaderSet->expects($this->never()) - ->method('get'); - - $message = new Message($swiftMessage, false); + public function testGetAutoSubmitted() { + $headers = new Headers($this->createMock(HeaderInterface::class)); + $headers->addTextHeader(AutoSubmitted::HEADER, 'no'); + $symfonyEmail = $this->createMock(Email::class); + + $symfonyEmail->method('getHeaders') + ->willReturn($headers); + + $message = new Message($symfonyEmail, false); $this->assertSame("no", $message->getAutoSubmitted()); } - public function testGetAutoSubmitted2() { - $swiftMimeHeader = $this->getMockBuilder('\Swift_Mime_Header') - ->disableOriginalConstructor() - ->getMockForAbstractClass(); - $swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet') - ->disableOriginalConstructor() - ->getMock(); - $swiftMessage = $this->getMockBuilder('\Swift_Message') - ->disableOriginalConstructor() - ->disableOriginalClone() - ->disableArgumentCloning() - ->disallowMockingUnknownTypes() - ->getMock(); - - - $swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet); - $swiftMimeHeader->method('toString')->willReturn(AutoSubmitted::VALUE_AUTO_GENERATED); - - $swiftMimeSimpleHeaderSet->expects($this->once()) - ->method('has') - ->with('Auto-Submitted') - ->willReturn(true); - $swiftMimeSimpleHeaderSet->expects($this->once()) - ->method('get') - ->willReturn($swiftMimeHeader); - - $message = new Message($swiftMessage, false); - $this->assertSame(AutoSubmitted::VALUE_AUTO_GENERATED, $message->getAutoSubmitted()); - } } |