AddEmbeddedImageTest: fully test exceptions for "fail to attach" test cases

This commit:
* Adds an `exceptionMessage` index to the `dataFailToAttach()` data provider.
* Renames the `testEmbeddedImageEncodingException()` method to `testFailToAttachException()`.
* Sets the `testFailToAttachException()` method up to use the `dataFailToAttach()` data provider.
* Adds testing of the exception message to the `testFailToAttachException()` method.

With this change, the "fail to attach" test cases are now fully tested for both a `PHPMailer` instance without exceptions enabled, as well as for an instance _with_ exceptions enabled.
This commit is contained in:
jrfnl 2021-07-04 15:40:06 +02:00
parent eb666a7e5d
commit 90c98c3c1c
1 changed files with 39 additions and 22 deletions

View File

@ -110,12 +110,13 @@ final class AddEmbeddedImageTest extends PreSendTestCase
*
* @dataProvider dataFailToAttach
*
* @param string $path Path to the attachment.
* @param string $cid Content ID for the attachment.
* @param string $name Optional. Attachment name to use.
* @param string $encoding Optional. File encoding to pass.
* @param string $path Path to the attachment.
* @param string $cid Content ID for the attachment.
* @param string $exceptionMessage Unused in this test.
* @param string $name Optional. Attachment name to use.
* @param string $encoding Optional. File encoding to pass.
*/
public function testFailToAttach($path, $cid, $name = '', $encoding = PHPMailer::ENCODING_BASE64)
public function testFailToAttach($path, $cid, $exceptionMessage, $name = '', $encoding = PHPMailer::ENCODING_BASE64)
{
$result = $this->Mail->addEmbeddedImage($path, $cid, $name, $encoding);
self::assertFalse($result, 'Image did not fail to attach');
@ -123,6 +124,31 @@ final class AddEmbeddedImageTest extends PreSendTestCase
self::assertFalse($this->Mail->inlineImageExists(), 'Inline image present in attachments array');
}
/**
* Test that embedding an image throws an exception in select use cases.
*
* @dataProvider dataFailToAttach
*
* @param string $path Path to the attachment.
* @param string $cid Content ID for the attachment.
* @param string $exceptionMessage The exception message to expect.
* @param string $name Optional. Attachment name to use.
* @param string $encoding Optional. File encoding to pass.
*/
public function testFailToAttachException(
$path,
$cid,
$exceptionMessage,
$name = '',
$encoding = PHPMailer::ENCODING_BASE64
) {
$this->expectException(Exception::class);
$this->expectExceptionMessage($exceptionMessage);
$mail = new PHPMailer(true);
$mail->addEmbeddedImage($path, $cid, $name, $encoding);
}
/**
* Data provider.
*
@ -132,26 +158,17 @@ final class AddEmbeddedImageTest extends PreSendTestCase
{
return [
'Invalid: non-existent file' => [
'path' => 'thisfiledoesntexist',
'cid' => 'xyz',
'path' => 'thisfiledoesntexist',
'cid' => 'xyz',
'exceptionMessage' => 'Could not access file: thisfiledoesntexist',
],
'Invalid: invalid encoding' => [
'path' => __FILE__,
'cid' => 'cid',
'name' => 'test.png',
'encoding' => 'invalidencoding',
'path' => __FILE__,
'cid' => 'cid',
'exceptionMessage' => 'Unknown encoding: invalidencoding',
'name' => 'test.png',
'encoding' => 'invalidencoding',
],
];
}
/**
* Expect exceptions on bad encoding
*/
public function testEmbeddedImageEncodingException()
{
$this->expectException(Exception::class);
$mail = new PHPMailer(true);
$mail->addEmbeddedImage(__FILE__, 'cid', 'test.png', 'invalidencoding');
}
}