diff --git a/rector.php b/rector.php index 90c00508dfb6..9631fd97fd0d 100644 --- a/rector.php +++ b/rector.php @@ -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; @@ -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 diff --git a/system/Email/Email.php b/system/Email/Email.php index 9d52b42e4876..55c9c95e2205 100644 --- a/system/Email/Email.php +++ b/system/Email/Email.php @@ -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); + } } } diff --git a/tests/system/Email/EmailTest.php b/tests/system/Email/EmailTest.php index e4fc3ec26780..da428519071d 100644 --- a/tests/system/Email/EmailTest.php +++ b/tests/system/Email/EmailTest.php @@ -14,6 +14,7 @@ use CodeIgniter\Events\Events; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockEmail; +use ErrorException; /** * @internal @@ -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)); @@ -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'); @@ -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)); @@ -89,6 +87,7 @@ public function testEmailSendRepeatUpdatesArchive() { $config = config('Email'); $email = new MockEmail($config); + $email->setTo('foo@foo.com'); $email->setFrom('bar@foo.com'); @@ -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; @@ -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; @@ -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); + } }