Skip to content

Commit

Permalink
Merge pull request #49 from WebFiori/dev
Browse files Browse the repository at this point in the history
Refactoring: Created Class for Parameter Options
  • Loading branch information
usernane authored Jan 1, 2024
2 parents cc09c90 + b25b52f commit bae1872
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 50 deletions.
3 changes: 1 addition & 2 deletions examples/GetRandomService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
class GetRandomService extends AbstractWebService {
public function __construct() {
parent::__construct('get-random-number');
$this->addRequestMethod('get');
$this->addRequestMethod('post');
$this->setRequestMethods(['get', 'post']);

$this->addParameter(new RequestParameter('min', 'integer', true));
$this->addParameter(new RequestParameter('max', 'integer', true));
Expand Down
1 change: 1 addition & 0 deletions examples/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
require_once '../src/RequestParameter.php';
require_once '../src/Request.php';
require_once '../src/Response.php';
require_once '../src/ParamOption.php';
require_once '../src/Uri.php';
require_once 'GetRandomService.php';
1 change: 1 addition & 0 deletions tests/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'HttpCookie.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'HeadersPool.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'ParamTypes.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'ParamOption.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'AbstractWebService.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'APIFilter.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'RequestParameter.php';
Expand Down
37 changes: 20 additions & 17 deletions tests/webfiori/tests/http/RequestParameterTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
namespace webfiori\tests\http;

use Exception;
use PHPUnit\Framework\TestCase;
use webfiori\http\RequestParameter;
use webfiori\http\APIFilter;
use webfiori\http\ParamOption;
use webfiori\http\ParamTypes;
use webfiori\http\RequestParameter;
/**
* Description of RequestParameterTest
*
Expand Down Expand Up @@ -32,8 +35,8 @@ public function testCreateParameter01() {
*/
public function testCreateParameter02() {
$param = RequestParameter::create([
'name'=>'hello',
'type' => 'integer',
ParamOption::NAME =>'hello',
ParamOption::TYPE => 'integer',
'min' => 33,
'max' => 100,
'custom-filter' => function ($original, $basicFilterResult, $param) {
Expand All @@ -55,10 +58,10 @@ public function testCreateParameter02() {
public function testCreateParameter03() {
$param = RequestParameter::create([
'name'=>'ok',
'type' => 'string',
'default' => 'Ibrahim',
'allow-empty' => true,
'description' => 'Super param.'
ParamOption::TYPE => ParamTypes::STRING,
ParamOption::DEFAULT => 'Ibrahim',
ParamOption::EMPTY => true,
ParamOption::DESCRIPTION => 'Super param.'
]);
$this->assertNotNull($param);
$this->assertEquals('ok', $param->getName());
Expand All @@ -73,9 +76,9 @@ public function testCreateParameter03() {
public function testCreateParameter04() {
$param = RequestParameter::create([
'name'=>'ok',
'type' => 'int',
'default' => 44,
'description' => 'Super param.'
ParamOption::TYPE => 'int',
ParamOption::DEFAULT => 44,
ParamOption::DESCRIPTION => 'Super param.'
]);
$this->assertNotNull($param);
$this->assertEquals('ok', $param->getName());
Expand Down Expand Up @@ -234,7 +237,7 @@ public function testSetDefault00() {
$this->assertEquals('A string.',$rp->getDefault());
$this->assertFalse($rp->setDefault(44.99));
$this->assertFalse($rp->setDefault([]));
$this->assertFalse($rp->setDefault(new \Exception()));
$this->assertFalse($rp->setDefault(new Exception()));
$this->assertFalse($rp->setDefault(null));
$this->assertFalse($rp->setDefault(false));
}
Expand All @@ -249,7 +252,7 @@ public function testSetDefault01() {
$this->assertEquals('A string.',$rp->getDefault());
$this->assertFalse($rp->setDefault(44.99));
$this->assertFalse($rp->setDefault([]));
$this->assertFalse($rp->setDefault(new \Exception()));
$this->assertFalse($rp->setDefault(new Exception()));
$this->assertFalse($rp->setDefault(null));
$this->assertFalse($rp->setDefault(false));
}
Expand All @@ -264,7 +267,7 @@ public function testSetDefault02() {
$this->assertEquals('A string.',$rp->getDefault());
$this->assertFalse($rp->setDefault(44.99));
$this->assertFalse($rp->setDefault([]));
$this->assertFalse($rp->setDefault(new \Exception()));
$this->assertFalse($rp->setDefault(new Exception()));
$this->assertFalse($rp->setDefault(null));
$this->assertFalse($rp->setDefault(false));
}
Expand All @@ -278,7 +281,7 @@ public function testSetDefault03() {
$this->assertEquals(44,$rp->getDefault());
$this->assertFalse($rp->setDefault(44.99));
$this->assertFalse($rp->setDefault([]));
$this->assertFalse($rp->setDefault(new \Exception()));
$this->assertFalse($rp->setDefault(new Exception()));
$this->assertFalse($rp->setDefault(null));
$this->assertFalse($rp->setDefault(false));
}
Expand All @@ -291,7 +294,7 @@ public function testSetDefault04() {
$this->assertTrue($rp->setDefault(44));
$this->assertTrue($rp->setDefault(44.99));
$this->assertFalse($rp->setDefault([]));
$this->assertFalse($rp->setDefault(new \Exception()));
$this->assertFalse($rp->setDefault(new Exception()));
$this->assertFalse($rp->setDefault(null));
$this->assertFalse($rp->setDefault(false));
}
Expand All @@ -304,7 +307,7 @@ public function testSetDefault05() {
$this->assertFalse($rp->setDefault(44));
$this->assertFalse($rp->setDefault(44.99));
$this->assertFalse($rp->setDefault([]));
$this->assertFalse($rp->setDefault(new \Exception()));
$this->assertFalse($rp->setDefault(new Exception()));
$this->assertFalse($rp->setDefault(null));
$this->assertTrue($rp->setDefault(false));
}
Expand All @@ -317,7 +320,7 @@ public function testSetDefault06() {
$this->assertFalse($rp->setDefault(44));
$this->assertFalse($rp->setDefault(44.99));
$this->assertTrue($rp->setDefault([]));
$this->assertFalse($rp->setDefault(new \Exception()));
$this->assertFalse($rp->setDefault(new Exception()));
$this->assertFalse($rp->setDefault(null));
$this->assertFalse($rp->setDefault(false));
}
Expand Down
27 changes: 14 additions & 13 deletions tests/webfiori/tests/http/WebServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
namespace webfiori\tests\http;

use PHPUnit\Framework\TestCase;
use webfiori\tests\http\testServices\TestServiceObj;
use webfiori\http\ParamOption;
use webfiori\http\ParamTypes;
use webfiori\http\RequestMethod;
use webfiori\http\RequestParameter;
use webfiori\tests\http\testServices\NoAuthService;
use webfiori\tests\http\testServices\TestServiceObj;

class WebServiceTest extends TestCase {
/**
Expand Down Expand Up @@ -71,8 +74,8 @@ public function testAddParameter00() {
public function testAddParameter01() {
$action = new TestServiceObj('add-user');
$this->assertTrue($action->addParameter([
'name' => 'new-param',
'type' => 'boolean'
ParamOption::NAME => 'new-param',
ParamOption::TYPE => ParamTypes::BOOL
]));

$this->assertEquals(1,count($action->getParameters()));
Expand Down Expand Up @@ -111,9 +114,9 @@ public function testAddParameters02() {
$action->addParameters([
new RequestParameter('username'),
'password' => [
'optional' => true,
'default' => 1234,
'type' => 'integer'
ParamOption::OPTIONAL => true,
ParamOption::DEFAULT => 1234,
ParamOption::TYPE => 'integer'
]
]);
$this->assertEquals(2,count($action->getParameters()));
Expand Down Expand Up @@ -273,14 +276,13 @@ public function testToJson00() {
.'"request-methods":[],'
.'"parameters":[],'
.'"responses":[]}',$action->toJSON().'');
$action->addRequestMethod('get');
$action->addRequestMethod('put');
$action->addRequestMethod('post');
$action->setRequestMethods([RequestMethod::GET, RequestMethod::POST, RequestMethod::PUT]);

$this->assertEquals(''
.'{"name":"login",'
.'"since":"1.0.1",'
.'"description":"Allow the user to login to the system.",'
.'"request-methods":["GET","PUT","POST"],'
.'"request-methods":["GET","POST","PUT"],'
.'"parameters":[],'
.'"responses":[]}',$action->toJSON().'');
$action->removeRequestMethod('put');
Expand Down Expand Up @@ -337,7 +339,7 @@ public function testToJson00() {
*/
public function testToString00() {
$action = new TestServiceObj('get-user');
$action->addRequestMethod('get');
$action->addRequestMethod(RequestMethod::GET);
$action->addParameter(new RequestParameter('user-id', 'integer'));
$action->getParameterByName('user-id')->setDescription('The ID of the user.');
$action->setDescription('Returns a JSON string which holds user profile info.');
Expand Down Expand Up @@ -367,8 +369,7 @@ public function testToString00() {
*/
public function testToString01() {
$action = new TestServiceObj('add-user');
$action->addRequestMethod('post');
$action->addRequestMethod('put');
$action->setRequestMethods([RequestMethod::POST, RequestMethod::PUT]);
$action->addParameter(new RequestParameter('username'));
$action->addParameter(new RequestParameter('email'));
$action->getParameterByName('username')->setDescription('The username of the user.');
Expand Down
3 changes: 2 additions & 1 deletion tests/webfiori/tests/http/testServices/AddNubmersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace webfiori\tests\http\testServices;

use webfiori\http\RequestMethod;
use webfiori\http\RequestParameter;
/**
* Description of AddNubmersService
Expand All @@ -12,7 +13,7 @@ class AddNubmersService extends AbstractNumbersService {
public function __construct() {
parent::__construct('add-two-integers');
$this->setDescription('Returns a JSON string that has the sum of two integers.');
$this->addRequestMethod('get');
$this->addRequestMethod(RequestMethod::GET);

$this->addParameter(new RequestParameter('first-number', 'integer'));
$this->addParameter(new RequestParameter('second-number', 'integer'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace webfiori\tests\http\testServices;

use Exception;
use webfiori\http\ParamOption;
use webfiori\http\ParamTypes;
use webfiori\http\RequestMethod;
use webfiori\http\RequestParameter;
use webfiori\json\Json;
use Exception;
/**
* Description of CreateUserProfileService
*
Expand All @@ -13,20 +16,20 @@
class CreateUserProfileService extends AbstractNumbersService {
public function __construct() {
parent::__construct('create-user-profile');
$this->addRequestMethod('post');
$this->addRequestMethod(RequestMethod::POST);
$this->addParameter(new RequestParameter('id', 'integer'));
$this->getParameterByName('id')->setIsOptional(true);
$this->addParameters([
'name' => [
'type' => 'string'
ParamOption::TYPE => ParamTypes::STRING
],
'username' => [
'type' => 'string'
ParamOption::TYPE => ParamTypes::STRING
],
'x' => [
'type' => 'int',
'optional' => true,
'default' => 3
ParamOption::TYPE => ParamTypes::INT,
ParamOption::OPTIONAL => true,
ParamOption::DEFAULT => 3
]
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace webfiori\tests\http\testServices;

use webfiori\http\RequestMethod;
use webfiori\http\RequestParameter;
use webfiori\json\Json;
/**
Expand All @@ -12,7 +13,7 @@
class GetUserProfileService extends AbstractNumbersService {
public function __construct() {
parent::__construct('get-user-profile');
$this->addRequestMethod('post');
$this->addRequestMethod(RequestMethod::POST);
$this->setDescription('Returns a JSON string that has user profile info.');
$this->addParameter(new RequestParameter('user-id', 'integer'));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/webfiori/tests/http/testServices/MulNubmersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace webfiori\tests\http\testServices;

use webfiori\http\AbstractWebService;
use webfiori\http\RequestMethod;
use webfiori\http\RequestParameter;
/**
*
Expand All @@ -12,7 +13,7 @@ class MulNubmersService extends AbstractWebService {
public function __construct() {
parent::__construct('mul-two-integers');
$this->setDescription('Returns a JSON string that has the multiplication of two integers.');
$this->addRequestMethod('get');
$this->addRequestMethod(RequestMethod::GET);

$this->addParameter(new RequestParameter('first-number', 'integer'));
$this->addParameter(new RequestParameter('second-number', 'integer'));
Expand Down
3 changes: 2 additions & 1 deletion tests/webfiori/tests/http/testServices/NoAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace webfiori\tests\http\testServices;

use webfiori\http\AbstractWebService;
use webfiori\http\RequestMethod;
/**
* Description of NoAuthService
*
Expand All @@ -12,7 +13,7 @@ class NoAuthService extends AbstractWebService {
public function __construct() {
parent::__construct('ok-service');
$this->setIsAuthRequired(false);
$this->addRequestMethod('get');
$this->addRequestMethod(RequestMethod::GET);
}
public function isAuthorized() {
return false;
Expand Down
3 changes: 2 additions & 1 deletion tests/webfiori/tests/http/testServices/NotImplService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace webfiori\tests\http\testServices;

use webfiori\http\AbstractWebService;
use webfiori\http\RequestMethod;

/**
* Description of NotImplService
Expand All @@ -12,7 +13,7 @@
class NotImplService extends AbstractWebService {
public function __construct() {
parent::__construct('not-implemented');
$this->addRequestMethod('post');
$this->addRequestMethod(RequestMethod::POST);
}
public function isAuthorized() {

Expand Down
10 changes: 10 additions & 0 deletions webfiori/http/AbstractWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ public final function addRequestMethod(string $method) : bool {

return false;
}
/**
* Adds multiple request methods as one group.
*
* @param array $methods
*/
public function setRequestMethods(array $methods) {
foreach ($methods as $m) {
$this->addRequestMethod($m);
}
}
/**
* Adds response description.
*
Expand Down
11 changes: 5 additions & 6 deletions webfiori/http/ManagerInfoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ public function __construct() {
.'needed information about all end points which are registered '
.'under given manager.');
$this->addParameter([
'name' => 'version',
'type' => 'string',
'optional' => true,
'description' => 'Optional parameter. '
ParamOption::NAME => 'version',
ParamOption::TYPE => ParamTypes::STRING,
ParamOption::OPTIONAL => true,
ParamOption::DESCRIPTION => 'Optional parameter. '
.'If set, the information that will be returned will be specific '
.'to the given version number.'
]);
$this->addRequestMethod('get');
$this->addRequestMethod('post');
$this->setRequestMethods(RequestMethod::GET, RequestMethod::POST);
}
/**
* Sends back JSON response that contains information about the services
Expand Down
Loading

0 comments on commit bae1872

Please sign in to comment.