Skip to content

Commit

Permalink
[Mime] Add tests on constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Aug 8, 2024
1 parent c71c7a1 commit eb930b8
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Tests/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function testCreate()
$this->assertEquals($a, Address::create('[email protected]'));
}

public function testCreateWithInvalidFormat()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Could not parse "<fabien@symfony" to a "Symfony\Component\Mime\Address" instance.');

Address::create('<fabien@symfony');
}

/**
* @dataProvider fromStringProvider
*/
Expand Down
3 changes: 2 additions & 1 deletion Tests/Encoder/IdnAddressEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mime\Encoder;
namespace Symfony\Component\Mime\Tests\Encoder;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Encoder\IdnAddressEncoder;

class IdnAddressEncoderTest extends TestCase
{
Expand Down
26 changes: 26 additions & 0 deletions Tests/Encoder/QpContentEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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)));
}
}
10 changes: 10 additions & 0 deletions Tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions Tests/Part/Multipart/FormDataPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
}
}
40 changes: 40 additions & 0 deletions Tests/Test/Constraint/EmailAddressContainsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 = '[email protected]';
$headers = new Headers();
$headers->addMailboxHeader($mailboxHeader, '[email protected]');

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('Failed asserting that the Email contains address "[email protected]" with value "[email protected]" (value is [email protected]).');

(new EmailAddressContains($mailboxHeader, '[email protected]'))->evaluate(new Email($headers));
}
}
38 changes: 38 additions & 0 deletions Tests/Test/Constraint/EmailAttachmentCountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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);
}
}
39 changes: 39 additions & 0 deletions Tests/Test/Constraint/EmailHasHeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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', '[email protected]');

$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));
}
}
39 changes: 39 additions & 0 deletions Tests/Test/Constraint/EmailHtmlBodyContainsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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);
}
}
39 changes: 39 additions & 0 deletions Tests/Test/Constraint/EmailTextBodyContainsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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);
}
}

0 comments on commit eb930b8

Please sign in to comment.