From 10f76cd790bd2ac83746d92e2c8b7f8cbcba1bcc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 11 Sep 2024 19:16:50 +0200 Subject: [PATCH 1/2] DKIMTest: make exception expectation more specific As things were, the `DKIMTest::testDKIMSignOpenSSLNotAvailableException()` test _could_ potentially pass even when another `Exception` than the `PHPMailer\PHPMailer\Exception` was being thrown, as _all_ exceptions extend the PHP native `Exception` class. Now this risk is not that high, as there is also a check on the exception message, but still. Making the exception expectation more specific (by changing the import `use` statement), should still make the test more stable. --- test/PHPMailer/DKIMTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/PHPMailer/DKIMTest.php b/test/PHPMailer/DKIMTest.php index 48080460..c9fcce9c 100644 --- a/test/PHPMailer/DKIMTest.php +++ b/test/PHPMailer/DKIMTest.php @@ -13,7 +13,7 @@ namespace PHPMailer\Test\PHPMailer; -use Exception; +use PHPMailer\PHPMailer\Exception; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\Test\SendTestCase; From 78146fb172c80a44cb56e2ce6217d0a446b158bb Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 11 Sep 2024 21:04:29 +0200 Subject: [PATCH 2/2] DKIMTest::testDKIMSignOpenSSLNotAvailable(): fix the test As things were, the `DKIMTest::testDKIMSignOpenSSLNotAvailable()` could not pass as the `DKIMTest` class sets the `USE_EXCEPTIONS` class constant to `true`, which means the method would fail on an exception. As this test is specifically about testing the behaviour when exceptions are _disabled_, the test needs to be in its own test class, which sets `USE_EXCEPTIONS` to `false`. That should allow the test to run properly and to pass. --- test/PHPMailer/DKIMTest.php | 15 ------- test/PHPMailer/DKIMWithoutExceptionsTest.php | 47 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 test/PHPMailer/DKIMWithoutExceptionsTest.php diff --git a/test/PHPMailer/DKIMTest.php b/test/PHPMailer/DKIMTest.php index c9fcce9c..63034ab0 100644 --- a/test/PHPMailer/DKIMTest.php +++ b/test/PHPMailer/DKIMTest.php @@ -240,21 +240,6 @@ final class DKIMTest extends SendTestCase self::assertTrue($this->Mail->send(), 'DKIM signed mail via mail() failed'); } - /** - * Verify behaviour of the DKIM_Sign method when Open SSL is not available. - * - * @covers \PHPMailer\PHPMailer\PHPMailer::DKIM_Sign - */ - public function testDKIMSignOpenSSLNotAvailable() - { - if (extension_loaded('openssl')) { - $this->markTestSkipped('Test requires OpenSSL *not* to be available'); - } - - $signature = $this->Mail->DKIM_Sign('foo'); - self::assertSame('', $signature); - } - /** * Verify behaviour of the DKIM_Sign method when Open SSL is not available and exceptions is enabled. * diff --git a/test/PHPMailer/DKIMWithoutExceptionsTest.php b/test/PHPMailer/DKIMWithoutExceptionsTest.php new file mode 100644 index 00000000..6e8429bb --- /dev/null +++ b/test/PHPMailer/DKIMWithoutExceptionsTest.php @@ -0,0 +1,47 @@ + + * @author Andy Prevost + * @copyright 2012 - 2020 Marcus Bointon + * @copyright 2004 - 2009 Andy Prevost + * @license https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html GNU Lesser General Public License + */ + +namespace PHPMailer\Test\PHPMailer; + +use PHPMailer\PHPMailer\PHPMailer; +use PHPMailer\Test\TestCase; + +/** + * Test DKIM signing functionality. + * + * @group dkim + */ +final class DKIMWithoutExceptionsTest extends TestCase +{ + /** + * Whether or not to initialize the PHPMailer object to throw exceptions. + * + * @var bool|null + */ + const USE_EXCEPTIONS = false; + + /** + * Verify behaviour of the DKIM_Sign method when Open SSL is not available and exceptions is disabled. + * + * @covers \PHPMailer\PHPMailer\PHPMailer::DKIM_Sign + */ + public function testDKIMSignOpenSSLNotAvailable() + { + if (extension_loaded('openssl')) { + $this->markTestSkipped('Test requires OpenSSL *not* to be available'); + } + + $signature = $this->Mail->DKIM_Sign('foo'); + self::assertSame('', $signature); + } +}