Skip to content

Commit 40b8e95

Browse files
minor symfony#57892 [Mime] Add tests on constraints (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [Mime] Add tests on constraints | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | on | New feature? | no | Deprecations? | no | Issues | - | License | MIT Commits ------- 3e41792 [Mime] Add tests on constraints
2 parents c9fbe07 + 3e41792 commit 40b8e95

File tree

10 files changed

+249
-1
lines changed

10 files changed

+249
-1
lines changed

src/Symfony/Component/Mime/Tests/AddressTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public function testCreate()
4444
$this->assertEquals($a, Address::create('[email protected]'));
4545
}
4646

47+
public function testCreateWithInvalidFormat()
48+
{
49+
$this->expectException(InvalidArgumentException::class);
50+
$this->expectExceptionMessage('Could not parse "<fabien@symfony" to a "Symfony\Component\Mime\Address" instance.');
51+
52+
Address::create('<fabien@symfony');
53+
}
54+
4755
/**
4856
* @dataProvider fromStringProvider
4957
*/

src/Symfony/Component/Mime/Tests/Encoder/IdnAddressEncoderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Mime\Encoder;
12+
namespace Symfony\Component\Mime\Tests\Encoder;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mime\Encoder\IdnAddressEncoder;
1516

1617
class IdnAddressEncoderTest extends TestCase
1718
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests\Encoder;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mime\Encoder\QpContentEncoder;
16+
17+
class QpContentEncoderTest extends TestCase
18+
{
19+
public function testReplaceLastChar()
20+
{
21+
$encoder = new QpContentEncoder();
22+
23+
$this->assertSame('message=09', $encoder->encodeString('message'.chr(0x09)));
24+
$this->assertSame('message=20', $encoder->encodeString('message'.chr(0x20)));
25+
}
26+
}

src/Symfony/Component/Mime/Tests/MessageTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ public function testGenerateMessageIdThrowsWhenHasFromButNoAddresses()
134134
$message->generateMessageId();
135135
}
136136

137+
public function testGenerateMessageIdThrowsWhenNeitherFromNorSenderIsPresent()
138+
{
139+
$message = new Message();
140+
141+
$this->expectException(LogicException::class);
142+
$this->expectExceptionMessage('An email must have a "From" or a "Sender" header.');
143+
144+
$message->generateMessageId();
145+
}
146+
137147
public function testToString()
138148
{
139149
$message = new Message();

src/Symfony/Component/Mime/Tests/Part/Multipart/FormDataPartTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,12 @@ public function testBoundaryContentTypeHeader()
228228
$headers[0]
229229
);
230230
}
231+
232+
public function testConstructThrowsOnUnexpectedFieldType()
233+
{
234+
$this->expectException(InvalidArgumentException::class);
235+
$this->expectExceptionMessage('A form field value can only be a string, an array, or an instance of TextPart ("stdClass" given).');
236+
237+
new FormDataPart(['foo' => new \stdClass()]);
238+
}
231239
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Mime\Email;
17+
use Symfony\Component\Mime\Header\Headers;
18+
use Symfony\Component\Mime\Test\Constraint\EmailAddressContains;
19+
20+
class EmailAddressContainsTest extends TestCase
21+
{
22+
public function testToString()
23+
{
24+
$constraint = new EmailAddressContains('headerName', 'expectedValue');
25+
26+
$this->assertSame('contains address "headerName" with value "expectedValue"', $constraint->toString());
27+
}
28+
29+
public function testFailureDescription()
30+
{
31+
$mailboxHeader = '[email protected]';
32+
$headers = new Headers();
33+
$headers->addMailboxHeader($mailboxHeader, '[email protected]');
34+
35+
$this->expectException(ExpectationFailedException::class);
36+
$this->expectExceptionMessage('Failed asserting that the Email contains address "[email protected]" with value "[email protected]" (value is [email protected]).');
37+
38+
(new EmailAddressContains($mailboxHeader, '[email protected]'))->evaluate(new Email($headers));
39+
}
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Mime\Email;
17+
use Symfony\Component\Mime\Test\Constraint\EmailAttachmentCount;
18+
19+
class EmailAttachmentCountTest extends TestCase
20+
{
21+
public function testToString()
22+
{
23+
$constraint = new EmailAttachmentCount(1);
24+
25+
$this->assertSame('has sent "1" attachment(s)', $constraint->toString());
26+
}
27+
28+
public function testFailureDescription()
29+
{
30+
$email = new Email();
31+
$email->attach('attachment content', 'attachment.txt');
32+
33+
$this->expectException(ExpectationFailedException::class);
34+
$this->expectExceptionMessage('Failed asserting that the Email has sent "2" attachment(s).');
35+
36+
(new EmailAttachmentCount(2))->evaluate($email);
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Mime\Email;
17+
use Symfony\Component\Mime\Header\Headers;
18+
use Symfony\Component\Mime\Test\Constraint\EmailHasHeader;
19+
20+
class EmailHasHeaderTest extends TestCase
21+
{
22+
public function testToString()
23+
{
24+
$constraint = new EmailHasHeader('headerName');
25+
26+
$this->assertSame('has header "headerName"', $constraint->toString());
27+
}
28+
29+
public function testFailureDescription()
30+
{
31+
$headers = new Headers();
32+
$headers->addMailboxHeader('headerName', '[email protected]');
33+
34+
$this->expectException(ExpectationFailedException::class);
35+
$this->expectExceptionMessage('Failed asserting that the Email has header "not existing header".');
36+
37+
(new EmailHasHeader('not existing header'))->evaluate(new Email($headers));
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Mime\Email;
17+
use Symfony\Component\Mime\Test\Constraint\EmailHtmlBodyContains;
18+
19+
class EmailHtmlBodyContainsTest extends TestCase
20+
{
21+
public function testToString()
22+
{
23+
$constraint = new EmailHtmlBodyContains('expectedValue');
24+
25+
$this->assertSame('contains "expectedValue"', $constraint->toString());
26+
}
27+
28+
public function testFailureDescription()
29+
{
30+
$expectedValue = 'expectedValue';
31+
$email = new Email();
32+
$email->html('actualValue')->text($expectedValue);
33+
34+
$this->expectException(ExpectationFailedException::class);
35+
$this->expectExceptionMessage('Failed asserting that the Email HTML body contains "expectedValue".');
36+
37+
(new EmailHtmlBodyContains($expectedValue))->evaluate($email);
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Mime\Email;
17+
use Symfony\Component\Mime\Test\Constraint\EmailTextBodyContains;
18+
19+
class EmailTextBodyContainsTest extends TestCase
20+
{
21+
public function testToString()
22+
{
23+
$constraint = new EmailTextBodyContains('expectedValue');
24+
25+
$this->assertSame('contains "expectedValue"', $constraint->toString());
26+
}
27+
28+
public function testFailureDescription()
29+
{
30+
$expectedValue = 'expectedValue';
31+
$email = new Email();
32+
$email->html($expectedValue)->text('actualValue');
33+
34+
$this->expectException(ExpectationFailedException::class);
35+
$this->expectExceptionMessage('Failed asserting that the Email text body contains "expectedValue".');
36+
37+
(new EmailTextBodyContains($expectedValue))->evaluate($email);
38+
}
39+
}

0 commit comments

Comments
 (0)