Skip to content

Commit

Permalink
fix: Run CS Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Apr 14, 2024
1 parent 9cea908 commit 94a2d06
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ tests/json.json
php-cs-fixer-v2.phar
.idea/*
php-cs-fixer.phar
.php-cs-fixer.cache
176 changes: 87 additions & 89 deletions webfiori/http/APITestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* For more information on the license, please visit:
* https://github.com/WebFiori/http/blob/master/LICENSE
*/

namespace webfiori\http;

use PHPUnit\Framework\TestCase;
Expand All @@ -20,23 +19,82 @@ class APITestCase extends TestCase {
const NL = "\r\n";
const OUTPUT_STREAM = __DIR__.DIRECTORY_SEPARATOR.'outputStream.txt';
/**
* Sends a GET request to specific endpoint.
* Adds a file to the array $_FILES for testing API with upload.
*
* @param WebServicesManager $manager The manager which is used to manage the endpoint.
* @param string $fileIdx The name of the index that will hold the blob.
* This is usually represented by the attribute 'name' of file input in
* the front-end.
*
* @param string $endpoint The name of the endpoint.
* @param string $filePath The path of the file within testing environment.
*
* @param array $parameters An optional array of request parameters that can be
* passed to the endpoint.
* @param bool $reset If set to true, the array $_FILES will be re-initialized.
*/
public function addFile(string $fileIdx, string $filePath, bool $reset = false) {
if ($reset) {
$_FILES = [];
}

if (!isset($_FILES[$fileIdx])) {
$_FILES[$fileIdx] = [];
$_FILES[$fileIdx]['name'] = [];
$_FILES[$fileIdx]['type'] = [];
$_FILES[$fileIdx]['size'] = [];
$_FILES[$fileIdx]['tmp_name'] = [];
$_FILES[$fileIdx]['error'] = [];
}
$info = $this->extractPathAndName($filePath);
$path = $info['path'].DS.$info['name'];

$_FILES[$fileIdx]['name'][] = $info['name'];
$_FILES[$fileIdx]['type'][] = mime_content_type($path);
$_FILES[$fileIdx]['size'][] = filesize($this->getAbsolutePath());
$_FILES[$fileIdx]['tmp_name'][] = $path;
$_FILES[$fileIdx]['error'][] = 0;
}
/**
* Performs a call to an endpoint.
*
* @return string The method will return the output that was produced by
* the endpoint as string.
* @param WebServicesManager $manager The services manager instance that is used to
* manage the service.
*
* @param string $requestMethod A string that represents the name of request
* method such as 'get' or 'post'.
*
* @param string $apiEndpointName The name of the endpoint that will be called such as 'add-user'.
*
* @param array $parameters A dictionary thar represents the parameters that
* will be sent to the endpoint. The name is parameter name as it appears in
* service implementation and its value is the value of the parameter.
*
* @return string The method will return the output of the endpoint.
*/
public function getRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string {
return $this->callEndpoint($manager, RequestMethod::GET, $endpoint, $parameters);
public function callEndpoint(WebServicesManager $manager, string $requestMethod, string $apiEndpointName, array $parameters = []) : string {
$manager->setOutputStream(fopen(self::OUTPUT_STREAM,'w'));
$method = strtoupper($requestMethod);
putenv('REQUEST_METHOD='.$method);

if ($method == 'GET' || $method == 'DELETE') {
foreach ($parameters as $key => $val) {
$_GET[$key] = $val;
}
$_GET['service'] = $apiEndpointName;
$this->unset($_GET, $parameters, $manager);
} else if ($method == 'POST' || $method == 'PUT') {
foreach ($parameters as $key => $val) {
$_POST[$key] = $val;
}
$_POST['service'] = $apiEndpointName;
$_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
$this->unset($_POST, $parameters, $manager);
}

$retVal = $manager->readOutputStream();
unlink(self::OUTPUT_STREAM);

return $retVal;
}
/**
* Sends a POST request to specific endpoint.
* Sends a DELETE request to specific endpoint.
*
* @param WebServicesManager $manager The manager which is used to manage the endpoint.
*
Expand All @@ -48,11 +106,11 @@ public function getRequest(WebServicesManager $manager, string $endpoint, array
* @return string The method will return the output that was produced by
* the endpoint as string.
*/
public function postRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string {
return $this->callEndpoint($manager, RequestMethod::POST, $endpoint, $parameters);
public function deletRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string {
return $this->callEndpoint($manager, RequestMethod::DELETE, $endpoint, $parameters);
}
/**
* Sends a PUT request to specific endpoint.
* Sends a GET request to specific endpoint.
*
* @param WebServicesManager $manager The manager which is used to manage the endpoint.
*
Expand All @@ -64,11 +122,11 @@ public function postRequest(WebServicesManager $manager, string $endpoint, array
* @return string The method will return the output that was produced by
* the endpoint as string.
*/
public function putRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string {
return $this->callEndpoint($manager, RequestMethod::PUT, $endpoint, $parameters);
public function getRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string {
return $this->callEndpoint($manager, RequestMethod::GET, $endpoint, $parameters);
}
/**
* Sends a DELETE request to specific endpoint.
* Sends a POST request to specific endpoint.
*
* @param WebServicesManager $manager The manager which is used to manage the endpoint.
*
Expand All @@ -80,84 +138,24 @@ public function putRequest(WebServicesManager $manager, string $endpoint, array
* @return string The method will return the output that was produced by
* the endpoint as string.
*/
public function deletRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string {
return $this->callEndpoint($manager, RequestMethod::DELETE, $endpoint, $parameters);
public function postRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string {
return $this->callEndpoint($manager, RequestMethod::POST, $endpoint, $parameters);
}
/**
* Performs a call to an endpoint.
*
* @param WebServicesManager $manager The services manager instance that is used to
* manage the service.
*
* @param string $requestMethod A string that represents the name of request
* method such as 'get' or 'post'.
*
* @param string $apiEndpointName The name of the endpoint that will be called such as 'add-user'.
* Sends a PUT request to specific endpoint.
*
* @param array $parameters A dictionary thar represents the parameters that
* will be sent to the endpoint. The name is parameter name as it appears in
* service implementation and its value is the value of the parameter.
*
* @return string The method will return the output of the endpoint.
*/
public function callEndpoint(WebServicesManager $manager, string $requestMethod, string $apiEndpointName, array $parameters = []) : string {
$manager->setOutputStream(fopen(self::OUTPUT_STREAM,'w'));
$method = strtoupper($requestMethod);
putenv('REQUEST_METHOD='.$method);

if ($method == 'GET' || $method == 'DELETE') {

foreach ($parameters as $key => $val) {
$_GET[$key] = $val;
}
$_GET['service'] = $apiEndpointName;
$this->unset($_GET, $parameters, $manager);
} else if ($method == 'POST' || $method == 'PUT') {

foreach ($parameters as $key => $val) {
$_POST[$key] = $val;
}
$_POST['service'] = $apiEndpointName;
$_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
$this->unset($_POST, $parameters, $manager);
}

$retVal = $manager->readOutputStream();
unlink(self::OUTPUT_STREAM);

return $retVal;
}
/**
* Adds a file to the array $_FILES for testing API with upload.
* @param WebServicesManager $manager The manager which is used to manage the endpoint.
*
* @param string $fileIdx The name of the index that will hold the blob.
* This is usually represented by the attribute 'name' of file input in
* the front-end.
* @param string $endpoint The name of the endpoint.
*
* @param string $filePath The path of the file within testing environment.
* @param array $parameters An optional array of request parameters that can be
* passed to the endpoint.
*
* @param bool $reset If set to true, the array $_FILES will be re-initialized.
* @return string The method will return the output that was produced by
* the endpoint as string.
*/
public function addFile(string $fileIdx, string $filePath, bool $reset = false) {
if ($reset) {
$_FILES = [];
}
if (!isset($_FILES[$fileIdx])) {
$_FILES[$fileIdx] = [];
$_FILES[$fileIdx]['name'] = [];
$_FILES[$fileIdx]['type'] = [];
$_FILES[$fileIdx]['size'] = [];
$_FILES[$fileIdx]['tmp_name'] = [];
$_FILES[$fileIdx]['error'] = [];
}
$info = $this->extractPathAndName($filePath);
$path = $info['path'].DS.$info['name'];

$_FILES[$fileIdx]['name'][] = $info['name'];
$_FILES[$fileIdx]['type'][] = mime_content_type($path);
$_FILES[$fileIdx]['size'][] = filesize($this->getAbsolutePath());
$_FILES[$fileIdx]['tmp_name'][] = $path;
$_FILES[$fileIdx]['error'][] = 0;
public function putRequest(WebServicesManager $manager, string $endpoint, array $parameters = []) : string {
return $this->callEndpoint($manager, RequestMethod::PUT, $endpoint, $parameters);
}
private function extractPathAndName($absPath): array {
$DS = DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -185,7 +183,7 @@ private function extractPathAndName($absPath): array {
}
private function unset(array &$arr, array $params, WebServicesManager $m) {
$m->process();

foreach ($params as $key => $val) {
unset($arr[$key]);
}
Expand Down
2 changes: 2 additions & 0 deletions webfiori/http/WebServicesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,11 @@ private function _AfterParamsCheck($processReq) {
*/
private function _checkAction(): bool {
$serviceName = $this->getCalledServiceName();

//first, check if action is set and not null
if ($serviceName !== null) {
$calledService = $this->getServiceByName($serviceName);

//after that, check if action is supported by the API.
if ($calledService !== null) {
$allowedMethods = $calledService->getRequestMethods();
Expand Down

0 comments on commit 94a2d06

Please sign in to comment.