diff --git a/api/BusinessLogic/Attachments/AttachmentHandler.php b/api/BusinessLogic/Attachments/AttachmentHandler.php index c61a109d..04a73e42 100644 --- a/api/BusinessLogic/Attachments/AttachmentHandler.php +++ b/api/BusinessLogic/Attachments/AttachmentHandler.php @@ -3,14 +3,48 @@ namespace BusinessLogic\Attachments; -class AttachmentHandler { +use BusinessLogic\Exceptions\ValidationException; +use BusinessLogic\ValidationModel; +class AttachmentHandler { + /** + * @param $createAttachmentModel CreateAttachmentForTicketModel + */ function createAttachmentForTicket($createAttachmentModel) { - + $this->validate($createAttachmentModel); } + /** + * @param $createAttachmentModel CreateAttachmentForTicketModel + * @throws ValidationException + */ private function validate($createAttachmentModel) { - + $errorKeys = array(); + if ($createAttachmentModel->attachmentContents === null || + trim($createAttachmentModel->attachmentContents) === '') { + $errorKeys[] = 'CONTENTS_EMPTY'; + } + + if (base64_decode($createAttachmentModel->attachmentContents, true) === false) { + $errorKeys[] = 'CONTENTS_NOT_BASE_64'; + } + + if ($createAttachmentModel->displayName === null || + trim($createAttachmentModel->displayName === '')) { + $errorKeys[] = 'DISPLAY_NAME_EMPTY'; + } + + if ($createAttachmentModel->ticketId === null || + $createAttachmentModel->ticketId < 1) { + $errorKeys[] = 'TICKET_ID_MISSING'; + } + + if (count($errorKeys) > 0) { + $validationModel = new ValidationModel(); + $validationModel->errorKeys = $errorKeys; + $validationModel->valid = false; + throw new ValidationException($validationModel); + } } } \ No newline at end of file diff --git a/api/BusinessLogic/Attachments/CreateAttachmentModel.php b/api/BusinessLogic/Attachments/CreateAttachmentModel.php index 209450d2..9a5f5428 100644 --- a/api/BusinessLogic/Attachments/CreateAttachmentModel.php +++ b/api/BusinessLogic/Attachments/CreateAttachmentModel.php @@ -13,6 +13,6 @@ class CreateAttachmentModel { /* @var $id int */ public $fileSize; - /* @var $attachmentContents string [base64-encoded] */ + /* @var $attachmentContents string */ public $attachmentContents; } \ No newline at end of file diff --git a/api/Tests/BusinessLogic/Attachments/AttachmentHandlerTest.php b/api/Tests/BusinessLogic/Attachments/AttachmentHandlerTest.php index 335b3b88..e956e321 100644 --- a/api/Tests/BusinessLogic/Attachments/AttachmentHandlerTest.php +++ b/api/Tests/BusinessLogic/Attachments/AttachmentHandlerTest.php @@ -4,6 +4,7 @@ namespace BusinessLogic\Attachments; +use BusinessLogic\Exceptions\ValidationException; use PHPUnit\Framework\TestCase; class AttachmentHandlerTest extends TestCase { @@ -11,7 +12,98 @@ class AttachmentHandlerTest extends TestCase { /* @var $attachmentHandler AttachmentHandler */ private $attachmentHandler; + /* @var $createAttachmentModel CreateAttachmentForTicketModel */ + private $createAttachmentForTicketModel; + protected function setUp() { $this->attachmentHandler = new AttachmentHandler(); + $this->createAttachmentForTicketModel = new CreateAttachmentForTicketModel(); + $this->createAttachmentForTicketModel->attachmentContents = base64_encode('string'); + $this->createAttachmentForTicketModel->displayName = 'Display Name'; + $this->createAttachmentForTicketModel->ticketId = 1; + } + + function testThatValidateThrowsAnExceptionWhenTheAttachmentBodyIsNull() { + //-- Arrange + $this->createAttachmentForTicketModel->attachmentContents = null; + + //-- Assert + $this->expectException(ValidationException::class); + $this->expectExceptionMessageRegExp('/CONTENTS_EMPTY/'); + + //-- Act + $this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel); + } + + function testThatValidateThrowsAnExceptionWhenTheAttachmentBodyIsEmpty() { + //-- Arrange + $this->createAttachmentForTicketModel->attachmentContents = ''; + + //-- Assert + $this->expectException(ValidationException::class); + $this->expectExceptionMessageRegExp('/CONTENTS_EMPTY/'); + + //-- Act + $this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel); + } + + function testThatValidateThrowsAnExceptionWhenTheAttachmentBodyIsInvalidBase64() { + //-- Arrange + $this->createAttachmentForTicketModel->attachmentContents = 'invalid base 64'; + + //-- Assert + $this->expectException(ValidationException::class); + $this->expectExceptionMessageRegExp('/CONTENTS_NOT_BASE_64/'); + + //-- Act + $this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel); + } + + function testThatValidateThrowsAnExceptionWhenTheDisplayNameIsNull() { + //-- Arrange + $this->createAttachmentForTicketModel->displayName = null; + + //-- Assert + $this->expectException(ValidationException::class); + $this->expectExceptionMessageRegExp('/DISPLAY_NAME_EMPTY/'); + + //-- Act + $this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel); + } + + function testThatValidateThrowsAnExceptionWhenTheDisplayNameIsEmpty() { + //-- Arrange + $this->createAttachmentForTicketModel->displayName = ''; + + //-- Assert + $this->expectException(ValidationException::class); + $this->expectExceptionMessageRegExp('/DISPLAY_NAME_EMPTY/'); + + //-- Act + $this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel); + } + + function testThatValidateThrowsAnExceptionWhenTheTicketIdIsNull() { + //-- Arrange + $this->createAttachmentForTicketModel->ticketId = null; + + //-- Assert + $this->expectException(ValidationException::class); + $this->expectExceptionMessageRegExp('/TICKET_ID_MISSING/'); + + //-- Act + $this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel); + } + + function testThatValidateThrowsAnExceptionWhenTheTicketIdIsANonPositiveInteger() { + //-- Arrange + $this->createAttachmentForTicketModel->ticketId = 0; + + //-- Assert + $this->expectException(ValidationException::class); + $this->expectExceptionMessageRegExp('/TICKET_ID_MISSING/'); + + //-- Act + $this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel); } }