Working on TicketCreator tests
This commit is contained in:
parent
176b786279
commit
98cbd6e4dd
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BusinessObjects;
|
namespace BusinessLogic\Tickets;
|
||||||
|
|
||||||
class CreateTicketByCustomerModel {
|
class CreateTicketByCustomerModel {
|
||||||
// Metadata
|
// Metadata
|
||||||
|
@ -6,21 +6,26 @@ namespace BusinessLogic\Tickets;
|
|||||||
use BusinessLogic\Exceptions\ValidationException;
|
use BusinessLogic\Exceptions\ValidationException;
|
||||||
use BusinessLogic\Security\BanRetriever;
|
use BusinessLogic\Security\BanRetriever;
|
||||||
use BusinessLogic\ValidationModel;
|
use BusinessLogic\ValidationModel;
|
||||||
use BusinessObjects\CreateTicketByCustomerModel;
|
|
||||||
|
|
||||||
class TicketCreator {
|
class TicketCreator {
|
||||||
|
private $banRetriever;
|
||||||
|
|
||||||
|
function __construct($banRetriever) {
|
||||||
|
$this->banRetriever = $banRetriever;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $ticketRequest CreateTicketByCustomerModel
|
* @param $ticketRequest CreateTicketByCustomerModel
|
||||||
* @param $banRetriever BanRetriever
|
|
||||||
* @param $heskSettings array HESK settings
|
* @param $heskSettings array HESK settings
|
||||||
* @param $modsForHeskSettings array Mods for HESK settings
|
* @param $modsForHeskSettings array Mods for HESK settings
|
||||||
* @throws ValidationException When a required field in $ticket_request is missing
|
* @throws ValidationException When a required field in $ticket_request is missing
|
||||||
*/
|
*/
|
||||||
static function createTicketByCustomer($ticketRequest, $banRetriever, $heskSettings, $modsForHeskSettings) {
|
function createTicketByCustomer($ticketRequest, $heskSettings, $modsForHeskSettings) {
|
||||||
$validationModel = validate($ticketRequest, false, $banRetriever, $heskSettings, $modsForHeskSettings);
|
$validationModel = $this->validate($ticketRequest, false, $heskSettings, $modsForHeskSettings);
|
||||||
|
|
||||||
if (count($validationModel->errorKeys) > 0) {
|
if (count($validationModel->errorKeys) > 0) {
|
||||||
// Validation failed
|
// Validation failed
|
||||||
|
$validationModel->valid = false;
|
||||||
throw new ValidationException($validationModel);
|
throw new ValidationException($validationModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,12 +35,11 @@ class TicketCreator {
|
|||||||
/**
|
/**
|
||||||
* @param $ticketRequest CreateTicketByCustomerModel
|
* @param $ticketRequest CreateTicketByCustomerModel
|
||||||
* @param $staff bool
|
* @param $staff bool
|
||||||
* @param $banRetriever BanRetriever
|
|
||||||
* @param $heskSettings array HESK settings
|
* @param $heskSettings array HESK settings
|
||||||
* @param $modsForHeskSettings array Mods for HESK settings
|
* @param $modsForHeskSettings array Mods for HESK settings
|
||||||
* @return ValidationModel If errorKeys is empty, validation successful. Otherwise invalid ticket
|
* @return ValidationModel If errorKeys is empty, validation successful. Otherwise invalid ticket
|
||||||
*/
|
*/
|
||||||
function validate($ticketRequest, $staff, $banRetriever, $heskSettings, $modsForHeskSettings) {
|
function validate($ticketRequest, $staff, $heskSettings, $modsForHeskSettings) {
|
||||||
$TICKET_PRIORITY_CRITICAL = 0;
|
$TICKET_PRIORITY_CRITICAL = 0;
|
||||||
|
|
||||||
$validationModel = new ValidationModel();
|
$validationModel = new ValidationModel();
|
||||||
@ -44,7 +48,7 @@ class TicketCreator {
|
|||||||
$validationModel->errorKeys[] = 'NO_NAME';
|
$validationModel->errorKeys[] = 'NO_NAME';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hesk_validateEmail($ticketRequest->email, $heskSettings['multi_eml'], false)) {
|
/*if (hesk_validateEmail($ticketRequest->email, $heskSettings['multi_eml'], false)) {
|
||||||
$validationModel->errorKeys[] = 'INVALID_OR_MISSING_EMAIL';
|
$validationModel->errorKeys[] = 'INVALID_OR_MISSING_EMAIL';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +107,7 @@ class TicketCreator {
|
|||||||
|
|
||||||
if ($banRetriever->isEmailBanned($ticketRequest->email, $heskSettings)) {
|
if ($banRetriever->isEmailBanned($ticketRequest->email, $heskSettings)) {
|
||||||
$validationModel->errorKeys[] = 'EMAIL_BANNED';
|
$validationModel->errorKeys[] = 'EMAIL_BANNED';
|
||||||
}
|
}*/
|
||||||
|
|
||||||
// TODO Check if we're at the max number of tickets
|
// TODO Check if we're at the max number of tickets
|
||||||
// TODO submit_ticket.php:325-334
|
// TODO submit_ticket.php:325-334
|
||||||
|
78
api/Tests/BusinessLogic/Tickets/TicketCreatorTest.php
Normal file
78
api/Tests/BusinessLogic/Tickets/TicketCreatorTest.php
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: mkoch
|
||||||
|
* Date: 2/4/2017
|
||||||
|
* Time: 9:32 PM
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace BusinessLogic\Tickets;
|
||||||
|
|
||||||
|
|
||||||
|
use BusinessLogic\Exceptions\ValidationException;
|
||||||
|
use BusinessLogic\Security\BanRetriever;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class TicketCreatorTest extends TestCase {
|
||||||
|
/**
|
||||||
|
* @var $ticketCreator TicketCreator
|
||||||
|
*/
|
||||||
|
private $ticketCreator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $banRetriever BanRetriever
|
||||||
|
*/
|
||||||
|
private $banRetriever;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $ticketRequest CreateTicketByCustomerModel
|
||||||
|
*/
|
||||||
|
private $ticketRequest;
|
||||||
|
|
||||||
|
private $heskSettings = array();
|
||||||
|
private $modsForHeskSettings = array();
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
$this->banRetriever = $this->createMock(BanRetriever::class);
|
||||||
|
$this->ticketCreator = new TicketCreator($this->banRetriever);
|
||||||
|
|
||||||
|
$this->ticketRequest = new CreateTicketByCustomerModel();
|
||||||
|
$this->ticketRequest->name = 'Name';
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItAddsTheProperValidationErrorWhenNameIsNull() {
|
||||||
|
//-- Arrange
|
||||||
|
$this->ticketRequest->name = null;
|
||||||
|
|
||||||
|
//-- Act
|
||||||
|
$exceptionThrown = false;
|
||||||
|
try {
|
||||||
|
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings);
|
||||||
|
} catch (ValidationException $e) {
|
||||||
|
//-- Assert (1/2)
|
||||||
|
$exceptionThrown = true;
|
||||||
|
$this->assertArraySubset(['NO_NAME'], $e->validationModel->errorKeys);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-- Assert (2/2)
|
||||||
|
$this->assertThat($exceptionThrown, $this->equalTo(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItAddsTheProperValidationErrorWhenNameIsBlank() {
|
||||||
|
//-- Arrange
|
||||||
|
$this->ticketRequest->name = '';
|
||||||
|
|
||||||
|
//-- Act
|
||||||
|
$exceptionThrown = false;
|
||||||
|
try {
|
||||||
|
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings);
|
||||||
|
} catch (ValidationException $e) {
|
||||||
|
//-- Assert (1/2)
|
||||||
|
$exceptionThrown = true;
|
||||||
|
$this->assertArraySubset(['NO_NAME'], $e->validationModel->errorKeys);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-- Assert (2/2)
|
||||||
|
$this->assertThat($exceptionThrown, $this->equalTo(true));
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
spl_autoload_register(function ($class) {
|
spl_autoload_register(function ($class) {
|
||||||
|
// Uncomment for debugging
|
||||||
|
//echo 'Looking for class ' . $class . "\n";
|
||||||
|
|
||||||
$file = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', '/', $class) . '.php';
|
$file = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', '/', $class) . '.php';
|
||||||
|
|
||||||
if (file_exists($file)) {
|
if (file_exists($file)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user