Skip to content

Commit

Permalink
+add way to change EOL for Mbox files
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-n-ko committed Oct 17, 2014
1 parent 78f46e2 commit c0b9465
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
20 changes: 18 additions & 2 deletions library/Zend/Mail/Storage/Mbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class Mbox extends AbstractStorage
* @var string
*/
protected $messageClass = '\Zend\Mail\Storage\Message\File';

protected $messageEOL;

/**
* Count messages all messages in current box
Expand Down Expand Up @@ -108,8 +110,18 @@ public function getMessage($id)
|| is_subclass_of($this->messageClass, '\Zend\Mail\Storage\Message\File')) {
// TODO top/body lines
$messagePos = $this->getPos($id);
return new $this->messageClass(array('file' => $this->fh, 'startPos' => $messagePos['start'],
'endPos' => $messagePos['end']));

$messageClassParams = array(
'file' => $this->fh,
'startPos' => $messagePos['start'],
'endPos' => $messagePos['end']
);

if ($this->messageEOL) {
$messageClassParams['EOL'] = $this->messageEOL;
}

return new $this->messageClass($messageClassParams);
}

$bodyLines = 0; // TODO: need a way to change that
Expand Down Expand Up @@ -183,6 +195,10 @@ public function __construct($params)
if (!isset($params->filename)) {
throw new Exception\InvalidArgumentException('no valid filename given in params');
}

if (isset($params->messageEOL)) {
$this->messageEOL = $params->messageEOL;
}

$this->openMboxFile($params->filename);
$this->has['top'] = true;
Expand Down
9 changes: 7 additions & 2 deletions library/Zend/Mail/Storage/Part/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class File extends Part
* - file filename or open file handler with message content (required)
* - startPos start position of message or part in file (default: current position)
* - endPos end position of message or part in file (default: end of file)
*
* - EOL end of Line for fields
*
* @param array $params full message with or without headers
* @throws Exception\RuntimeException
* @throws Exception\InvalidArgumentException
Expand Down Expand Up @@ -53,7 +54,11 @@ public function __construct(array $params)
$header .= $line;
}

$this->headers = Headers::fromString($header);
if (isset($params['EOL'])){
$this->headers = Headers::fromString($header, $params['EOL']);
}else{
$this->headers = Headers::fromString($header);
}

$this->contentPos[0] = ftell($this->fh);
if ($endPos !== null) {
Expand Down
32 changes: 30 additions & 2 deletions tests/ZendTest/Mail/Storage/MboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MboxTest extends \PHPUnit_Framework_TestCase
{
protected $_mboxOriginalFile;
protected $_mboxFile;
protected $_mboxFileUnix;
protected $_tmpdir;

public function setUp()
Expand All @@ -43,16 +44,26 @@ public function setUp()
return;
}
}

$this->_mboxOriginalFile = __DIR__ . '/../_files/test.mbox/INBOX';
$this->_mboxOriginalFile = __DIR__ . '/../_files/test.mbox/INBOX';
$this->_mboxFile = $this->_tmpdir . 'INBOX';

copy($this->_mboxOriginalFile, $this->_mboxFile);

if (strpos($this->getName(), 'Unix')){
$this->_mboxOriginalFileLinux = __DIR__ . '/../_files/test.mbox/INBOX.unix';
$this->_mboxFileUnix = $this->_tmpdir . 'INBOX.unix';
copy($this->_mboxOriginalFileLinux, $this->_mboxFileUnix);
}
}

public function tearDown()
{
unlink($this->_mboxFile);

if ($this->_mboxFileUnix){
unlink($this->_mboxFileUnix);
}
}

public function testLoadOk()
Expand Down Expand Up @@ -155,6 +166,14 @@ public function testFetchTopBody()
}
*/

public function testFetchMessageHeaderLinuxUnix()
{
$mail = new Storage\Mbox(array('filename' => $this->_mboxFileUnix, 'messageEOL' => "\n"));

$subject = $mail->getMessage(1)->subject;
$this->assertEquals('Simple Message', $subject);
}

public function testFetchMessageHeader()
{
$mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
Expand All @@ -171,6 +190,15 @@ public function testFetchMessageBody()
list($content, ) = explode("\n", $content, 2);
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
}

public function testFetchMessageBodyUnix()
{
$mail = new Storage\Mbox(array('filename' => $this->_mboxFileUnix, 'messageEOL' => "\n"));

$content = $mail->getMessage(3)->getContent();
list($content, ) = explode("\n", $content, 2);
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
}

public function testFailedRemove()
{
Expand Down

0 comments on commit c0b9465

Please sign in to comment.