Add Composer and phpunit, more work on CRUD

This commit is contained in:
sctn4elk 2024-05-01 15:03:43 -05:00
parent 6e50ad5eab
commit 3ba4af82fa
13 changed files with 2565 additions and 96 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@ dist/
nbdist/ nbdist/
.nb-gradle/ .nb-gradle/
/Include/config.php /Include/config.php
/vendor/

View File

@ -30,58 +30,28 @@ class CustomerController extends BaseController{
$this->arrQueryStringParams = $this->getQueryStringParams(); $this->arrQueryStringParams = $this->getQueryStringParams();
} }
public function processAction() public function processAction() {
{
$this->strErrorDesc = ''; $this->strErrorDesc = '';
try { try {
switch($this->action) { switch($this->action) {
case "select": case "select":
if (isset($this->arrQueryStringParams['id'])) { $response = $this->selectAction();
$response = $this->selectByIdAction();
} else {
$response = $this->selectAction();
}
break; break;
case "insert": case "insert":
/*$customerModel->first = $arrQueryStringParams['first'];
$customerModel->last = $arrQueryStringParams['last'];
$customerModel->email = $arrQueryStringParams['email'];
$customerModel->phone = $arrQueryStringParams['phone'];
$customerModel->birthday = $arrQueryStringParams['birthday'];
$customerModel->street = $arrQueryStringParams['street'];
$customerModel->city = $arrQueryStringParams['city'];
$customerModel->state = $arrQueryStringParams['state'];
$customerModel->zip = $arrQueryStringParams['zip'];
$customerModel->loyalty = $arrQueryStringParams['loyalty'];*/
$response = $this->insertCustomer(); $response = $this->insertCustomer();
/*unset($customerModel->first);
unset($customerModel->last);
unset($customerModel->email);
unset($customerModel->phone);
unset($customerModel->birthday);
unset($customerModel->street);
unset($customerModel->city);
unset($customerModel->state);
unset($customerModel->zip);
unset($customerModel->loyalty);*/
break; break;
case "update": case "update":
$response = $this->updateCustomer(); $response = $this->updateCustomer();
/*$arrCustomer = $this->customerModel->updateCustomer($arrQueryStringParams);*/
break; break;
case "delete": case "delete":
/*$arrCustomer = $this->customerModel->deleteCustomer($arrQueryStringParams);*/
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
$response = $this->deleteCustomer(); $response = $this->deleteCustomer();
break; break;
default: default:
$strErrorDesc = 'Controller Method not supported for processAction: ' . $action; $strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
$strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
break; break;
} }
@ -108,31 +78,26 @@ class CustomerController extends BaseController{
private function selectAction(){ private function selectAction(){
if ($this->checkRequestType('GET') == 'false') { if ($this->checkRequestType('GET') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return; return;
} }
if (isset($this->arrQueryStringParams['customer_id'])) {
$response = $this->selectByIdAction();
} else {
$this->customerModel->limit = 10;
$this->customerModel->limit = 10; if (isset($this->arrQueryStringParams['limit'])) {
$this->customerModel->limit = $this->arrQueryStringParams['limit'];
if (isset($this->arrQueryStringParams['limit'])) { }
$this->customerModel->limit = $this->arrQueryStringParams['limit']; $response = $this->customerModel->findAllCustomers();
unset($this->customerModel->limit);
} }
$response = $this->customerModel->findAllCustomers();
unset($this->customerModel->limit);
return $response; return $response;
} }
private function selectByIdAction(){ private function selectByIdAction(){
if (isset($this->arrQueryStringParams['customer_id'])) {
if ($this->checkRequestType('GET') == 'false') { $this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
return;
}
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$response = $this->customerModel->findByCustomerId(); $response = $this->customerModel->findByCustomerId();
unset($this->customerModel->customerId); unset($this->customerModel->customerId);
} else { } else {
@ -142,21 +107,11 @@ class CustomerController extends BaseController{
return $response; return $response;
} }
private function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
return $response;
}
private function insertCustomer() private function insertCustomer()
{ {
if ($this->checkRequestType('POST') == 'false') { if ($this->checkRequestType('POST') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return; return;
} }
// reads the raw POST data and returns it as a string. // reads the raw POST data and returns it as a string.
@ -172,21 +127,23 @@ class CustomerController extends BaseController{
{ {
if ($this->checkRequestType('PUT') == 'false') { if ($this->checkRequestType('PUT') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return; return;
} }
if (isset($this->arrQueryStringParams['customerId'])) { $jsonPayload = file_get_contents('php://input');
$this->customerModel->customerId = $this->arrQueryStringParams['customerId']; $input = json_decode($jsonPayload);
if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse();
}
if ($input->customer_id != null) {
$this->customerModel->customerId = $input->customer_id;
$result = $this->customerModel->findByCustomerId(); $result = $this->customerModel->findByCustomerId();
if (! $result) { if (! $result) {
return $this->notFoundResponse(); return $this->notFoundResponse();
} }
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse();
}
$response = $this->customerModel->updateCustomer($input); $response = $this->customerModel->updateCustomer($input);
unset($this->customerModel->customerId); unset($this->customerModel->customerId);
} else { } else {
@ -197,8 +154,13 @@ class CustomerController extends BaseController{
private function deleteCustomer() private function deleteCustomer()
{ {
if (isset($this->arrQueryStringParams['customerId'])) { if ($this->checkRequestType('DELETE') == 'false') {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId']; $this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return;
}
if (isset($this->arrQueryStringParams['customer_id'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
$result = $this->customerModel->findByCustomerId(); $result = $this->customerModel->findByCustomerId();
if (! $result) { if (! $result) {
return $this->notFoundResponse(); return $this->notFoundResponse();
@ -211,20 +173,24 @@ class CustomerController extends BaseController{
return $response; return $response;
} }
private function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
return $response;
}
private function validatePerson($input) private function validatePerson($input)
{ {
if (! isset($input['first'])) {
return false;
}
if (! isset($input['last'])) {
return false;
}
return true; return true;
} }
private function unprocessableEntityResponse() private function unprocessableEntityResponse()
{ {
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Entity'; $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload';
$response['body'] = json_encode([ $response['body'] = json_encode([
'error' => 'Invalid input' 'error' => 'Invalid input'
]); ]);
@ -233,7 +199,7 @@ class CustomerController extends BaseController{
private function notFoundResponse() private function notFoundResponse()
{ {
$response['status_code_header'] = 'HTTP/1.1 404 Not Found'; $response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
$response['body'] = null; $response['body'] = null;
return $response; return $response;
} }

View File

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

View File

@ -32,12 +32,18 @@ require_once PROJECT_ROOT_PATH . "/Model/Database.php";
class CustomerModel extends Database { class CustomerModel extends Database {
private $params = array(); private $params = array();
/*
* @assert ('name', 'value')
*/
public function __set($name, $value) public function __set($name, $value)
{ {
//echo "Setting '$name' to '$value'\n"; //echo "Setting '$name' to '$value'\n";
$this->params[$name] = $value; $this->params[$name] = $value;
} }
/*
* @assert ('name') == 'value'
*/
public function __get($name) public function __get($name)
{ {
//echo "Getting '$name'\n"; //echo "Getting '$name'\n";
@ -54,6 +60,10 @@ class CustomerModel extends Database {
return null; return null;
} }
/*
* @assert ('name') == 'true'
* @assert ('test') == 'false'
*/
public function __isset($name) public function __isset($name)
{ {
//echo "Is '$name' set?\n"; //echo "Is '$name' set?\n";
@ -76,10 +86,10 @@ class CustomerModel extends Database {
return $this->processQuery("SELECT * FROM customer_view WHERE customer_id = ?", ["i", $this->customerId]); return $this->processQuery("SELECT * FROM customer_view WHERE customer_id = ?", ["i", $this->customerId]);
} }
public function insertCustomer(Array $jsonPayLoad) public function insertCustomer($jsonPayLoad)
{ {
$rowCount = $this->processStatement("CALL insert_new_customer_proc(?,?,?,?,?,?,?,?,?,?)", $rowCount = $this->processStatement("CALL insert_new_customer_proc(?,?,?,?,?,?,?,?,?,?)",
[$jsonPayLoad->first, [$jsonPayLoad->customer_name_first,
$jsonPayLoad->last, $jsonPayLoad->last,
$jsonPayLoad->email, $jsonPayLoad->email,
$jsonPayLoad->phone, $jsonPayLoad->phone,
@ -92,19 +102,20 @@ class CustomerModel extends Database {
return $rowCount; return $rowCount;
} }
public function updateCustomer(Array $jsonPayLoad) public function updateCustomer($jsonPayLoad)
{ {
$rowCount = $this->processStatement("CALL update_customer_proc(?,?,?,?,?,?,?,?,?,?)", $rowCount = $this->processStatement("CALL update_existing_customer_proc(?,?,?,?,?,?,?,?,?,?,?)",
[$jsonPayLoad->first, [$jsonPayLoad->customer_id,
$jsonPayLoad->last, $jsonPayLoad->customer_name_first,
$jsonPayLoad->email, $jsonPayLoad->customer_name_last,
$jsonPayLoad->phone, $jsonPayLoad->customer_email,
$jsonPayLoad->birthday, $jsonPayLoad->customer_phone,
$jsonPayLoad->street, $jsonPayLoad->customer_birthday,
$jsonPayLoad->city, $jsonPayLoad->customer_street,
$jsonPayLoad->state, $jsonPayLoad->address_city,
$jsonPayLoad->zip, $jsonPayLoad->address_state,
$jsonPayLoad->loyalty]); $jsonPayLoad->address_zip,
$jsonPayLoad->loyalty_member]);
return $rowCount; return $rowCount;
} }

View File

@ -15,6 +15,7 @@ class Database {
public function __construct() public function __construct()
{ {
try { try {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME); $this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME);
if ( mysqli_connect_errno()) { if ( mysqli_connect_errno()) {
@ -76,8 +77,10 @@ class Database {
throw New Exception("Unable to do prepared statement: " . $query); throw New Exception("Unable to do prepared statement: " . $query);
} }
if( $params ) { if( $params ) {
$stmt->bind_param(str_repeat('s', count($params)), ...$params); $stmt->bind_param(str_repeat("s", count($params)), ...$params);
//$stmt->bind_param("isssssssssi", $params[0],$params[1],$params[2],$params[3],$params[4],$params[5],$params[6],$params[7],$params[8],$params[9],$params[10]);
} }
$stmt->execute(); $stmt->execute();
return $stmt; return $stmt;
} catch(Exception $e) { } catch(Exception $e) {

View File

@ -0,0 +1,36 @@
<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/SeleniumTests/SeleneseTest.php to edit this template
*/
/**
* Description of BaseControllerTest
*
* @author SCTN4
*/
class BaseControllerTest extends PHPUnit_Framework_TestCase {
/**
* @var \RemoteWebDriver
*/
protected $webDriver;
public function setUp() {
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
}
public function tearDown() {
$this->webDriver->close();
}
protected $url = 'http://www.netbeans.org/';
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'NetBeans'
$this->assertContains('NetBeans', $this->webDriver->getTitle());
}
}

View File

@ -0,0 +1,36 @@
<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/SeleniumTests/SeleneseTest.php to edit this template
*/
/**
* Description of CustomerControllerTest
*
* @author SCTN4
*/
class CustomerControllerTest extends PHPUnit_Framework_TestCase {
/**
* @var \RemoteWebDriver
*/
protected $webDriver;
public function setUp() {
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
}
public function tearDown() {
$this->webDriver->close();
}
protected $url = 'http://www.netbeans.org/';
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'NetBeans'
$this->assertContains('NetBeans', $this->webDriver->getTitle());
}
}

View File

@ -0,0 +1,36 @@
<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/SeleniumTests/SeleneseTest.php to edit this template
*/
/**
* Description of CustomerModelTest
*
* @author SCTN4
*/
class CustomerModelTest extends PHPUnit_Framework_TestCase {
/**
* @var \RemoteWebDriver
*/
protected $webDriver;
public function setUp() {
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
}
public function tearDown() {
$this->webDriver->close();
}
protected $url = 'http://www.netbeans.org/';
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'NetBeans'
$this->assertContains('NetBeans', $this->webDriver->getTitle());
}
}

19
composer.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "sctn4/customer-rewards-restapi",
"type": "library",
"autoload": {
"psr-4": {
"Sctn4\\CustomerRewardsRestapi\\": "src/"
}
},
"authors": [
{
"name": "sctn4elk",
"email": "sctn4elk@gmail.com"
}
],
"require-dev": {
"phpunit/phpunit": "11",
"vitexsoftware/phpunit-skeleton-generator": "*"
}
}

2345
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,12 @@
browser.reload.on.save=true browser.reload.on.save=true
code.analysis.excludes=
ignore.path=
include.path=${php.global.include.path} include.path=${php.global.include.path}
php.version=PHP_81 php.version=PHP_83
selenium.src.dir=Tests
source.encoding=UTF-8 source.encoding=UTF-8
src.dir=. src.dir=.
tags.asp=false tags.asp=false
tags.short=false tags.short=false
testing.providers=
web.root=. web.root=.

9
phpunit.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Customer Rewards REST API Tests">
<directory>Tests</directory>
</testsuite>
</testsuites>
</phpunit>