Skip to content

Commit

Permalink
Add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zbateson committed Apr 22, 2024
1 parent f8f6f5c commit 4ec0f2c
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 21 deletions.
6 changes: 3 additions & 3 deletions tests/MailMimeParser/Header/Part/AddressGroupPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPUnit\Framework\TestCase;

use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use ZBateson\MbWrapper\MbWrapper;

/**
Expand Down Expand Up @@ -46,6 +45,7 @@ public function testNameGroup() : void

$part = $this->newAddressGroupPart([$name], $members);
$this->assertEquals('Roman Senate', $part->getName());
$this->assertEquals('Roman Senate', $part->getValue());
$this->assertEquals($members, $part->getAddresses());
$this->assertEquals($members[0], $part->getAddress(0));
$this->assertEquals($members[1], $part->getAddress(1));
Expand All @@ -58,9 +58,9 @@ public function testValidation() : void
$part = $this->newAddressGroupPart([], []);
$errs = $part->getErrors(true, LogLevel::NOTICE);
$this->assertCount(2, $errs);
$this->assertEquals('Address group doesn\'t have a name', $errs[0]->getMessage());
$this->assertNotEmpty($errs[0]->getMessage());
$this->assertEquals(LogLevel::ERROR, $errs[0]->getPsrLevel());
$this->assertEquals('Address group doesn\'t have any email addresses defined in it', $errs[1]->getMessage());
$this->assertNotEmpty($errs[1]->getMessage());
$this->assertEquals(LogLevel::NOTICE, $errs[1]->getPsrLevel());
}
}
47 changes: 46 additions & 1 deletion tests/MailMimeParser/Header/Part/AddressPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ class AddressPartTest extends TestCase
{
// @phpstan-ignore-next-line
private $mb;
private $hpf;
private $logger;

protected function setUp() : void
{
$this->logger = \mmpGetTestLogger();
$this->mb = new MbWrapper();
$this->hpf = $this->getMockBuilder(HeaderPartFactory::class)
->setConstructorArgs([$this->logger, $this->mb])
->setMethods()
->getMock();
}

private function newAddressPart($nameParts, $valueParts)
Expand All @@ -40,6 +45,22 @@ private function getTokenMock(string $name) : Token
->getMock();
}

private function getQuotedMock(string $name) : QuotedLiteralPart
{
return $this->getMockBuilder(QuotedLiteralPart::class)
->setConstructorArgs([$this->logger, $this->mb, [$this->getTokenMock($name)]])
->setMethods()
->getMock();
}

private function getCommentMock(string $name) : CommentPart
{
return $this->getMockBuilder(CommentPart::class)
->setConstructorArgs([$this->logger, $this->mb, $this->hpf, [$this->getTokenMock($name)]])
->setMethods()
->getMock();
}

public function testNameEmail() : void
{
$name = 'Julius Caeser';
Expand All @@ -49,12 +70,36 @@ public function testNameEmail() : void
$this->assertEquals($email, $part->getEmail());
}

public function testEmailWithQuotedParts() : void
{
$email = 'gaius" julius "[email protected]';
$part = $this->newAddressPart([], [$this->getTokenMock('gaius '), $this->getQuotedMock(' julius '), $this->getTokenMock(' caesa [email protected]')]);
$this->assertEquals($email, $part->getEmail());
}

public function testEmailWithCommentsAndQuotedParts() : void
{
$email = 'gaius"julius"[email protected]';
$part = $this->newAddressPart([], [
$this->getTokenMock('gaius '),
$this->getQuotedMock('julius'),
$this->getTokenMock('caesar'),
$this->getCommentMock('emperor, innit'),
$this->getTokenMock('@altavista.com')
]);
$this->assertEquals($email, $part->getEmail());
$comments = $part->getComments();
$this->assertNotEmpty($comments);
$this->assertCount(1, $comments);
$this->assertEquals('emperor, innit', $comments[0]->getComment());
}

public function testValidation() : void
{
$part = $this->newAddressPart([], []);
$errs = $part->getErrors(true, LogLevel::ERROR);
$this->assertCount(1, $errs);
$this->assertEquals('Address doesn\'t contain an email address', $errs[0]->getMessage());
$this->assertNotEmpty($errs[0]->getMessage());
$this->assertEquals(LogLevel::ERROR, $errs[0]->getPsrLevel());
}
}
24 changes: 19 additions & 5 deletions tests/MailMimeParser/Header/Part/CommentPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ZBateson\MailMimeParser\Header\Part;

use Psr\Log\NullLogger;
use PHPUnit\Framework\TestCase;
use ZBateson\MbWrapper\MbWrapper;

Expand Down Expand Up @@ -34,12 +33,20 @@ protected function setUp() : void

private function getTokenMock(string $name) : Token
{
return $this->getMockBuilder(MimeToken::class)
return $this->getMockBuilder(Token::class)
->setConstructorArgs([$this->logger, $this->mb, $name])
->setMethods()
->getMock();
}

private function getQuotedMock(string $name) : QuotedLiteralPart
{
return $this->getMockBuilder(QuotedLiteralPart::class)
->setConstructorArgs([$this->logger, $this->mb, [$this->getTokenMock($name)]])
->setMethods()
->getMock();
}

private function newCommentPart($childParts)
{
return new CommentPart($this->logger, $this->mb, $this->hpf, $childParts);
Expand All @@ -53,10 +60,17 @@ public function testBasicComment() : void
$this->assertEquals($comment, $part->getComment());
}

public function testMimeEncoding() : void
public function testNestedCommentPartStringValue() : void
{
$part = $this->newCommentPart([$this->getTokenMock('=?US-ASCII?Q?Kilgore_Trout?=')]);
$comment = 'Some ((very) silly) "comment" made about my moustache';
$part = $this->newCommentPart([
$this->getTokenMock('Some '),
$this->newCommentPart([$this->newCommentPart([$this->getTokenMock('very')]), $this->getTokenMock(' silly')]),
$this->getTokenMock(' '),
$this->getQuotedMock('comment'),
$this->getTokenMock(' made about my moustache')
]);
$this->assertEquals('', $part->getValue());
$this->assertEquals('Kilgore Trout', $part->getComment());
$this->assertEquals($comment, $part->getComment());
}
}
95 changes: 85 additions & 10 deletions tests/MailMimeParser/Header/Part/ContainerPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use ZBateson\MbWrapper\MbWrapper;

/**
* Description of LiteralTest
* Description of ContainerPartTest
*
* @group HeaderParts
* @group ContainerPart
Expand All @@ -18,34 +18,109 @@ class ContainerPartTest extends TestCase
{
// @phpstan-ignore-next-line
private $mb;
private $hpf;
private $logger;

protected function setUp() : void
{
$this->logger = \mmpGetTestLogger();
$this->mb = new MbWrapper();
$this->hpf = $this->getMockBuilder(HeaderPartFactory::class)
->setConstructorArgs([$this->logger, $this->mb])
->setMethods()
->getMock();
}

private function getTokenArray(string $name) : array
private function getTokenArray(string ...$args) : array
{
return [$this->getMockBuilder(MimeToken::class)
->setConstructorArgs([$this->logger, $this->mb, $name])
->setMethods()
->getMock()];
return \array_map(
fn ($a) => $this->getMockBuilder(Token::class)
->setConstructorArgs([$this->logger, $this->mb, $a])
->setMethods()
->getMock(),
$args
);
}

private function getCommentPart(string $comment) : CommentPart
{
return $this->getMockBuilder(CommentPart::class)
->setConstructorArgs([$this->logger, $this->mb, $this->hpf, $this->getTokenArray($comment)])
->setMethods()
->getMock();
}

private function newContainerPart($childParts)
private function newContainerPart($childParts) : ContainerPart
{
return new ContainerPart($this->logger, $this->mb, $childParts);
}

public function testInstance() : void
{
$part = $this->newContainerPart($this->getTokenArray('"'));
$part = $this->newContainerPart($this->getTokenArray('Kilgore Trout'));
$this->assertNotNull($part);
$this->assertEquals('Kilgore Trout', $part->getValue());
}

public function testIgnorableSpaces() : void
{
$part = $this->newContainerPart($this->getTokenArray(' ', 'Kilgore', ' ' , ' ', "\n\t ", 'Trout', ' ', "\n ", ' '));
$this->assertNotNull($part);
$this->assertEquals('"', $part->getValue());
$this->assertEquals('Kilgore Trout', $part->getValue());
}

$part = $this->newContainerPart($this->getTokenArray('=?US-ASCII?Q?Kilgore_Trout?='));
public function testIgnorableSpacesWithComments() : void
{
$part = $this->newContainerPart(\array_merge(
$this->getTokenArray(' ', 'Kilgore', ' '),
[$this->getCommentPart('test a comment')],
$this->getTokenArray(' ', "\n\t ", 'Trout', ' '),
[$this->getCommentPart('test another comment')],
$this->getTokenArray(' ')
));
$this->assertNotNull($part);
$this->assertEquals('Kilgore Trout', $part->getValue());
}

public function testGetChildParts() : void
{
$children = $this->getTokenArray(' ', 'Kilgore', ' ' , ' ', "\n\t ", 'Trout', ' ', "\n ", ' ');
$part = $this->newContainerPart($children);
$this->assertNotNull($part);
$this->assertEquals('Kilgore Trout', $part->getValue());
$this->assertCount(9, $part->getChildParts());
$this->assertEquals($children, $part->getChildParts());
}

public function testGetChildPartsWithComments() : void
{
$children = \array_merge(
$this->getTokenArray(' ', 'Kilgore', ' '),
[$this->getCommentPart('test a comment')],
$this->getTokenArray(' ', "\n\t ", 'Trout', ' '),
[$this->getCommentPart('test another comment')],
$this->getTokenArray(' ')
);
$part = $this->newContainerPart($children);
$this->assertNotNull($part);
$this->assertEquals('Kilgore Trout', $part->getValue());
$this->assertCount(10, $part->getChildParts());
$this->assertEquals($children, $part->getChildParts());
$this->assertCount(2, $part->getComments());
$this->assertEquals([$children[3], $children[8]], $part->getComments());
}

public function testErrorOnChildPart() : void
{
$tokens = $this->getTokenArray('Kilgore', ' ');
$part = $this->newContainerPart([$this->newContainerPart($tokens), $this->getTokenArray(' ')[0], $this->newContainerPart($this->getTokenArray(' ', 'Trout'))]);
$tokens[0]->addError('Test', \Psr\Log\LogLevel::ERROR);

$this->assertNotNull($part);
$this->assertEquals('Kilgore Trout', $part->getValue());

$errors = $part->getAllErrors();
$this->assertCount(1, $errors);
$this->assertEquals('Test', $errors[0]->getMessage());
}
}
Loading

0 comments on commit 4ec0f2c

Please sign in to comment.