Work on refactor, unit tests and image upload

This commit is contained in:
sctn4elk 2024-05-13 14:36:49 -05:00
parent 16baa575b1
commit 5e1bbf8817
9 changed files with 82 additions and 26 deletions

View File

@ -60,7 +60,7 @@ class BaseController {
*
* @return array
*/
protected function getUriSegments()
public function getUriSegments()
{
$requestUri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
$parsedUri = parse_url($requestUri, PHP_URL_PATH);
@ -72,7 +72,7 @@ class BaseController {
*
* @return array
*/
protected function getQueryStringParams()
public function getQueryStringParams()
{
$query = array();
$queryString = filter_input(INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
@ -80,7 +80,7 @@ class BaseController {
return $query;
}
protected function getServerRequestMethod()
public function getServerRequestMethod()
{
$requestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
return $requestMethod;
@ -91,7 +91,7 @@ class BaseController {
* @param mixed $data
* @param string $httpHeader
*/
protected function sendOutput($data, $httpHeaders=array())
public function sendOutput($data, $httpHeaders=array())
{
header_remove('Set-Cookie');
if (is_array($httpHeaders) && count($httpHeaders)) {

View File

@ -12,18 +12,21 @@ class CustomerController extends BaseController {
/**
* "/customer/list" Endpoint - Get list of users
*/
private $customerModel;
public $customerModel;
public $action;
function __construct() {
$this->customerModel = new CustomerModel();
$this->requestMethod = $this->getServerRequestMethod();
parent::__construct();
$this->basename = "CustomerController";
}
public function processAction() {
$this->strErrorDesc = '';
$this->strErrorHeader = '';
try {
$this->customerModel = new CustomerModel();
$this->requestMethod = $this->getServerRequestMethod();
switch($this->action) {
case "select":
$response = $this->selectAction();
@ -73,6 +76,7 @@ class CustomerController extends BaseController {
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return;
}
$this->arrQueryStringParams = $this->getQueryStringParams();
if (isset($this->arrQueryStringParams['customer_id'])) {
$response = $this->selectByIdAction();
@ -80,7 +84,7 @@ class CustomerController extends BaseController {
$this->customerModel->limit = 10;
if (isset($this->arrQueryStringParams['limit'])) {
$this->customerModel->limit = $this->arrQueryStringParams['limit'];
$customerModel->limit = $this->arrQueryStringParams['limit'];
}
$response = $this->customerModel->findAllCustomers();
unset($this->customerModel->limit);

View File

@ -8,12 +8,13 @@ header("Content-Type: application/json");
*/
class ImageController extends BaseController {
private $imageModel;
public $imageModel;
public $action;
#[\Override]
function __construct() {
$this->imageModel = new ImageModel();
$this->requestMethod = $this->getServerRequestMethod();
parent::__construct();
$this->basename = "ImageController";
}
/*
@ -23,6 +24,9 @@ class ImageController extends BaseController {
$this->strErrorDesc = '';
$this->strErrorHeader = '';
try {
$this->imageModel = new ImageModel();
$this->requestMethod = $this->getServerRequestMethod();
switch($this->action) {
case "upload":
$response = $this->uploadImage();
@ -73,6 +77,7 @@ class ImageController extends BaseController {
return;
}
// reads the raw POST data and returns it as a string.
//base64_decode($_POST['content']);
$imagePayload = file_get_contents($_FILES['your_image_name']['tmp_name']);
if (! $this->validateImage($imagePayload)) {
return $this->unprocessableEntityResponse();

View File

@ -12,5 +12,6 @@ require_once PROJECT_ROOT_PATH . "/include/config.php";
require_once PROJECT_ROOT_PATH . "/Controller/Api/BaseController.php";
// include the use model file
require_once PROJECT_ROOT_PATH . "/Model/CustomerModel.php";
require_once PROJECT_ROOT_PATH . "/Model/ImageModel.php";
// include the tests autoloader when in development
//require_once __DIR__ . '/../vendor/autoload.php';

View File

@ -27,7 +27,11 @@
* address_state varchar(45)
* address_zip varchar(10)
*/
require_once PROJECT_ROOT_PATH . "/Model/Database.php";
//define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
//require_once PROJECT_ROOT_PATH . "/Model/Database.php";
require_once PD . "/Model/Database.php";
require_once PD . "/Model/ModelTraits.php";
class CustomerModel extends Database {
use ModelTraits;

View File

@ -8,21 +8,29 @@
/**
* Description of ImageModel
*
* @author SCTN4
* @author Mike Howard
*
* Table: loyalty_value_blob
* Columns:
* loyalty_value_id bigint PK
* loyalty_member_id bigint
* loyalty_attribute_id bigint
* loyalty_value_mime_type varchar(255)
* loyalty_value_blob blob
*/
class ImageModel extends Database {
use ModelTraits;
public function insertImage($inputModel)
{
//$query = "INSERT INTO " . $this->imageTable;
//$query .= " (filename, filepath) VALUES (?,?)";
//return var_dump($jsonPayLoad);
$keys = array_keys($inputModel);
$n = count($keys);
$query .= "CALL insert_customer_image_proc (";
//$sql = "INSERT INTO ImageStore(ImageId,Image) VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')";
$query .= "CALL insert_loyalty_member_image_proc (" . $inputModel[$keys[0]] . ", ";
for($i = 0; $i < $n-1; $i++) {
$query .= "'" . $inputModel[$keys[$i]] . "', ";
}

View File

@ -5,7 +5,7 @@
*
* @author SCTN4
*/
Trait ModelTraits {
trait ModelTraits {
private $params = array();
/*

View File

@ -25,13 +25,13 @@ class BaseControllerTest extends TestCase {
public function testConstruct(): void
{
$this->assertSame('BaseController', $this->base->name);
$this->assertSame('BaseController', $this->base->basename);
}
public function testCreate(): void
{
$controllerObj = BaseController::create();
$this->assertSame('BaseController', $controllerObj->name);
$this->assertSame('BaseController', $controllerObj->basename);
}
#[Depends('testConstruct')]

View File

@ -13,22 +13,54 @@ use Symfony\Component\HttpClient\HttpClient;
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Controller/API/BaseController.php";
require_once PD . "/Controller/API/CustomerController.php";
require_once PD . "/Model/CustomerModel.php";
require_once PD . "/Model/ModelTraits.php";
require PD . "/include/bootstrap.php";
class CustomerControllerTest extends TestCase {
private readonly HttpClientInterface $httpClient;
protected $url = 'http://localhost/CustomerRewardsRESTAPI/index.php/customer/process/select';
protected $url = 'http://localhost/CustomerRewards/index.php/customer/process/select';
protected $control;
#[\Override]
public function setUp(): void {
$this->httpClient= HttpClient::create();
$this->control = new CustomerController();
}
#[\Override]
public function tearDown(): void {
//nothing to do
$this->control = null;
}
public function testConstruct(): void
{
$this->assertSame('CustomerController', $this->control->basename);
}
public function testCreate(): void
{
$controllerObj = BaseController::create();
$this->assertSame('BaseController', $controllerObj->basename);
}
#[Depends('testConstruct')]
public function testCall(): void
{
$this->control->mymethod = 'test';
$this->assertSame('test', $this->control->mymethod);
}
public function testProcessAction(): void
{
$this->markTestIncomplete(
'This test has not been implemented yet.',
);
//$this->control->customerModel->customer_id = 0;
//$this->assertSame(0, $this->control->customerModel->customer_id);
}
/*
* [
* {
@ -48,7 +80,9 @@ class CustomerControllerTest extends TestCase {
*/
public function testIndexSelectById(): void {
$response = $this->httpClient->request('GET', $this->url, [
'query' => ['customer_id' => 0]]);
'query' => ['customer_id' => 0]]);
//$this->assertSame("GET", $this->control->requestMethod);
$statusCode = $response->getStatusCode();
$this->assertSame(200, $statusCode);
@ -75,7 +109,7 @@ class CustomerControllerTest extends TestCase {
public function testIndexSelect(): void {
$response = $this->httpClient->request('GET', $this->url, [
'query' => ['limit' => 1]]);
'query' => ['limit' => 5]]);
$statusCode = $response->getStatusCode();
$this->assertSame(200, $statusCode);
@ -87,7 +121,7 @@ class CustomerControllerTest extends TestCase {
//Check if a single array is returned
$arrayCount = count($content);
$this->assertSame(1, $arrayCount);
$this->assertSame(4, $arrayCount);
//Check if the array has 11 elements
$arrayElementCount = count($content[0]);