Skip to content

Commit a1e8197

Browse files
authored
Merge pull request #6184 from kenjis/fix-email-Uncaught-ErrorException
fix: Email SMTP may throw Uncaught ErrorException
2 parents 397c606 + 8fc648d commit a1e8197

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

rector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
4242
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
4343
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
44+
use Rector\PHPUnit\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
4445
use Rector\PHPUnit\Set\PHPUnitSetList;
4546
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
4647
use Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector;
@@ -121,6 +122,11 @@
121122

122123
// use mt_rand instead of random_int on purpose on non-cryptographically random
123124
RandomFunctionRector::class,
125+
126+
// @TODO remove if https://github.com/rectorphp/rector-phpunit/issues/86 is fixed
127+
GetMockBuilderGetMockToCreateMockRector::class => [
128+
__DIR__ . '/tests/system/Email/EmailTest.php',
129+
],
124130
]);
125131

126132
// auto import fully qualified class names

system/Email/Email.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,13 @@ protected function mimeTypes($ext = '')
21442144
public function __destruct()
21452145
{
21462146
if (is_resource($this->SMTPConnect)) {
2147-
$this->sendCommand('quit');
2147+
try {
2148+
$this->sendCommand('quit');
2149+
} catch (ErrorException $e) {
2150+
$protocol = $this->getProtocol();
2151+
$method = 'sendWith' . ucfirst($protocol);
2152+
log_message('error', 'Email: ' . $method . ' throwed ' . $e);
2153+
}
21482154
}
21492155
}
21502156

tests/system/Email/EmailTest.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Events\Events;
1515
use CodeIgniter\Test\CIUnitTestCase;
1616
use CodeIgniter\Test\Mock\MockEmail;
17+
use ErrorException;
1718

1819
/**
1920
* @internal
@@ -44,9 +45,8 @@ public function autoClearProvider()
4445
*/
4546
public function testEmailSendWithClearance($autoClear)
4647
{
47-
$config = config('Email');
48-
$config->validate = true;
49-
$email = new MockEmail($config);
48+
$email = $this->createMockEmail();
49+
5050
$email->setTo('[email protected]');
5151

5252
$this->assertTrue($email->send($autoClear));
@@ -58,9 +58,8 @@ public function testEmailSendWithClearance($autoClear)
5858

5959
public function testEmailSendStoresArchive()
6060
{
61-
$config = config('Email');
62-
$config->validate = true;
63-
$email = new MockEmail($config);
61+
$email = $this->createMockEmail();
62+
6463
$email->setTo('[email protected]');
6564
$email->setFrom('[email protected]');
6665
$email->setSubject('Archive Test');
@@ -75,9 +74,8 @@ public function testEmailSendStoresArchive()
7574

7675
public function testAutoClearLeavesArchive()
7776
{
78-
$config = config('Email');
79-
$config->validate = true;
80-
$email = new MockEmail($config);
77+
$email = $this->createMockEmail();
78+
8179
$email->setTo('[email protected]');
8280

8381
$this->assertTrue($email->send(true));
@@ -89,6 +87,7 @@ public function testEmailSendRepeatUpdatesArchive()
8987
{
9088
$config = config('Email');
9189
$email = new MockEmail($config);
90+
9291
$email->setTo('[email protected]');
9392
$email->setFrom('[email protected]');
9493

@@ -104,9 +103,8 @@ public function testEmailSendRepeatUpdatesArchive()
104103

105104
public function testSuccessDoesTriggerEvent()
106105
{
107-
$config = config('Email');
108-
$config->validate = true;
109-
$email = new MockEmail($config);
106+
$email = $this->createMockEmail();
107+
110108
$email->setTo('[email protected]');
111109

112110
$result = null;
@@ -123,9 +121,8 @@ public function testSuccessDoesTriggerEvent()
123121

124122
public function testFailureDoesNotTriggerEvent()
125123
{
126-
$config = config('Email');
127-
$config->validate = true;
128-
$email = new MockEmail($config);
124+
$email = $this->createMockEmail();
125+
129126
$email->setTo('[email protected]');
130127
$email->returnValue = false;
131128

@@ -139,4 +136,28 @@ public function testFailureDoesNotTriggerEvent()
139136

140137
$this->assertNull($result);
141138
}
139+
140+
public function testDestructDoesNotThrowException()
141+
{
142+
$email = $this->getMockBuilder(Email::class)
143+
->disableOriginalConstructor()
144+
->onlyMethods(['sendCommand'])
145+
->getMock();
146+
$email->expects($this->once())->method('sendCommand')
147+
->willThrowException(new ErrorException('SMTP Error.'));
148+
149+
// Force resource to be injected into the property
150+
$SMTPConnect = fopen(__FILE__, 'rb');
151+
$this->setPrivateProperty($email, 'SMTPConnect', $SMTPConnect);
152+
153+
$email->__destruct();
154+
}
155+
156+
private function createMockEmail(): MockEmail
157+
{
158+
$config = config('Email');
159+
$config->validate = true;
160+
161+
return new MockEmail($config);
162+
}
142163
}

0 commit comments

Comments
 (0)