From c19d8df1fa580599933b7d8b03fc623f4f1f89c1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 24 Jun 2022 11:43:29 +0900 Subject: [PATCH 1/4] test: extract method --- tests/system/Email/EmailTest.php | 34 ++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/tests/system/Email/EmailTest.php b/tests/system/Email/EmailTest.php index e4fc3ec26780..d72b51c46e39 100644 --- a/tests/system/Email/EmailTest.php +++ b/tests/system/Email/EmailTest.php @@ -44,9 +44,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 +57,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 +73,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 +86,7 @@ public function testEmailSendRepeatUpdatesArchive() { $config = config('Email'); $email = new MockEmail($config); + $email->setTo('foo@foo.com'); $email->setFrom('bar@foo.com'); @@ -104,9 +102,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 +120,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 +135,12 @@ public function testFailureDoesNotTriggerEvent() $this->assertNull($result); } + + private function createMockEmail(): MockEmail + { + $config = config('Email'); + $config->validate = true; + + return new MockEmail($config); + } } From 204287c258af82c7b211108df8ffb63e56d35936 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 24 Jun 2022 12:11:48 +0900 Subject: [PATCH 2/4] fix: Email SMTP may throw Uncaught ErrorException --- system/Email/Email.php | 8 +++++++- tests/system/Email/EmailTest.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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 d72b51c46e39..2fa0030f4fdd 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 @@ -136,6 +137,24 @@ public function testFailureDoesNotTriggerEvent() $this->assertNull($result); } + public function testDestructDoesNotThrowException() + { + $email = $this->getMockBuilder(Email::class) + ->disableOriginalConstructor() + ->onlyMethods(['sendCommand']) + ->getMock(); + $email->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(); + + $this->assertTrue(true); + } + private function createMockEmail(): MockEmail { $config = config('Email'); From b872d7b364c17fa4afdb1e3044c9585654ee0ee2 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 25 Jun 2022 05:42:27 +0900 Subject: [PATCH 3/4] test: add expects() and remove assertTrue(true) --- tests/system/Email/EmailTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/system/Email/EmailTest.php b/tests/system/Email/EmailTest.php index 2fa0030f4fdd..da428519071d 100644 --- a/tests/system/Email/EmailTest.php +++ b/tests/system/Email/EmailTest.php @@ -143,7 +143,7 @@ public function testDestructDoesNotThrowException() ->disableOriginalConstructor() ->onlyMethods(['sendCommand']) ->getMock(); - $email->method('sendCommand') + $email->expects($this->once())->method('sendCommand') ->willThrowException(new ErrorException('SMTP Error.')); // Force resource to be injected into the property @@ -151,8 +151,6 @@ public function testDestructDoesNotThrowException() $this->setPrivateProperty($email, 'SMTPConnect', $SMTPConnect); $email->__destruct(); - - $this->assertTrue(true); } private function createMockEmail(): MockEmail From 8fc648dcccd122915a00db559097d9ce0814a47d Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 1 Jul 2022 15:33:56 +0900 Subject: [PATCH 4/4] chore: skip GetMockBuilderGetMockToCreateMockRector --- rector.php | 6 ++++++ 1 file changed, 6 insertions(+) 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