Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mime] Add tests on constraints #57892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Symfony/Component/Mime/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
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 src/Symfony/Component/Mime/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 src/Symfony/Component/Mime/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
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()]);
}
}
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));
}
}
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);
}
}
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));
}
}
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);
}
}
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);
}
}
Loading