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 27, 2020
1 parent 33d79fd commit a15e7ed
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 @@ -15,16 +15,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 @@ -33,28 +33,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 @@ -99,9 +99,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 @@ -163,13 +165,15 @@ public function serverEncodingProvider(): array
*/
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 @@ -180,7 +184,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 @@ -192,30 +196,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 @@ -296,11 +302,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 @@ -309,19 +317,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 @@ -345,8 +355,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 @@ -396,8 +407,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 @@ -409,7 +422,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 @@ -444,7 +457,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 @@ -470,7 +483,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 @@ -480,7 +493,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 @@ -511,11 +524,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 @@ -553,11 +568,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 @@ -600,11 +617,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 @@ -636,8 +655,10 @@ 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);
}

/**
Expand Down Expand Up @@ -685,4 +706,10 @@ public function testAttachmentDirFailure(string $initialDir, string $attachments

$mailbox->setAttachmentsDir($attachmentsDir);
}

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

0 comments on commit a15e7ed

Please sign in to comment.