Working on some validation tests
This commit is contained in:
parent
8378d35149
commit
ffd3ac2edf
@ -3,14 +3,48 @@
|
||||
namespace BusinessLogic\Attachments;
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -13,6 +13,6 @@ class CreateAttachmentModel {
|
||||
/* @var $id int */
|
||||
public $fileSize;
|
||||
|
||||
/* @var $attachmentContents string [base64-encoded] */
|
||||
/* @var $attachmentContents string */
|
||||
public $attachmentContents;
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user