From eb930b8229f6d14f81de607a8979e425d6f98ad8 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 --- Tests/AddressTest.php | 8 ++++ Tests/Encoder/IdnAddressEncoderTest.php | 3 +- Tests/Encoder/QpContentEncoderTest.php | 26 ++++++++++++ Tests/MessageTest.php | 10 +++++ Tests/Part/Multipart/FormDataPartTest.php | 8 ++++ .../Constraint/EmailAddressContainsTest.php | 40 +++++++++++++++++++ .../Constraint/EmailAttachmentCountTest.php | 38 ++++++++++++++++++ Tests/Test/Constraint/EmailHasHeaderTest.php | 39 ++++++++++++++++++ .../Constraint/EmailHtmlBodyContainsTest.php | 39 ++++++++++++++++++ .../Constraint/EmailTextBodyContainsTest.php | 39 ++++++++++++++++++ 10 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 Tests/Encoder/QpContentEncoderTest.php create mode 100644 Tests/Test/Constraint/EmailAddressContainsTest.php create mode 100644 Tests/Test/Constraint/EmailAttachmentCountTest.php create mode 100644 Tests/Test/Constraint/EmailHasHeaderTest.php create mode 100644 Tests/Test/Constraint/EmailHtmlBodyContainsTest.php create mode 100644 Tests/Test/Constraint/EmailTextBodyContainsTest.php diff --git a/Tests/AddressTest.php b/Tests/AddressTest.php index fe10c73..5e7bc61 100644 --- a/Tests/AddressTest.php +++ b/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/Tests/MessageTest.php b/Tests/MessageTest.php index 6d981e7..9f5fc1f 100644 --- a/Tests/MessageTest.php +++ b/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/Tests/Part/Multipart/FormDataPartTest.php b/Tests/Part/Multipart/FormDataPartTest.php index d86c5f8..22e38c5 100644 --- a/Tests/Part/Multipart/FormDataPartTest.php +++ b/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/Tests/Test/Constraint/EmailAddressContainsTest.php b/Tests/Test/Constraint/EmailAddressContainsTest.php new file mode 100644 index 0000000..227a51f --- /dev/null +++ b/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/Tests/Test/Constraint/EmailAttachmentCountTest.php b/Tests/Test/Constraint/EmailAttachmentCountTest.php new file mode 100644 index 0000000..6097667 --- /dev/null +++ b/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/Tests/Test/Constraint/EmailHasHeaderTest.php b/Tests/Test/Constraint/EmailHasHeaderTest.php new file mode 100644 index 0000000..ae5f75f --- /dev/null +++ b/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/Tests/Test/Constraint/EmailHtmlBodyContainsTest.php b/Tests/Test/Constraint/EmailHtmlBodyContainsTest.php new file mode 100644 index 0000000..ae994b0 --- /dev/null +++ b/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/Tests/Test/Constraint/EmailTextBodyContainsTest.php b/Tests/Test/Constraint/EmailTextBodyContainsTest.php new file mode 100644 index 0000000..43ba017 --- /dev/null +++ b/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); + } +}