From 5e1bbf8817f5090fbf3b5e39d388a8d7251e1c7a Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Mon, 13 May 2024 14:36:49 -0500 Subject: [PATCH] Work on refactor, unit tests and image upload --- Controller/API/BaseController.php | 8 ++-- Controller/API/CustomerController.php | 14 +++--- Controller/API/ImageController.php | 11 +++-- Include/bootstrap.php | 1 + Model/CustomerModel.php | 6 ++- Model/ImageModel.php | 18 +++++--- Model/ModelTraits.php | 2 +- Tests/Controller/API/BaseControllerTest.php | 4 +- .../Controller/API/CustomerControllerTest.php | 44 ++++++++++++++++--- 9 files changed, 82 insertions(+), 26 deletions(-) diff --git a/Controller/API/BaseController.php b/Controller/API/BaseController.php index 784baf5..afc5968 100644 --- a/Controller/API/BaseController.php +++ b/Controller/API/BaseController.php @@ -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)) { diff --git a/Controller/API/CustomerController.php b/Controller/API/CustomerController.php index 8483b4c..2825425 100644 --- a/Controller/API/CustomerController.php +++ b/Controller/API/CustomerController.php @@ -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); diff --git a/Controller/API/ImageController.php b/Controller/API/ImageController.php index 82499e3..5c89790 100644 --- a/Controller/API/ImageController.php +++ b/Controller/API/ImageController.php @@ -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(); diff --git a/Include/bootstrap.php b/Include/bootstrap.php index 2805434..45bbd41 100644 --- a/Include/bootstrap.php +++ b/Include/bootstrap.php @@ -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'; diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index ecbff70..97a3e8e 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -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; diff --git a/Model/ImageModel.php b/Model/ImageModel.php index 66b0432..a7b3709 100644 --- a/Model/ImageModel.php +++ b/Model/ImageModel.php @@ -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]] . "', "; } diff --git a/Model/ModelTraits.php b/Model/ModelTraits.php index d94d852..8a48042 100644 --- a/Model/ModelTraits.php +++ b/Model/ModelTraits.php @@ -5,7 +5,7 @@ * * @author SCTN4 */ -Trait ModelTraits { +trait ModelTraits { private $params = array(); /* diff --git a/Tests/Controller/API/BaseControllerTest.php b/Tests/Controller/API/BaseControllerTest.php index a7d8e46..3adb3b2 100644 --- a/Tests/Controller/API/BaseControllerTest.php +++ b/Tests/Controller/API/BaseControllerTest.php @@ -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')] diff --git a/Tests/Controller/API/CustomerControllerTest.php b/Tests/Controller/API/CustomerControllerTest.php index e10fefd..66ff35e 100644 --- a/Tests/Controller/API/CustomerControllerTest.php +++ b/Tests/Controller/API/CustomerControllerTest.php @@ -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]);