Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Email SMTP may throw Uncaught ErrorException #6184

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('[email protected]');

$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('[email protected]');
$email->setFrom('[email protected]');
$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('[email protected]');

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

$email->setTo('[email protected]');
$email->setFrom('[email protected]');

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('[email protected]');

$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('[email protected]');
$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);
}
}