Skip to content

Commit 06a30a1

Browse files
coverage
1 parent c2688bd commit 06a30a1

File tree

6 files changed

+162
-5
lines changed

6 files changed

+162
-5
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ test-header-info:
7474
test-num-msg:
7575
@docker-compose run --rm phpunit tests --filter CompatibilityTest::testNumMsg
7676

77+
test-reopen:
78+
@docker-compose run --rm phpunit tests --filter CompatibilityTest::testReopen
79+
7780
test-ping:
7881
@docker-compose run --rm phpunit tests --filter CompatibilityTest::testPing
7982

src/Connection.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Connection
2626
protected $port;
2727
protected $sslMode;
2828
protected $currentMailbox;
29+
protected $connected;
2930

3031
/**
3132
*
@@ -84,7 +85,15 @@ public static function reopen($imap, $mailbox, $flags = 0, $retries = 0)
8485

8586
$success = $imap->connect();
8687

87-
return boolval($success);
88+
if (empty($success)) {
89+
trigger_error('imap2_reopen(): Couldn\'t re-open stream', E_USER_WARNING);
90+
91+
return false;
92+
}
93+
94+
$imap->selectMailbox();
95+
96+
return true;
8897
}
8998

9099
public static function ping($imap)
@@ -105,6 +114,7 @@ public static function ping($imap)
105114
*/
106115
protected function connect()
107116
{
117+
$this->connected = false;
108118
//$this->client->setDebug(true);
109119

110120
$success = $this->client->connect($this->host, $this->user, $this->password, [
@@ -127,9 +137,37 @@ protected function connect()
127137
}
128138
}
129139

140+
$this->rewriteMailbox();
141+
142+
$this->connected = true;
143+
130144
return $this;
131145
}
132146

147+
/**
148+
*
149+
*/
150+
protected function rewriteMailbox($forceMailbox = null)
151+
{
152+
$mailboxParts = Functions::parseMailboxString($this->mailbox);
153+
154+
// '{imap.gmail.com:993/imap/notls/ssl/user="[email protected]"}INBOX'
155+
$params = [];
156+
157+
$params[] = 'imap';
158+
if ($this->sslMode == 'ssl') {
159+
$params[] = 'notls';
160+
$params[] = 'ssl';
161+
}
162+
$params[] = 'user="'.$this->user.'"';
163+
164+
$mailboxName = $forceMailbox ? $forceMailbox : $mailboxParts['mailbox'];
165+
166+
$updatedMailbox = '{'.$mailboxParts['host'].':'.$mailboxParts['port'].'/'.implode('/', $params).'}'.$mailboxName;
167+
168+
$this->mailbox = $updatedMailbox;
169+
}
170+
133171
/**
134172
*
135173
*/
@@ -167,7 +205,11 @@ public function getHost()
167205
*/
168206
public function selectMailbox()
169207
{
170-
$this->client->select($this->currentMailbox);
208+
$success = $this->client->select($this->currentMailbox);
209+
210+
if (empty($success)) {
211+
$this->rewriteMailbox('<no_mailbox>');
212+
}
171213
}
172214

173215
/**
@@ -196,4 +238,14 @@ public static function close($imap, $flags = 0)
196238

197239
return true;
198240
}
241+
242+
public function isConnected()
243+
{
244+
return boolval($this->connected);
245+
}
246+
247+
public static function isValid($imap)
248+
{
249+
return is_a($imap, Connection::class) && $imap->isConnected();
250+
}
199251
}

src/Functions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,14 @@ public static function getListAttributesValue($attributes)
144144

145145
return $attributesValue;
146146
}
147+
148+
public static function isValidImap1Connection($imap)
149+
{
150+
return is_resource($imap) && get_resource_type($imap) == 'imap';
151+
}
152+
153+
public static function isValidImap2Connection($imap)
154+
{
155+
return Connection::isValid($imap);
156+
}
147157
}

src/Mailbox.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public static function check($imap)
2222
$status = $client->status($imap->getMailboxName(), ['MESSAGES', 'RECENT']);
2323

2424
return (object) [
25-
'Date' => date('Y-m-d H:i:s'),
25+
'Date' => date('D, j M Y G:i:s').' +0000 (UTC)',
2626
'Driver' => 'imap',
2727
'Mailbox' => $imap->getMailbox(),
28-
'Nmsgs' => $status['MESSAGES'],
29-
'Recent' => $status['RECENT'],
28+
'Nmsgs' => intval($status['MESSAGES']),
29+
'Recent' => intval($status['RECENT']),
3030
];
3131

3232
} elseif (IMAP2_RETROFIT_MODE && is_resource($imap) && get_resource_type($imap) == 'imap') {

tests/CompatibilityTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,58 @@ public function testNumMsg()
518518
imap2_close($imap2);
519519
}
520520

521+
public function testReopen()
522+
{
523+
$imap1 = imap_open($this->mailbox, $this->username, $this->password);
524+
$imap2 = imap2_open($this->mailbox, $this->username, $this->accessToken, OP_XOAUTH2);
525+
526+
$this->assertTrue(Functions::isValidImap1Connection($imap1));
527+
$this->assertTrue(Functions::isValidImap2Connection($imap2));
528+
529+
$randomMailboxName = 'Mailbox ' . Functions::unique();
530+
$this->assertTrue(imap2_createmailbox($imap2, $randomMailboxName));
531+
532+
$check1 = imap_check($imap1);
533+
$check2 = imap2_check($imap2);
534+
$check2->Date = $check1->Date;
535+
536+
$this->assertEquals($check1, $check2);
537+
538+
imap_reopen($imap1, $this->mailbox.$randomMailboxName);
539+
imap2_reopen($imap2, $this->mailbox.$randomMailboxName);
540+
541+
$check1 = imap_check($imap1);
542+
$check2 = imap2_check($imap2);
543+
$check2->Date = $check1->Date;
544+
545+
$this->assertEquals($check1, $check2);
546+
547+
$wrongMailbox = 'This is wrong mailbox for test';
548+
imap_reopen($imap1, $this->mailbox.$wrongMailbox);
549+
imap2_reopen($imap2, $this->mailbox.$wrongMailbox);
550+
551+
$check1 = imap_check($imap1);
552+
$check2 = imap2_check($imap2);
553+
554+
$this->assertEquals($check1, $check2);
555+
556+
/*
557+
$this->captureError();
558+
@imap_reopen($imap1, $this->altMailbox);
559+
$error1 = $this->retrieveError();
560+
*/
561+
562+
$this->captureError();
563+
@imap2_reopen($imap2, $this->altMailbox);
564+
$error2 = $this->retrieveError();
565+
566+
/*$this->assertEquals('imap_reopen(): Couldn\'t re-open stream', $error1);*/
567+
$this->assertEquals('imap2_reopen(): Couldn\'t re-open stream', $error2);
568+
569+
imap_close($imap1);
570+
imap2_close($imap2);
571+
}
572+
521573
public function testPing()
522574
{
523575
$wrongPassword = 'wrong-password';

tests/ImapTestCase.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,54 @@ class ImapTestCase extends TestCase
1010
protected $username;
1111
protected $password;
1212
protected $accessToken;
13+
14+
protected $altMailbox;
15+
protected $altUsername;
16+
protected $altPassword;
17+
protected $altAccessToken;
18+
1319
protected $message;
1420

21+
protected $errorMessage;
22+
protected $errorNumber;
23+
1524
public function setUp()
1625
{
1726
$this->mailbox = getenv('IMAP_MAILBOX');
1827
$this->username = getenv('IMAP_USERNAME');
1928
$this->password = getenv('IMAP_PASSWORD');
2029
$this->accessToken = getenv('IMAP_ACCESS_TOKEN');
30+
31+
$this->altMailbox = getenv('IMAP_ALT_MAILBOX');
32+
$this->altUsername = getenv('IMAP_ALT_USERNAME');
33+
$this->altPassword = getenv('IMAP_ALT_PASSWORD');
34+
$this->altAccessToken = getenv('IMAP_ALT_ACCESS_TOKEN');
35+
2136
$this->message = file_get_contents('tests/fixtures/message.eml');
2237
}
38+
39+
public function captureError()
40+
{
41+
$this->errorMessage = null;
42+
$this->errorNumber = null;
43+
$errorMessage =& $this->errorMessage;
44+
$errorNumber =& $this->errorNumber;
45+
\set_error_handler(static function ($nr, $message) use (&$errorMessage, &$errorNumber) {
46+
$errorMessage = $message;
47+
$errorNumber = $nr;
48+
});
49+
}
50+
51+
public function retrieveError(/*&$errorNumber = 0*/)
52+
{
53+
\restore_error_handler();
54+
55+
if (empty($this->errorMessage)) {
56+
return null;
57+
}
58+
59+
/*$errorNumber = $this->errorNumber;*/
60+
61+
return $this->errorMessage;
62+
}
2363
}

0 commit comments

Comments
 (0)