Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zbateson committed Apr 15, 2024
1 parent 5a9eac7 commit a626bad
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,43 @@ public function testMultipleTokens() : void
$this->assertCount(6, $ret);
}

public function testLiteralTokens() : void
{
$value = "Je\ suis\\\nici\\\r\noui";
$mock = $this->getMockBuilder(Token::class)->disableOriginalConstructor();
$args = ['Je', ' ', 'suis', "\n", 'ici', "\r\n", 'oui'];
$parts = [
$mock->getMock(),
$mock->getMock(),
$mock->getMock(),
$mock->getMock(),
$mock->getMock(),
$mock->getMock(),
$mock->getMock()
];

$stub = $this->abstractConsumerStub;

$stub->expects($this->exactly(7))
->method('getPartForToken')
->withConsecutive(
[$args[0], false],
[$args[1], true],
[$args[2], false],
[$args[3], true],
[$args[4], false],
[$args[5], true],
[$args[6], false]
)
->will($this->onConsecutiveCalls($parts[0], $parts[1], $parts[2], $parts[3], $parts[4], $parts[5], $parts[6]));
$stub->method('processParts')
->willReturn($parts);

$ret = $stub($value);
$this->assertNotEmpty($ret);
$this->assertCount(7, $ret);
}

public function testInvokeWithEmptyValue() : void
{
$stub = $this->abstractConsumerStub;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ protected function setUp() : void

public function testConsumeTokens() : void
{
$value = "Je\ \t suis\nici";
$value = "Je\ \t suis\n ici";

$ret = $this->genericConsumer->__invoke($value);
$this->assertNotEmpty($ret);
$this->assertCount(1, $ret);
$this->assertEquals('Je suis ici', $ret[0]);
$this->assertEquals('Je suis ici', $ret[0]->getValue());
}

public function testFilterSpacesBetweenMimeParts() : void
Expand All @@ -64,4 +64,42 @@ public function testFilterSpacesBetweenMimeParts() : void
$this->assertCount(1, $ret);
$this->assertEquals('Jesuisici', $ret[0]);
}

protected function assertDecoded($expected, $encodedActual)
{
$ret = $this->genericConsumer->__invoke($encodedActual);
$this->assertNotEmpty($ret);
$this->assertCount(1, $ret);
$this->assertEquals($expected, $ret[0]->getValue());
}

public function testDecodingTwoParts() : void
{
$kilgore = '=?US-ASCII?Q?Kilgore_Trout?=';
$snow = '=?US-ASCII?Q?Jon_Snow?=';

$this->assertDecoded(
'Kilgore TroutJon Snow',
" $kilgore $snow "
);
$this->assertDecoded(
'Kilgore TroutJon Snow',
"{$kilgore}{$snow}"
);
$this->assertDecoded(
'Kilgore Trout Jon',
"$kilgore Jon"
);
$this->assertDecoded(
'Kilgore Jon Snow',
"Kilgore $snow"
);
$this->assertDecoded(
'KilgoreJon SnowTrout',
"Kilgore{$snow}Trout"
);
$this->assertDecoded('外為オンラインデモ(25)(デモ)決済約定のお知らせ', '=?iso-2022-jp?Q?=1B$B300Y=1B(I5]W2]C^S=1B(B(25?=
=?iso-2022-jp?Q?)(=1B$B%G%b=1B(B)=1B$B7h:QLsDj$N$*CN$i$;=1B(B?=');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ protected function setUp() : void

public function testConsumeTokens() : void
{
$value = "Je\ \t suis\nici";
$value = "Je\ \t suis\n ici";

$ret = $this->genericConsumer->__invoke($value);
$this->assertNotEmpty($ret);
$this->assertCount(1, $ret);
$this->assertEquals('Je suis ici', $ret[0]);
$this->assertEquals('Je suis ici', $ret[0]->getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ protected function setUp() : void
->setConstructorArgs([$this->logger, $mpf, $qscs])
->setMethods()
->getMock();
$qsmlpcs = $this->getMockBuilder(QuotedStringMimeLiteralPartConsumerService::class)
->setConstructorArgs([$this->logger, $pf])
->setMethods()
->getMock();
$pvcs = $this->getMockBuilder(ParameterValueConsumerService::class)
->setConstructorArgs([$this->logger, $mpf, $ccs, $qscs])
->setConstructorArgs([$this->logger, $mpf, $ccs, $qsmlpcs])
->setMethods()
->getMock();
$pnvcs = $this->getMockBuilder(ParameterNameValueConsumerService::class)
Expand Down Expand Up @@ -247,6 +251,6 @@ public function testSplitHeaderWithMultipleSplitRfc2047() : void
. '=?US-ASCII?Q?TS_E?= =?US-ASCII?Q?li"; condiments*1="ot?="');
$this->assertNotEmpty($ret);
$this->assertCount(2, $ret);
$this->assertEquals('TS Eliot', $ret[1]->getValue());
$this->assertEquals('TS E liot', $ret[1]->getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testConsumeParts() : void
foreach ($aTests as $test) {
$ret = $this->domainConsumer->__invoke($test[0]);
$this->assertNotEmpty($ret, $test[0]);
$this->assertCount(1 + \count($test[2]), $ret, $test[0]);
$this->assertCount(1, $ret, $test[0], $test);

$pt = $test[1];
$domPart = $ret[0];
Expand All @@ -92,9 +92,10 @@ public function testConsumeParts() : void
}

foreach ($test[2] as $comment) {
$this->assertNotNull($ret[1], $test[0]);
$this->assertInstanceOf('\\' . \ZBateson\MailMimeParser\Header\Part\CommentPart::class, $ret[1], $test[0]);
$this->assertEquals($comment, $ret[1]->getComment(), $test[0]);
$comms = $ret[0]->getComments();
$this->assertNotEmpty($comms, $test[0]);
$this->assertInstanceOf('\\' . \ZBateson\MailMimeParser\Header\Part\CommentPart::class, $comms[0], $test[0]);
$this->assertEquals($comment, $comms[0]->getComment(), $test[0]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ protected function setUp() : void

public function testConsumeTokens() : void
{
$value = "Je \t suis\nici";
$value = "Je \t suis\n ici";

$ret = $this->genericConsumer->__invoke($value);
$this->assertNotEmpty($ret);
$this->assertCount(1, $ret);
$this->assertEquals('Je suis ici', $ret[0]);
$this->assertEquals('Je suis ici', $ret[0]->getValue());
}

public function testEndsAtViaWithIdAndFor() : void
Expand All @@ -77,21 +77,23 @@ public function testWithSingleComments() : void
$str = 'sweet (via sugar) bee';
$ret = $this->genericConsumer->__invoke($str);
$this->assertNotEmpty($ret);
$this->assertCount(2, $ret);
$this->assertEquals('sweet bee', $ret[0]);
$this->assertEquals('via sugar', $ret[1]->getComment());
$this->assertCount(1, $ret);
$this->assertEquals('sweet bee', $ret[0]->getValue());
$this->assertEquals('via sugar', $ret[0]->getComments()[0]->getComment());
}

public function testWithMultipleComments() : void
{
$str = 'sweet (as can) (surely) bee (innit)';
$ret = $this->genericConsumer->__invoke($str);
$this->assertNotEmpty($ret);
$this->assertCount(4, $ret);
$this->assertEquals('sweet bee', $ret[0]);
$this->assertEquals('as can', $ret[1]->getComment());
$this->assertEquals('surely', $ret[2]->getComment());
$this->assertEquals('innit', $ret[3]->getComment());
$this->assertCount(1, $ret);
$this->assertEquals('sweet bee', $ret[0]->getValue());
$comms = $ret[0]->getComments();
$this->assertCount(3, $comms);
$this->assertEquals('as can', $comms[0]->getComment());
$this->assertEquals('surely', $comms[1]->getComment());
$this->assertEquals('innit', $comms[2]->getComment());
}

public function testWithSeparatorInWords() : void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ public function testWithWith() : void

$ret = $this->receivedConsumer->__invoke($value);
$this->assertNotEmpty($ret);
$this->assertCount(2, $ret);
$this->assertCount(1, $ret);
$this->assertInstanceOf('\\' . \ZBateson\MailMimeParser\Header\Part\ReceivedPart::class, $ret[0]);
$this->assertInstanceOf('\\' . \ZBateson\MailMimeParser\Header\Part\CommentPart::class, $ret[1]);

$comments = $ret[0]->getComments();
$this->assertCount(1, $comments);
$this->assertEquals('with', $ret[0]->getName());
$this->assertEquals('ESMTP', $ret[0]->getValue());
$this->assertEquals('TLS1.2', $ret[1]->getComment());
$this->assertEquals('TLS1.2', $comments[0]->getComment());
}

public function testWithId() : void
Expand All @@ -148,12 +150,14 @@ public function testWithId() : void

$ret = $this->receivedConsumer->__invoke($value);
$this->assertNotEmpty($ret);
$this->assertCount(2, $ret);
$this->assertCount(1, $ret);
$this->assertInstanceOf('\\' . \ZBateson\MailMimeParser\Header\Part\ReceivedPart::class, $ret[0]);
$this->assertInstanceOf('\\' . \ZBateson\MailMimeParser\Header\Part\CommentPart::class, $ret[1]);

$comments = $ret[0]->getComments();
$this->assertCount(1, $comments);
$this->assertEquals('id', $ret[0]->getName());
$this->assertEquals('123', $ret[0]->getValue());
$this->assertEquals('blah', $ret[1]->getComment());
$this->assertEquals('blah', $comments[0]->getComment());
}

public function testWithVia() : void
Expand Down Expand Up @@ -204,7 +208,7 @@ public function testExampleFullLines() : void
$ret = $this->receivedConsumer->__invoke($value);
$this->assertNotEmpty($ret);

$this->assertCount(6, $ret);
$this->assertCount(5, $ret);

$this->assertEquals('from', $ret[0]->getName());
$this->assertEquals('LeComputer', $ret[0]->getEhloName());
Expand All @@ -217,12 +221,13 @@ public function testExampleFullLines() : void
$this->assertEquals('with', $ret[2]->getName());
$this->assertEquals('ESMTP', $ret[2]->getValue());

$this->assertEquals('TLS BLAH', $ret[3]->getComment());
$this->assertCount(1, $ret[2]->getComments());
$this->assertEquals('TLS BLAH', $ret[2]->getComments()[0]->getComment());

$this->assertEquals('id', $ret[4]->getName());
$this->assertEquals('123', $ret[4]->getValue());
$this->assertEquals('id', $ret[3]->getName());
$this->assertEquals('123', $ret[3]->getValue());

$dt = $ret[5]->getDateTime();
$dt = $ret[4]->getDateTime();
$this->assertEquals('2000-05-17T19:08:29-04:00', $dt->format(DateTime::RFC3339));
}

Expand All @@ -235,7 +240,7 @@ public function testExampleFullLine2() : void
$ret = $this->receivedConsumer->__invoke($value);
$this->assertNotEmpty($ret);

$this->assertCount(8, $ret);
$this->assertCount(6, $ret);

$this->assertEquals('from', $ret[0]->getName());
$this->assertEquals('xcv.gurbuzsrc.com', $ret[0]->getEhloName());
Expand All @@ -247,13 +252,13 @@ public function testExampleFullLine2() : void
$this->assertEquals('with', $ret[2]->getName());
$this->assertEquals('esmtp', $ret[2]->getValue());

$this->assertEquals('id', $ret[5]->getName());
$this->assertEquals('1kfCyx-0002Zp-BY', $ret[5]->getValue());
$this->assertEquals('id', $ret[3]->getName());
$this->assertEquals('1kfCyx-0002Zp-BY', $ret[3]->getValue());

$this->assertEquals('for', $ret[6]->getName());
$this->assertEquals('[email protected]', $ret[6]->getValue());
$this->assertEquals('for', $ret[4]->getName());
$this->assertEquals('[email protected]', $ret[4]->getValue());

$dt = $ret[7]->getDateTime();
$dt = $ret[5]->getDateTime();
$this->assertEquals('2020-11-18T03:14:03+01:00', $dt->format(DateTime::RFC3339));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testConsumeTokens() : void
$ret = $this->subjectConsumer->__invoke($value);
$this->assertNotEmpty($ret);
$this->assertCount(1, $ret);
$this->assertEquals("Je\ \t suis ici", $ret[0]);
$this->assertEquals("Je\ \t suis ici", $ret[0]->getValue());
}

public function testFilterSpacesBetweenMimeParts() : void
Expand All @@ -50,6 +50,6 @@ public function testFilterSpacesBetweenMimeParts() : void
$ret = $this->subjectConsumer->__invoke($value);
$this->assertNotEmpty($ret);
$this->assertCount(1, $ret);
$this->assertEquals('Jesuisici', $ret[0]);
$this->assertEquals('Jesuisici', $ret[0]->getValue());
}
}
7 changes: 6 additions & 1 deletion tests/MailMimeParser/Header/HeaderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ZBateson\MailMimeParser\Header\Consumer\ParameterValueConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\ParameterNameValueConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\QuotedStringConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\QuotedStringMimeLiteralPartConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\ReceivedConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\SubjectConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\Received\DomainConsumerService;
Expand Down Expand Up @@ -81,8 +82,12 @@ protected function setUp() : void
->setMethods()
->getMock();

$qsmlpcs = $this->getMockBuilder(QuotedStringMimeLiteralPartConsumerService::class)
->setConstructorArgs([$this->logger, $pf])
->setMethods()
->getMock();
$pvcs = $this->getMockBuilder(ParameterValueConsumerService::class)
->setConstructorArgs([$this->logger, $mpf, $ccs, $qscs])
->setConstructorArgs([$this->logger, $mpf, $ccs, $qsmlpcs])
->setMethods()
->getMock();
$pnvcs = $this->getMockBuilder(ParameterNameValueConsumerService::class)
Expand Down
9 changes: 7 additions & 2 deletions tests/MailMimeParser/Header/ParameterHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace ZBateson\MailMimeParser\Header;

use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use ZBateson\MailMimeParser\Header\Consumer\CommentConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\QuotedStringConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\QuotedStringMimeLiteralPartConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\ParameterValueConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\ParameterNameValueConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\ParameterConsumerService;
Expand Down Expand Up @@ -47,8 +47,13 @@ protected function setUp() : void
->setConstructorArgs([$this->logger, $mpf, $qscs])
->setMethods()
->getMock();

$qsmlpcs = $this->getMockBuilder(QuotedStringMimeLiteralPartConsumerService::class)
->setConstructorArgs([$this->logger, $pf])
->setMethods()
->getMock();
$pvcs = $this->getMockBuilder(ParameterValueConsumerService::class)
->setConstructorArgs([$this->logger, $mpf, $ccs, $qscs])
->setConstructorArgs([$this->logger, $mpf, $ccs, $qsmlpcs])
->setMethods()
->getMock();
$pnvcs = $this->getMockBuilder(ParameterNameValueConsumerService::class)
Expand Down
7 changes: 1 addition & 6 deletions tests/MailMimeParser/Header/Part/AddressGroupPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,18 @@
class AddressGroupPartTest extends TestCase
{
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 newAddressGroupPart($nameParts, $addressAndGroupParts)
{
return new AddressGroupPart(
$this->logger, $this->mb, $this->hpf, $nameParts, $addressAndGroupParts
$this->logger, $this->mb, $nameParts, $addressAndGroupParts
);
}

Expand Down
Loading

0 comments on commit a626bad

Please sign in to comment.