Skip to content

Commit

Permalink
implementing typed properties, adding getter method as workaround for p…
Browse files Browse the repository at this point in the history
  • Loading branch information
bapcltd-marv committed Jan 17, 2020
1 parent 989489e commit 64809bb
Showing 1 changed file with 71 additions and 44 deletions.
115 changes: 71 additions & 44 deletions tests/unit/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ final class MailboxTest extends TestCase
/**
* Holds a PhpImap\Mailbox instance.
*
* @var Mailbox
* @var Mailbox|null
*/
private $mailbox;
private ?Mailbox $mailbox = null;

/**
* Holds the imap path.
*
* @var string
*/
private $imapPath = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX';
private string $imapPath = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX';

/**
* Holds the imap username.
Expand All @@ -34,28 +34,28 @@ final class MailboxTest extends TestCase
*
* @psalm-var string
*/
private $login = 'php-imap@example.com';
private string $login = 'php-imap@example.com';

/**
* Holds the imap user password.
*
* @var string
*/
private $password = 'v3rY!53cEt&P4sSWöRd$';
private string $password = 'v3rY!53cEt&P4sSWöRd$';

/**
* Holds the relative name of the directory, where email attachments will be saved.
*
* @var string
*/
private $attachmentsDir = '.';
private string $attachmentsDir = '.';

/**
* Holds the server encoding setting.
*
* @var string
*/
private $serverEncoding = 'UTF-8';
private string $serverEncoding = 'UTF-8';

/**
* Run before each test is started.
Expand Down Expand Up @@ -100,9 +100,11 @@ public function testConstructorTrimsPossibleVariables(): void
*/
public function testSetAndGetServerEncoding(): void
{
$this->mailbox->setServerEncoding('UTF-8');
$mailbox = $this->getMailbox();

$this->assertEquals($this->mailbox->getServerEncoding(), 'UTF-8');
$mailbox->setServerEncoding('UTF-8');

$this->assertEquals($mailbox->getServerEncoding(), 'UTF-8');
}

/**
Expand Down Expand Up @@ -164,13 +166,15 @@ public function serverEncodingProvider()
*/
public function testServerEncodingOnlyUseSupportedSettings(bool $bool, string $encoding): void
{
$mailbox = $this->getMailbox();

if ($bool) {
$this->mailbox->setServerEncoding($encoding);
$this->assertEquals($encoding, $this->mailbox->getServerEncoding());
$mailbox->setServerEncoding($encoding);
$this->assertEquals($encoding, $mailbox->getServerEncoding());
} else {
$this->expectException(InvalidParameterException::class);
$this->mailbox->setServerEncoding($encoding);
$this->assertNotEquals($encoding, $this->mailbox->getServerEncoding());
$mailbox->setServerEncoding($encoding);
$this->assertNotEquals($encoding, $mailbox->getServerEncoding());
}
}

Expand All @@ -181,7 +185,7 @@ public function testServerEncodingOnlyUseSupportedSettings(bool $bool, string $e
*/
public function testImapSearchOptionHasADefault(): void
{
$this->assertEquals($this->mailbox->getImapSearchOption(), 1);
$this->assertEquals($this->getMailbox()->getImapSearchOption(), 1);
}

/**
Expand All @@ -193,30 +197,32 @@ public function testSetAndGetImapSearchOption(): void
{
define('ANYTHING', 0);

$this->mailbox->setImapSearchOption(SE_FREE);
$this->assertEquals($this->mailbox->getImapSearchOption(), 2);
$mailbox = $this->getMailbox();

$mailbox->setImapSearchOption(SE_FREE);
$this->assertEquals($mailbox->getImapSearchOption(), 2);

$this->expectException(InvalidParameterException::class);
$this->mailbox->setImapSearchOption(ANYTHING);
$mailbox->setImapSearchOption(ANYTHING);

$this->mailbox->setImapSearchOption(SE_UID);
$this->assertEquals($this->mailbox->getImapSearchOption(), 1);
$mailbox->setImapSearchOption(SE_UID);
$this->assertEquals($mailbox->getImapSearchOption(), 1);
}

/**
* Test, that the imap login can be retrieved.
*/
public function testGetLogin(): void
{
$this->assertEquals($this->mailbox->getLogin(), 'php-imap@example.com');
$this->assertEquals($this->getMailbox()->getLogin(), 'php-imap@example.com');
}

/**
* Test, that the path delimiter has a default value.
*/
public function testPathDelimiterHasADefault(): void
{
$this->assertNotEmpty($this->mailbox->getPathDelimiter());
$this->assertNotEmpty($this->getMailbox()->getPathDelimiter());
}

/**
Expand Down Expand Up @@ -297,11 +303,13 @@ public function testPathDelimiterIsBeingChecked(string $str): void
{
$supported_delimiters = ['.', '/'];

$mailbox = $this->getMailbox();

if (in_array($str, $supported_delimiters)) {
$this->assertTrue($this->mailbox->validatePathDelimiter($str));
$this->assertTrue($mailbox->validatePathDelimiter($str));
} else {
$this->expectException(InvalidParameterException::class);
$this->mailbox->setPathDelimiter($str);
$mailbox->setPathDelimiter($str);
}
}

Expand All @@ -310,19 +318,21 @@ public function testPathDelimiterIsBeingChecked(string $str): void
*/
public function testSetAndGetPathDelimiter(): void
{
$this->mailbox->setPathDelimiter('.');
$this->assertEquals($this->mailbox->getPathDelimiter(), '.');
$mailbox = $this->getMailbox();

$this->mailbox->setPathDelimiter('/');
$this->assertEquals($this->mailbox->getPathDelimiter(), '/');
$mailbox->setPathDelimiter('.');
$this->assertEquals($mailbox->getPathDelimiter(), '.');

$mailbox->setPathDelimiter('/');
$this->assertEquals($mailbox->getPathDelimiter(), '/');
}

/**
* Test, that the attachments are not ignored by default.
*/
public function testGetAttachmentsAreNotIgnoredByDefault(): void
{
$this->assertEquals($this->mailbox->getAttachmentsIgnore(), false);
$this->assertEquals($this->getMailbox()->getAttachmentsIgnore(), false);
}

/**
Expand All @@ -346,8 +356,9 @@ public function attachmentsIgnoreProvider(): array
*/
public function testSetAttachmentsIgnore(bool $paramValue): void
{
$this->mailbox->setAttachmentsIgnore($paramValue);
$this->assertEquals($this->mailbox->getAttachmentsIgnore(), $paramValue);
$mailbox = $this->getMailbox();
$mailbox->setAttachmentsIgnore($paramValue);
$this->assertEquals($mailbox->getAttachmentsIgnore(), $paramValue);
}

/**
Expand Down Expand Up @@ -397,8 +408,10 @@ public function encodingTestStringsProvider(): array
*/
public function testEncodingToUtf7DecodeBackToUtf8(string $str): void
{
$utf7_encoded_str = $this->mailbox->encodeStringToUtf7Imap($str);
$utf8_decoded_str = $this->mailbox->decodeStringFromUtf7ImapToUtf8($utf7_encoded_str);
$mailbox = $this->getMailbox();

$utf7_encoded_str = $mailbox->encodeStringToUtf7Imap($str);
$utf8_decoded_str = $mailbox->decodeStringFromUtf7ImapToUtf8($utf7_encoded_str);

$this->assertEquals($utf8_decoded_str, $str);
}
Expand All @@ -410,7 +423,7 @@ public function testEncodingToUtf7DecodeBackToUtf8(string $str): void
*/
public function testMimeDecodingReturnsCorrectValues(string $str): void
{
$this->assertEquals($this->mailbox->decodeMimeStr($str, 'utf-8'), $str);
$this->assertEquals($this->getMailbox()->decodeMimeStr($str, 'utf-8'), $str);
}

/**
Expand Down Expand Up @@ -445,7 +458,7 @@ public function datetimeProvider(): array
*/
public function testParsedDateDifferentTimeZones(string $dateToParse, int $epochToCompare): void
{
$parsedDt = $this->mailbox->parseDateTime($dateToParse);
$parsedDt = $this->getMailbox()->parseDateTime($dateToParse);
$parsedDateTime = new DateTime($parsedDt);
$this->assertEquals($parsedDateTime->getTimestamp(), $epochToCompare);
}
Expand All @@ -471,7 +484,7 @@ public function invalidDatetimeProvider(): array
*/
public function testParsedDateWithUnparseableDateTime(string $dateToParse): void
{
$parsedDt = $this->mailbox->parseDateTime($dateToParse);
$parsedDt = $this->getMailbox()->parseDateTime($dateToParse);
$this->assertEquals($parsedDt, $dateToParse);
}

Expand All @@ -481,7 +494,7 @@ public function testParsedDateWithUnparseableDateTime(string $dateToParse): void
public function testParsedDateTimeWithEmptyHeaderDate(): void
{
$this->expectException(InvalidParameterException::class);
$this->mailbox->parseDateTime('');
$this->getMailbox()->parseDateTime('');
}

/**
Expand Down Expand Up @@ -512,11 +525,13 @@ public function mimeEncodingProvider(): array
*/
public function testMimeEncoding(string $str, string $expected): void
{
$mailbox = $this->getMailbox();

if (empty($expected)) {
$this->expectException(Exception::class);
$this->mailbox->decodeMimeStr($str);
$mailbox->decodeMimeStr($str);
} else {
$this->assertEquals($this->mailbox->decodeMimeStr($str), $expected);
$this->assertEquals($mailbox->decodeMimeStr($str), $expected);
}
}

Expand Down Expand Up @@ -554,11 +569,13 @@ public function timeoutsProvider(): array
*/
public function testSetTimeouts(string $assertMethod, int $timeout, array $types): void
{
$mailbox = $this->getMailbox();

if ('expectException' == $assertMethod) {
$this->expectException(InvalidParameterException::class);
$this->mailbox->setTimeouts($timeout, $types);
$mailbox->setTimeouts($timeout, $types);
} else {
$this->assertNull($this->mailbox->setTimeouts($timeout, $types));
$this->assertNull($mailbox->setTimeouts($timeout, $types));
}
}

Expand Down Expand Up @@ -601,11 +618,13 @@ public function connectionArgsProvider(): array
*/
public function testSetConnectionArgs(string $assertMethod, int $option, int $retriesNum, array $param = null): void
{
$mailbox = $this->getMailbox();

if ('expectException' == $assertMethod) {
$this->expectException(InvalidParameterException::class);
$this->mailbox->setConnectionArgs($option, $retriesNum, $param);
$mailbox->setConnectionArgs($option, $retriesNum, $param);
} elseif ('assertNull' == $assertMethod) {
$this->assertNull($this->mailbox->setConnectionArgs($option, $retriesNum, $param));
$this->assertNull($mailbox->setConnectionArgs($option, $retriesNum, $param));
}
}

Expand Down Expand Up @@ -637,7 +656,15 @@ public function mimeStrDecodingProvider(): array
*/
public function testDecodeMimeStr(string $str, string $expectedStr, string $serverEncoding = 'utf-8'): void
{
$this->mailbox->setServerEncoding($serverEncoding);
$this->assertEquals($this->mailbox->decodeMimeStr($str, $this->mailbox->getServerEncoding()), $expectedStr);
$mailbox = $this->getMailbox();

$mailbox->setServerEncoding($serverEncoding);
$this->assertEquals($mailbox->decodeMimeStr($str, $mailbox->getServerEncoding()), $expectedStr);
}

protected function getMailbox(): Mailbox
{
/** @var Mailbox */
return $this->mailbox;
}
}

0 comments on commit 64809bb

Please sign in to comment.