From 94a2d068ec61bfa364ad983d52d3e99f04a34572 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Sun, 14 Apr 2024 17:57:54 +0300 Subject: [PATCH] fix: Run CS Fixer --- .gitignore | 1 + webfiori/http/APITestCase.php | 176 +++++++++++++-------------- webfiori/http/WebServicesManager.php | 2 + 3 files changed, 90 insertions(+), 89 deletions(-) diff --git a/.gitignore b/.gitignore index fff42b1..de025d7 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ tests/json.json php-cs-fixer-v2.phar .idea/* php-cs-fixer.phar +.php-cs-fixer.cache diff --git a/webfiori/http/APITestCase.php b/webfiori/http/APITestCase.php index e78f571..6344aa5 100644 --- a/webfiori/http/APITestCase.php +++ b/webfiori/http/APITestCase.php @@ -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; @@ -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. * @@ -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. * @@ -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. * @@ -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; @@ -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]); } diff --git a/webfiori/http/WebServicesManager.php b/webfiori/http/WebServicesManager.php index c2d00c5..2d8efa7 100644 --- a/webfiori/http/WebServicesManager.php +++ b/webfiori/http/WebServicesManager.php @@ -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();