Skip to content

Commit

Permalink
Merge pull request #82 from fredden/test-coverage
Browse files Browse the repository at this point in the history
Storage\Maildir fixes + enable unit tests
  • Loading branch information
michalbundyra committed Apr 1, 2020
2 parents 40c3ea7 + 6c6867c commit 40d0612
Show file tree
Hide file tree
Showing 33 changed files with 913 additions and 162 deletions.
9 changes: 1 addition & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,7 @@
<env name="TESTS_LAMINAS_MAIL_IMAP_WRONG_PORT" value="80" />
<env name="TESTS_LAMINAS_MAIL_IMAP_INVALID_PORT" value="3141" />

<!-- Laminas\Mail\Storage\Maildir test
Before enabling this test you have to unpack messages.tar in
test/_files/test.maildir/cur/ and remove the tar for this test to
work. That's because the messages files have a colon in the
filename and that's a forbidden character on Windows. -->
<env name="TESTS_LAMINAS_MAIL_MAILDIR_ENABLED" value="false" />

<!-- Laminas\Mail\Storage\Maildir test -->
<env name="TESTS_LAMINAS_MAIL_SMTP_ENABLED" value="false" />
<env name="TESTS_LAMINAS_MAIL_SMTP_HOST" value="localhost" />
<env name="TESTS_LAMINAS_MAIL_SMTP_PORT" value="25" />
Expand Down
2 changes: 1 addition & 1 deletion src/Header/IdentificationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getEncoding()
*/
public function toString()
{
return sprintf('%s: %s', $this->fieldName, $this->getFieldValue());
return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Storage/Folder/Maildir.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function buildFolderTree()
continue;
}

if ($this->_isMaildir($this->rootdir . $entry)) {
if ($this->isMaildir($this->rootdir . $entry)) {
$dirs[] = $entry;
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public function selectFolder($globalName)
$folder = $this->getFolders($this->currentFolder);

try {
$this->_openMaildir($this->rootdir . '.' . $folder->getGlobalName());
$this->openMaildir($this->rootdir . '.' . $folder->getGlobalName());
} catch (Exception\ExceptionInterface $e) {
// check what went wrong
if (! $folder->isSelectable()) {
Expand Down
5 changes: 4 additions & 1 deletion src/Storage/Maildir.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getSize($id = null)
public function getMessage($id)
{
// TODO that's ugly, would be better to let the message class decide
if ($this->messageClass === Message\File::class
if (\trim($this->messageClass, '\\') === Message\File::class
|| is_subclass_of($this->messageClass, Message\File::class)
) {
return new $this->messageClass([
Expand Down Expand Up @@ -336,6 +336,9 @@ protected function getMaildirFiles($dh, $dirname, $defaultFlags = [])
}
$this->files[] = $data;
}
\usort($this->files, function ($a, $b) {
return \strcmp($a['filename'], $b['filename']);
});
}

/**
Expand Down
55 changes: 55 additions & 0 deletions test/AddressListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public function testAddingEmailsIncreasesCount()
$this->assertEquals(1, count($this->list));
}

public function testAddingEmailFromStringIncreasesCount()
{
$this->list->addFromString('test@example.com');
$this->assertEquals(1, count($this->list));
}

public function testImplementsTraversable()
{
$this->assertInstanceOf('Traversable', $this->list);
Expand All @@ -65,6 +71,20 @@ public function testGetReturnsFalseWhenEmailNotFound()
$this->assertFalse($this->list->get('foo@example.com'));
}

public function testThrowExceptionOnInvalidInputAdd()
{
$this->expectException('Laminas\Mail\Exception\InvalidArgumentException');
$this->expectExceptionMessage('add expects an email address or Laminas\Mail\Address object');
$this->list->add(null);
}

public function testThrowExceptionOnInvalidInputAddMany()
{
$this->expectException('Laminas\Mail\Exception\InvalidArgumentException');
$this->expectExceptionMessage('add expects an email address or Laminas\Mail\Address object');
$this->list->addMany([null]);
}

public function testGetReturnsAddressObjectWhenEmailFound()
{
$this->list->add('test@example.com');
Expand Down Expand Up @@ -145,4 +165,39 @@ public function testSemicolonSeparator()
$this->assertTrue($addressList->has('asda.fasd@example.net'));
$this->assertTrue($addressList->has('root@example.org'));
}

public function testMergeTwoLists()
{
$otherList = new AddressList();
$this->list->add('one@example.net');
$otherList->add('two@example.org');
$this->list->merge($otherList);
$this->assertEquals(2, count($this->list));
}

public function testDeleteSuccess()
{
$this->list->add('test@example.com');
$this->assertTrue($this->list->delete('test@example.com'));
$this->assertEquals(0, count($this->list));
}

public function testDeleteNotExist()
{
$this->assertFalse($this->list->delete('test@example.com'));
}

public function testKey()
{
$this->assertNull($this->list->key());
$this->list->add('test@example.com');
$this->list->add('test@example.net');
$this->list->add('test@example.org');
$this->list->rewind();
$this->assertSame('test@example.com', $this->list->key());
$this->list->next();
$this->assertSame('test@example.net', $this->list->key());
$this->list->next();
$this->assertSame('test@example.org', $this->list->key());
}
}
26 changes: 26 additions & 0 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* @see https://github.com/laminas/laminas-mail for the canonical source repository
* @copyright https://github.com/laminas/laminas-mail/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-mail/blob/master/LICENSE.md New BSD License
*/

namespace LaminasTest\Mail;

use Laminas\Mail\ConfigProvider;
use PHPUnit\Framework\TestCase;

/**
* @group Laminas_Mail
* @covers \Laminas\Mail\ConfigProvider<extended>
*/
class ConfigProviderTest extends TestCase
{
public function testInvoke()
{
$configProvider = new ConfigProvider();
$config = $configProvider();
$this->assertEquals(['dependencies'], \array_keys($config));
}
}
21 changes: 21 additions & 0 deletions test/Header/ContentTransferEncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function testContentTransferEncodingGetFieldValueReturnsProperValue($enco
$contentTransferEncodingHeader = new ContentTransferEncoding();
$contentTransferEncodingHeader->setTransferEncoding($encoding);
$this->assertEquals($encoding, $contentTransferEncodingHeader->getFieldValue());
$this->assertEquals($encoding, $contentTransferEncodingHeader->getTransferEncoding());
}

/**
Expand Down Expand Up @@ -147,4 +148,24 @@ public function testSetTransferEncodingRaisesExceptionForInvalidValues()
$this->expectExceptionMessage('expects');
$header->setTransferEncoding("8bit\r\n 7bit");
}

public function testFromStringRaisesExceptionOnInvalidHeader()
{
$this->expectException('Laminas\Mail\Header\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Invalid header line for Content-Transfer-Encoding string');
ContentTransferEncoding::fromString('Foo: bar');
}

public function testDefaultEncoding()
{
$header = new ContentTransferEncoding('today');
$this->assertSame('ASCII', $header->getEncoding());
}

public function testChangeEncodingHasNoEffect()
{
$header = new ContentTransferEncoding('today');
$header->setEncoding('UTF-8');
$this->assertSame('ASCII', $header->getEncoding());
}
}
52 changes: 52 additions & 0 deletions test/Header/ContentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,56 @@ public function invalidHeaderLinesProvider()
];
// @codingStandardsIgnoreEnd
}

public function testFromStringRaisesExceptionOnInvalidHeader()
{
$this->expectException('Laminas\Mail\Header\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Invalid header line for Content-Type string');
ContentType::fromString('Foo: bar');
}

public function testDefaultEncoding()
{
$header = new ContentType('today');
$this->assertSame('ASCII', $header->getEncoding());
}

public function testSetEncoding()
{
$header = new ContentType('today');
$header->setEncoding('UTF-8');
$this->assertSame('UTF-8', $header->getEncoding());
}

public function testSetTypeThrowsOnInvalidValue()
{
$header = new ContentType();
$this->expectException('Laminas\Mail\Header\Exception\InvalidArgumentException');
$this->expectExceptionMessage('setType expects a value in the format "type/subtype"');
$header->setType('invalid');
}

public function testGetParameter()
{
$header = ContentType::fromString('content-type: text/plain; level=top');
$this->assertSame('top', $header->getParameter('level'));
}

public function testGetParameterNotExists()
{
$header = ContentType::fromString('content-type: text/plain');
$this->assertNull($header->getParameter('level'));
}

public function testRemoveParameter()
{
$header = ContentType::fromString('content-type: text/plain; level=top');
$this->assertTrue($header->removeParameter('level'));
}

public function testRemoveParameterNotExists()
{
$header = ContentType::fromString('content-type: text/plain');
$this->assertFalse($header->removeParameter('level'));
}
}
26 changes: 26 additions & 0 deletions test/Header/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,30 @@ public function testPreventsCRLFInjectionViaConstructor()
$this->expectException('Laminas\Mail\Header\Exception\InvalidArgumentException');
$address = new Header\Date("This\ris\r\na\nCRLF Attack");
}

public function testFromStringRaisesExceptionOnInvalidHeader()
{
$this->expectException('Laminas\Mail\Header\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Invalid header line for Date string');
Header\Date::fromString('Foo: bar');
}

public function testDefaultEncoding()
{
$header = new Header\Date('today');
$this->assertSame('ASCII', $header->getEncoding());
}

public function testSetEncodingHasNoEffect()
{
$header = new Header\Date('today');
$header->setEncoding('UTF-8');
$this->assertSame('ASCII', $header->getEncoding());
}

public function testToString()
{
$header = new Header\Date('today');
$this->assertEquals('Date: today', $header->toString());
}
}
49 changes: 45 additions & 4 deletions test/Header/GenericHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,41 @@
*/
class GenericHeaderTest extends TestCase
{
public function invalidHeaderLines()
{
return [
'append-chr-32' => [
'Content-Type' . chr(32) . ': text/html',
'Invalid header name detected',
],
'newline-non-continuation' => [
'Content-Type: text/html; charset = "iso-8859-1"' . "\nThis is a test",
'Invalid header value detected',
],
'missing-colon' => [
'content-type text/html',
'Header must match with the format "name:value"',
],
];
}

/**
* @dataProvider invalidHeaderLines
* @group ZF2015-04
*/
public function testSplitHeaderLineRaisesExceptionOnInvalidHeader()
public function testSplitHeaderLineRaisesExceptionOnInvalidHeader($line, $message)
{
$this->expectException('Laminas\Mail\Header\Exception\InvalidArgumentException');
GenericHeader::splitHeaderLine(
'Content-Type' . chr(32) . ': text/html; charset = "iso-8859-1"' . "\nThis is a test"
);
$this->expectExceptionMessage($message);
GenericHeader::splitHeaderLine($line);
}

public function fieldNames()
{
return [
'append-chr-13' => ["Subject" . chr(13)],
'append-chr-127' => ["Subject" . chr(127)],
'non-string' => [null],
];
}

Expand Down Expand Up @@ -148,4 +167,26 @@ public function testAllowZeroInHeaderValueInConstructor()
$this->assertEquals(0, $header->getFieldValue());
$this->assertEquals('Foo: 0', $header->toString());
}

public function testDefaultEncoding()
{
$header = new GenericHeader('Foo');
$this->assertSame('ASCII', $header->getEncoding());
}

public function testSetEncoding()
{
$header = new GenericHeader('Foo');
$header->setEncoding('UTF-8');
$this->assertSame('UTF-8', $header->getEncoding());
}

public function testToStringThrowsWithoutFieldName()
{
$header = new GenericHeader;

$this->expectException('Laminas\Mail\Header\Exception\RuntimeException');
$this->expectExceptionMessage('Header name is not set, use setFieldName()');
$header->toString();
}
}
Loading

0 comments on commit 40d0612

Please sign in to comment.