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/
.nb-gradle/
/Include/config.php
/vendor/

View File

@ -30,58 +30,28 @@ class CustomerController extends BaseController{
$this->arrQueryStringParams = $this->getQueryStringParams();
}
public function processAction()
{
public function processAction() {
$this->strErrorDesc = '';
try {
switch($this->action) {
case "select":
if (isset($this->arrQueryStringParams['id'])) {
$response = $this->selectByIdAction();
} else {
$response = $this->selectAction();
}
$response = $this->selectAction();
break;
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();
/*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;
case "update":
$response = $this->updateCustomer();
/*$arrCustomer = $this->customerModel->updateCustomer($arrQueryStringParams);*/
break;
case "delete":
/*$arrCustomer = $this->customerModel->deleteCustomer($arrQueryStringParams);*/
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
$response = $this->deleteCustomer();
break;
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';
break;
}
@ -108,31 +78,26 @@ class CustomerController extends BaseController{
private function selectAction(){
if ($this->checkRequestType('GET') == 'false') {
$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;
}
$this->customerModel->limit = 10;
if (isset($this->arrQueryStringParams['customer_id'])) {
$response = $this->selectByIdAction();
} else {
$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;
}
private function selectByIdAction(){
if ($this->checkRequestType('GET') == 'false') {
$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'];
if (isset($this->arrQueryStringParams['customer_id'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
$response = $this->customerModel->findByCustomerId();
unset($this->customerModel->customerId);
} else {
@ -142,21 +107,11 @@ class CustomerController extends BaseController{
return $response;
}
private function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
return $response;
}
private function insertCustomer()
{
if ($this->checkRequestType('POST') == 'false') {
$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;
}
// reads the raw POST data and returns it as a string.
@ -172,21 +127,23 @@ class CustomerController extends BaseController{
{
if ($this->checkRequestType('PUT') == 'false') {
$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;
}
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$jsonPayload = file_get_contents('php://input');
$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();
if (! $result) {
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);
unset($this->customerModel->customerId);
} else {
@ -197,8 +154,13 @@ class CustomerController extends BaseController{
private function deleteCustomer()
{
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
if ($this->checkRequestType('DELETE') == 'false') {
$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();
if (! $result) {
return $this->notFoundResponse();
@ -211,20 +173,24 @@ class CustomerController extends BaseController{
return $response;
}
private function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
return $response;
}
private function validatePerson($input)
{
if (! isset($input['first'])) {
return false;
}
if (! isset($input['last'])) {
return false;
}
return true;
}
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([
'error' => 'Invalid input'
]);
@ -233,7 +199,7 @@ class CustomerController extends BaseController{
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;
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";
// include the use model file
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 {
private $params = array();
/*
* @assert ('name', 'value')
*/
public function __set($name, $value)
{
//echo "Setting '$name' to '$value'\n";
$this->params[$name] = $value;
}
/*
* @assert ('name') == 'value'
*/
public function __get($name)
{
//echo "Getting '$name'\n";
@ -54,6 +60,10 @@ class CustomerModel extends Database {
return null;
}
/*
* @assert ('name') == 'true'
* @assert ('test') == 'false'
*/
public function __isset($name)
{
//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]);
}
public function insertCustomer(Array $jsonPayLoad)
public function insertCustomer($jsonPayLoad)
{
$rowCount = $this->processStatement("CALL insert_new_customer_proc(?,?,?,?,?,?,?,?,?,?)",
[$jsonPayLoad->first,
[$jsonPayLoad->customer_name_first,
$jsonPayLoad->last,
$jsonPayLoad->email,
$jsonPayLoad->phone,
@ -92,19 +102,20 @@ class CustomerModel extends Database {
return $rowCount;
}
public function updateCustomer(Array $jsonPayLoad)
public function updateCustomer($jsonPayLoad)
{
$rowCount = $this->processStatement("CALL update_customer_proc(?,?,?,?,?,?,?,?,?,?)",
[$jsonPayLoad->first,
$jsonPayLoad->last,
$jsonPayLoad->email,
$jsonPayLoad->phone,
$jsonPayLoad->birthday,
$jsonPayLoad->street,
$jsonPayLoad->city,
$jsonPayLoad->state,
$jsonPayLoad->zip,
$jsonPayLoad->loyalty]);
$rowCount = $this->processStatement("CALL update_existing_customer_proc(?,?,?,?,?,?,?,?,?,?,?)",
[$jsonPayLoad->customer_id,
$jsonPayLoad->customer_name_first,
$jsonPayLoad->customer_name_last,
$jsonPayLoad->customer_email,
$jsonPayLoad->customer_phone,
$jsonPayLoad->customer_birthday,
$jsonPayLoad->customer_street,
$jsonPayLoad->address_city,
$jsonPayLoad->address_state,
$jsonPayLoad->address_zip,
$jsonPayLoad->loyalty_member]);
return $rowCount;
}

View File

@ -15,6 +15,7 @@ class Database {
public function __construct()
{
try {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME);
if ( mysqli_connect_errno()) {
@ -76,8 +77,10 @@ class Database {
throw New Exception("Unable to do prepared statement: " . $query);
}
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();
return $stmt;
} 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

@ -36,7 +36,7 @@ if (!isset($uri[$uri_pos]) || !isset($uri[$uri_pos+1]) || !isset($uri[$uri_pos+2
header("HTTP/1.1 404 Not Found");
exit();
}
switch($uri[$uri_pos + 1]) {
case "purchase":
header("HTTP/1.1 404 Module Not Defined");

View File

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