Skip to content

Commit

Permalink
Merge pull request #6184 from kenjis/fix-email-Uncaught-ErrorException
Browse files Browse the repository at this point in the history
fix: Email SMTP may throw Uncaught ErrorException
  • Loading branch information
kenjis committed Jul 1, 2022
2 parents 397c606 + 8fc648d commit a1e8197
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
6 changes: 6 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
use Rector\PHPUnit\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector;
Expand Down Expand Up @@ -121,6 +122,11 @@

// use mt_rand instead of random_int on purpose on non-cryptographically random
RandomFunctionRector::class,

// @TODO remove if https://github.com/rectorphp/rector-phpunit/issues/86 is fixed
GetMockBuilderGetMockToCreateMockRector::class => [
__DIR__ . '/tests/system/Email/EmailTest.php',
],
]);

// auto import fully qualified class names
Expand Down
8 changes: 7 additions & 1 deletion system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2144,7 +2144,13 @@ protected function mimeTypes($ext = '')
public function __destruct()
{
if (is_resource($this->SMTPConnect)) {
$this->sendCommand('quit');
try {
$this->sendCommand('quit');
} catch (ErrorException $e) {
$protocol = $this->getProtocol();
$method = 'sendWith' . ucfirst($protocol);
log_message('error', 'Email: ' . $method . ' throwed ' . $e);
}
}
}

Expand Down
51 changes: 36 additions & 15 deletions tests/system/Email/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Events\Events;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockEmail;
use ErrorException;

/**
* @internal
Expand Down Expand Up @@ -44,9 +45,8 @@ public function autoClearProvider()
*/
public function testEmailSendWithClearance($autoClear)
{
$config = config('Email');
$config->validate = true;
$email = new MockEmail($config);
$email = $this->createMockEmail();

$email->setTo('foo@foo.com');

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

public function testEmailSendStoresArchive()
{
$config = config('Email');
$config->validate = true;
$email = new MockEmail($config);
$email = $this->createMockEmail();

$email->setTo('foo@foo.com');
$email->setFrom('bar@foo.com');
$email->setSubject('Archive Test');
Expand All @@ -75,9 +74,8 @@ public function testEmailSendStoresArchive()

public function testAutoClearLeavesArchive()
{
$config = config('Email');
$config->validate = true;
$email = new MockEmail($config);
$email = $this->createMockEmail();

$email->setTo('foo@foo.com');

$this->assertTrue($email->send(true));
Expand All @@ -89,6 +87,7 @@ public function testEmailSendRepeatUpdatesArchive()
{
$config = config('Email');
$email = new MockEmail($config);

$email->setTo('foo@foo.com');
$email->setFrom('bar@foo.com');

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

public function testSuccessDoesTriggerEvent()
{
$config = config('Email');
$config->validate = true;
$email = new MockEmail($config);
$email = $this->createMockEmail();

$email->setTo('foo@foo.com');

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

public function testFailureDoesNotTriggerEvent()
{
$config = config('Email');
$config->validate = true;
$email = new MockEmail($config);
$email = $this->createMockEmail();

$email->setTo('foo@foo.com');
$email->returnValue = false;

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

$this->assertNull($result);
}

public function testDestructDoesNotThrowException()
{
$email = $this->getMockBuilder(Email::class)
->disableOriginalConstructor()
->onlyMethods(['sendCommand'])
->getMock();
$email->expects($this->once())->method('sendCommand')
->willThrowException(new ErrorException('SMTP Error.'));

// Force resource to be injected into the property
$SMTPConnect = fopen(__FILE__, 'rb');
$this->setPrivateProperty($email, 'SMTPConnect', $SMTPConnect);

$email->__destruct();
}

private function createMockEmail(): MockEmail
{
$config = config('Email');
$config->validate = true;

return new MockEmail($config);
}
}

0 comments on commit a1e8197

Please sign in to comment.