-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate 'email' into its own consumer #165
- Loading branch information
Showing
6 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* This file is part of the ZBateson\MailMimeParser project. | ||
* | ||
* @license http://opensource.org/licenses/bsd-license.php BSD | ||
*/ | ||
namespace ZBateson\MailMimeParser\Header\Consumer; | ||
|
||
/** | ||
* Parses the Address portion of an email address header, for an address part | ||
* that contains both a name and an email address, e.g. "name" <email@tld.com>. | ||
* | ||
* The address portion found within the '<' and '>' chars may contain comments | ||
* and quoted portions. | ||
* | ||
* @author Zaahid Bateson | ||
*/ | ||
class AddressEmailConsumer extends AbstractConsumer | ||
{ | ||
/** | ||
* Returns the following as sub-consumers: | ||
* - {@see AddressGroupConsumer} | ||
* - {@see CommentConsumer} | ||
* - {@see QuotedStringConsumer} | ||
* | ||
* @return AbstractConsumer[] the sub-consumers | ||
*/ | ||
protected function getSubConsumers() | ||
{ | ||
return [ | ||
$this->consumerService->getCommentConsumer(), | ||
$this->consumerService->getQuotedStringConsumer(), | ||
]; | ||
} | ||
|
||
/** | ||
* Overridden to return patterns matching the beginning/end part of an | ||
* address in a name/address part ("<" and ">" chars). | ||
* | ||
* @return string[] the patterns | ||
*/ | ||
public function getTokenSeparators() | ||
{ | ||
return [ '<', '>' ]; | ||
} | ||
|
||
/** | ||
* Returns true for the '>' char. | ||
* | ||
* @param string $token | ||
* @return boolean false | ||
*/ | ||
protected function isEndToken($token) | ||
{ | ||
return ($token === '>'); | ||
} | ||
|
||
/** | ||
* Returns true for the '<' char. | ||
* | ||
* @param string $token | ||
* @return boolean false | ||
*/ | ||
protected function isStartToken($token) | ||
{ | ||
return ($token === '<'); | ||
} | ||
|
||
/** | ||
* Returns a single AddressPart with its 'email' portion set, so an | ||
* AddressConsumer can identify it and create an AddressPart with both a | ||
* name and email set. | ||
* | ||
* @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts | ||
* @return \ZBateson\MailMimeParser\Header\IHeaderPart[]|array | ||
*/ | ||
protected function processParts(array $parts) | ||
{ | ||
$strEmail = ''; | ||
foreach ($parts as $p) { | ||
$strEmail .= $p->getValue(); | ||
} | ||
return [ $this->partFactory->newAddressPart('', $strEmail) ]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
tests/MailMimeParser/Header/Consumer/AddressEmailConsumerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
namespace ZBateson\MailMimeParser\Header\Consumer; | ||
|
||
use LegacyPHPUnit\TestCase; | ||
|
||
/** | ||
* Description of AddressEmailConsumerTest | ||
* | ||
* @group Consumers | ||
* @group AddressEmailConsumer | ||
* @covers ZBateson\MailMimeParser\Header\Consumer\AddressEmailConsumer | ||
* @covers ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer | ||
* @author Zaahid Bateson | ||
*/ | ||
class AddressEmailConsumerTest extends TestCase | ||
{ | ||
private $addressConsumer; | ||
|
||
protected function legacySetUp() | ||
{ | ||
$charsetConverter = $this->getMockBuilder('ZBateson\MbWrapper\MbWrapper') | ||
->setMethods(['__toString']) | ||
->getMock(); | ||
$pf = $this->getMockBuilder('ZBateson\MailMimeParser\Header\Part\HeaderPartFactory') | ||
->setConstructorArgs([$charsetConverter]) | ||
->setMethods(['__toString']) | ||
->getMock(); | ||
$mlpf = $this->getMockBuilder('ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory') | ||
->setConstructorArgs([$charsetConverter]) | ||
->setMethods(['__toString']) | ||
->getMock(); | ||
$cs = $this->getMockBuilder('ZBateson\MailMimeParser\Header\Consumer\ConsumerService') | ||
->setConstructorArgs([$pf, $mlpf]) | ||
->setMethods(['__toString']) | ||
->getMock(); | ||
$this->addressConsumer = new AddressEmailConsumer($cs, $pf); | ||
} | ||
|
||
public function testConsumeEmail() | ||
{ | ||
$email = 'Max.Payne@AddressUnknown.com'; | ||
$ret = $this->addressConsumer->__invoke($email); | ||
$this->assertNotEmpty($ret); | ||
$this->assertCount(1, $ret); | ||
|
||
$address = $ret[0]; | ||
$this->assertInstanceOf('\ZBateson\MailMimeParser\Header\Part\AddressPart', $address); | ||
$this->assertEquals('', $address->getName()); | ||
$this->assertEquals($email, $address->getEmail()); | ||
} | ||
|
||
public function testConsumeEmailWithComments() | ||
{ | ||
// can't remember any longer if this is how it should be handled | ||
// need to review RFC | ||
$email = 'Max(imum).Payne (comment)@AddressUnknown.com'; | ||
$ret = $this->addressConsumer->__invoke($email); | ||
$this->assertNotEmpty($ret); | ||
$this->assertCount(1, $ret); | ||
|
||
$address = $ret[0]; | ||
$this->assertInstanceOf('\ZBateson\MailMimeParser\Header\Part\AddressPart', $address); | ||
$this->assertEquals('Max.Payne@AddressUnknown.com', $address->getEmail()); | ||
} | ||
|
||
public function testConsumeEmailWithQuotes() | ||
{ | ||
// can't remember any longer if this is how it should be handled | ||
// need to review RFC | ||
$email = 'Max"(imum).Payne (comment)"@AddressUnknown.com'; | ||
$ret = $this->addressConsumer->__invoke($email); | ||
$this->assertNotEmpty($ret); | ||
$this->assertCount(1, $ret); | ||
|
||
$address = $ret[0]; | ||
$this->assertInstanceOf('\ZBateson\MailMimeParser\Header\Part\AddressPart', $address); | ||
$this->assertEquals('Max(imum).Payne(comment)@AddressUnknown.com', $address->getEmail()); | ||
} | ||
|
||
public function testNotConsumeAddressGroup() | ||
{ | ||
$email = 'Senate: Caesar@Dictator.com,Cicero@Philosophy.com, Marc Antony <MarcAntony@imawesome.it>'; | ||
$ret = $this->addressConsumer->__invoke($email); | ||
$this->assertNotEmpty($ret); | ||
$this->assertCount(1, $ret); | ||
|
||
$address = $ret[0]; | ||
$this->assertInstanceOf('\ZBateson\MailMimeParser\Header\Part\AddressPart', $address); | ||
$this->assertEquals('Senate:Caesar@Dictator.com,Cicero@Philosophy.com,MarcAntony<MarcAntony@imawesome.it', $address->getEmail()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters