From 3e4179267a6c5bd2ba1c910f7dbdde09af878f41 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 31 Jul 2024 10:36:38 +0200 Subject: [PATCH] [Mime] Add tests on constraints --- .../Component/Mime/Tests/AddressTest.php | 8 ++++ .../Tests/Encoder/IdnAddressEncoderTest.php | 3 +- .../Tests/Encoder/QpContentEncoderTest.php | 26 ++++++++++++ .../Component/Mime/Tests/MessageTest.php | 10 +++++ .../Tests/Part/Multipart/FormDataPartTest.php | 8 ++++ .../Constraint/EmailAddressContainsTest.php | 40 +++++++++++++++++++ .../Constraint/EmailAttachmentCountTest.php | 38 ++++++++++++++++++ .../Test/Constraint/EmailHasHeaderTest.php | 39 ++++++++++++++++++ .../Constraint/EmailHtmlBodyContainsTest.php | 39 ++++++++++++++++++ .../Constraint/EmailTextBodyContainsTest.php | 39 ++++++++++++++++++ 10 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Mime/Tests/Encoder/QpContentEncoderTest.php create mode 100644 src/Symfony/Component/Mime/Tests/Test/Constraint/EmailAddressContainsTest.php create mode 100644 src/Symfony/Component/Mime/Tests/Test/Constraint/EmailAttachmentCountTest.php create mode 100644 src/Symfony/Component/Mime/Tests/Test/Constraint/EmailHasHeaderTest.php create mode 100644 src/Symfony/Component/Mime/Tests/Test/Constraint/EmailHtmlBodyContainsTest.php create mode 100644 src/Symfony/Component/Mime/Tests/Test/Constraint/EmailTextBodyContainsTest.php diff --git a/src/Symfony/Component/Mime/Tests/AddressTest.php b/src/Symfony/Component/Mime/Tests/AddressTest.php index fe10c73910bde..5e7bc6105df7c 100644 --- a/src/Symfony/Component/Mime/Tests/AddressTest.php +++ b/src/Symfony/Component/Mime/Tests/AddressTest.php @@ -44,6 +44,14 @@ public function testCreate() $this->assertEquals($a, Address::create('fabien@symfony.com')); } + public function testCreateWithInvalidFormat() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Could not parse " + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime\Tests\Encoder; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\Encoder\QpContentEncoder; + +class QpContentEncoderTest extends TestCase +{ + public function testReplaceLastChar() + { + $encoder = new QpContentEncoder(); + + $this->assertSame('message=09', $encoder->encodeString('message'.chr(0x09))); + $this->assertSame('message=20', $encoder->encodeString('message'.chr(0x20))); + } +} diff --git a/src/Symfony/Component/Mime/Tests/MessageTest.php b/src/Symfony/Component/Mime/Tests/MessageTest.php index 6d981e7eb1f6d..9f5fc1f713ac7 100644 --- a/src/Symfony/Component/Mime/Tests/MessageTest.php +++ b/src/Symfony/Component/Mime/Tests/MessageTest.php @@ -134,6 +134,16 @@ public function testGenerateMessageIdThrowsWhenHasFromButNoAddresses() $message->generateMessageId(); } + public function testGenerateMessageIdThrowsWhenNeitherFromNorSenderIsPresent() + { + $message = new Message(); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('An email must have a "From" or a "Sender" header.'); + + $message->generateMessageId(); + } + public function testToString() { $message = new Message(); diff --git a/src/Symfony/Component/Mime/Tests/Part/Multipart/FormDataPartTest.php b/src/Symfony/Component/Mime/Tests/Part/Multipart/FormDataPartTest.php index d86c5f84da131..22e38c5db235e 100644 --- a/src/Symfony/Component/Mime/Tests/Part/Multipart/FormDataPartTest.php +++ b/src/Symfony/Component/Mime/Tests/Part/Multipart/FormDataPartTest.php @@ -228,4 +228,12 @@ public function testBoundaryContentTypeHeader() $headers[0] ); } + + public function testConstructThrowsOnUnexpectedFieldType() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('A form field value can only be a string, an array, or an instance of TextPart ("stdClass" given).'); + + new FormDataPart(['foo' => new \stdClass()]); + } } diff --git a/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailAddressContainsTest.php b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailAddressContainsTest.php new file mode 100644 index 0000000000000..227a51f58a4b7 --- /dev/null +++ b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailAddressContainsTest.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime\Tests\Test\Constraint; + +use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Header\Headers; +use Symfony\Component\Mime\Test\Constraint\EmailAddressContains; + +class EmailAddressContainsTest extends TestCase +{ + public function testToString() + { + $constraint = new EmailAddressContains('headerName', 'expectedValue'); + + $this->assertSame('contains address "headerName" with value "expectedValue"', $constraint->toString()); + } + + public function testFailureDescription() + { + $mailboxHeader = 'text@example.com'; + $headers = new Headers(); + $headers->addMailboxHeader($mailboxHeader, 'actualValue@example.com'); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('Failed asserting that the Email contains address "text@example.com" with value "expectedValue@example.com" (value is actualValue@example.com).'); + + (new EmailAddressContains($mailboxHeader, 'expectedValue@example.com'))->evaluate(new Email($headers)); + } +} diff --git a/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailAttachmentCountTest.php b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailAttachmentCountTest.php new file mode 100644 index 0000000000000..60976675ab3d5 --- /dev/null +++ b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailAttachmentCountTest.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime\Tests\Test\Constraint; + +use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Test\Constraint\EmailAttachmentCount; + +class EmailAttachmentCountTest extends TestCase +{ + public function testToString() + { + $constraint = new EmailAttachmentCount(1); + + $this->assertSame('has sent "1" attachment(s)', $constraint->toString()); + } + + public function testFailureDescription() + { + $email = new Email(); + $email->attach('attachment content', 'attachment.txt'); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('Failed asserting that the Email has sent "2" attachment(s).'); + + (new EmailAttachmentCount(2))->evaluate($email); + } +} diff --git a/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailHasHeaderTest.php b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailHasHeaderTest.php new file mode 100644 index 0000000000000..ae5f75fddfb55 --- /dev/null +++ b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailHasHeaderTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime\Tests\Test\Constraint; + +use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Header\Headers; +use Symfony\Component\Mime\Test\Constraint\EmailHasHeader; + +class EmailHasHeaderTest extends TestCase +{ + public function testToString() + { + $constraint = new EmailHasHeader('headerName'); + + $this->assertSame('has header "headerName"', $constraint->toString()); + } + + public function testFailureDescription() + { + $headers = new Headers(); + $headers->addMailboxHeader('headerName', 'test@example.com'); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('Failed asserting that the Email has header "not existing header".'); + + (new EmailHasHeader('not existing header'))->evaluate(new Email($headers)); + } +} diff --git a/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailHtmlBodyContainsTest.php b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailHtmlBodyContainsTest.php new file mode 100644 index 0000000000000..ae994b0959796 --- /dev/null +++ b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailHtmlBodyContainsTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime\Tests\Test\Constraint; + +use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Test\Constraint\EmailHtmlBodyContains; + +class EmailHtmlBodyContainsTest extends TestCase +{ + public function testToString() + { + $constraint = new EmailHtmlBodyContains('expectedValue'); + + $this->assertSame('contains "expectedValue"', $constraint->toString()); + } + + public function testFailureDescription() + { + $expectedValue = 'expectedValue'; + $email = new Email(); + $email->html('actualValue')->text($expectedValue); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('Failed asserting that the Email HTML body contains "expectedValue".'); + + (new EmailHtmlBodyContains($expectedValue))->evaluate($email); + } +} diff --git a/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailTextBodyContainsTest.php b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailTextBodyContainsTest.php new file mode 100644 index 0000000000000..43ba0170ef52c --- /dev/null +++ b/src/Symfony/Component/Mime/Tests/Test/Constraint/EmailTextBodyContainsTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime\Tests\Test\Constraint; + +use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Test\Constraint\EmailTextBodyContains; + +class EmailTextBodyContainsTest extends TestCase +{ + public function testToString() + { + $constraint = new EmailTextBodyContains('expectedValue'); + + $this->assertSame('contains "expectedValue"', $constraint->toString()); + } + + public function testFailureDescription() + { + $expectedValue = 'expectedValue'; + $email = new Email(); + $email->html($expectedValue)->text('actualValue'); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('Failed asserting that the Email text body contains "expectedValue".'); + + (new EmailTextBodyContains($expectedValue))->evaluate($email); + } +}