Browse Source

Enable logging in SmtpService

Signed-off-by: Dušan Mitrović <dusan@dusanmitrovic.xyz>
master
Dušan Mitrović 8 months ago
parent
commit
18ac13534e
Signed by: dusan GPG Key ID: 8E81D1BFCE8427E5
3 changed files with 56 additions and 3 deletions
  1. +6
    -2
      src/Services/SmtpService.php
  2. +43
    -0
      tests/Unit/Services/SmtpServiceTest.php
  3. +7
    -1
      tests/Unit/Services/SmtpServiceTestCase.php

+ 6
- 2
src/Services/SmtpService.php View File

@@ -21,16 +21,19 @@ declare(strict_types=1);
namespace BrosSquad\LogSender\Services;

use BrosSquad\LogSender\DTO\MessageDTO;
use Monolog\Logger;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception as PHPMailerException;

class SmtpService
{
private PHPMailer $mailer;
private Logger $logger;

public function __construct(PHPMailer $mailer)
public function __construct(PHPMailer $mailer, Logger $logger)
{
$this->mailer = $mailer;
$this->logger = $logger;
}

public function send(MessageDTO $dto): bool
@@ -45,7 +48,8 @@ class SmtpService

return $this->mailer->send();
} catch (PHPMailerException $e) {
// TODO: Log this
$this->logger->log(Logger::ERROR, $e->getMessage());

return false;
}
}

+ 43
- 0
tests/Unit/Services/SmtpServiceTest.php View File

@@ -20,6 +20,9 @@ declare(strict_types=1);

namespace BrosSquad\LogSender\Tests\Unit\Services;

use Monolog\Logger;
use PHPMailer\PHPMailer\Exception as PHPMailerException;

/**
* @coversDefaultClass \BrosSquad\LogSender\Services\SmtpService
*/
@@ -58,4 +61,44 @@ final class SmtpServiceTest extends SmtpServiceTestCase

self::assertEquals(true, $result);
}

/**
* @covers ::send
*/
public function testSendThrowsException()
{
$phpMailerException = new PHPMailerException('An error occured.');

$this->phpMailerProphecy
->isHTML(true)
->shouldBeCalled();

$this->phpMailerProphecy
->send()
->shouldBeCalled()
->willThrow($phpMailerException);

$this->messageDTOProphecy
->isHtml()
->shouldBeCalled()
->willReturn(true);

$this->messageDTOProphecy
->getAltBody()
->shouldBeCalled()
->willReturn($this->altBody);

$this->messageDTOProphecy
->getBody()
->shouldBeCalled()
->willReturn($this->body);

$this->loggerProphecy
->log(Logger::ERROR, $phpMailerException->getMessage())
->shouldBeCalled();

$result = $this->smtpService->send($this->messageDTOProphecy->reveal());

self::assertEquals(false, $result);
}
}

+ 7
- 1
tests/Unit/Services/SmtpServiceTestCase.php View File

@@ -23,6 +23,7 @@ namespace BrosSquad\LogSender\Tests\Unit\Services;
use BrosSquad\LogSender\DTO\MessageDTO;
use BrosSquad\LogSender\Services\SmtpService;
use BrosSquad\LogSender\Tests\Unit\UnitTestCase;
use Monolog\Logger;
use PHPMailer\PHPMailer\PHPMailer;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
@@ -36,6 +37,7 @@ abstract class SmtpServiceTestCase extends UnitTestCase
protected string $altBody = 'Some important message!';
protected ObjectProphecy $phpMailerProphecy;
protected ObjectProphecy $messageDTOProphecy;
protected ObjectProphecy $loggerProphecy;
protected SmtpService $smtpService;

protected function setUp(): void
@@ -44,7 +46,11 @@ abstract class SmtpServiceTestCase extends UnitTestCase

$this->phpMailerProphecy = $this->prophesize(PHPMailer::class);
$this->messageDTOProphecy = $this->prophesize(MessageDTO::class);
$this->loggerProphecy = $this->prophesize(Logger::class);

$this->smtpService = new SmtpService($this->phpMailerProphecy->reveal());
$this->smtpService = new SmtpService(
$this->phpMailerProphecy->reveal(),
$this->loggerProphecy->reveal()
);
}
}

Loading…
Cancel
Save