Skip to content

Commit ce29310

Browse files
authored
feat: validator for non null and non empty parameter + new utilities (#56)
1 parent 50fafa9 commit ce29310

11 files changed

Lines changed: 421 additions & 279 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"ext-curl": "*",
1717
"ext-dom": "*",
1818
"ext-libxml": "*",
19-
"apimatic/core-interfaces": "~0.1.0",
19+
"apimatic/core-interfaces": "~0.1.4",
2020
"apimatic/jsonmapper": "^3.1.1",
2121
"php-jsonpointer/php-jsonpointer": "^3.0.2"
2222
},

composer.lock

Lines changed: 292 additions & 266 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Request/Parameters/FormParam.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ private function __construct(string $key, $value)
3030

3131
/**
3232
* Sets encoding header with the key and value provided.
33-
*
34-
* @param string $key
35-
* @param string $value
3633
*/
3734
public function encodingHeader(string $key, string $value): self
3835
{

src/Request/Parameters/Parameter.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace Core\Request\Parameters;
66

77
use Closure;
8-
use CoreInterfaces\Core\Request\ParamInterface;
8+
use Core\Utils\CoreHelper;
9+
use CoreInterfaces\Core\Request\NonEmptyParamInterface;
910
use CoreInterfaces\Core\Request\TypeValidatorInterface;
1011
use InvalidArgumentException;
1112
use Throwable;
1213

13-
abstract class Parameter implements ParamInterface
14+
abstract class Parameter implements NonEmptyParamInterface
1415
{
1516
protected $key;
1617
protected $value;
@@ -63,7 +64,8 @@ private function extractFromArray(string $key, $defaultValue): self
6364
}
6465

6566
/**
66-
* Marks the value of the parameter as required and throws an exception on validate if the value is missing.
67+
* Marks the value of the parameter as required and
68+
* throws an exception on validate if the value is missing.
6769
*/
6870
public function required(): self
6971
{
@@ -73,6 +75,18 @@ public function required(): self
7375
return $this;
7476
}
7577

78+
/**
79+
* Marks the value of the parameter as required + non-empty and
80+
* throws an exception on validate if the value is missing.
81+
*/
82+
public function requiredNonEmpty(): self
83+
{
84+
if (CoreHelper::isNullOrEmpty($this->value)) {
85+
$this->valueMissing = true;
86+
}
87+
return $this;
88+
}
89+
7690
/**
7791
* Serializes the parameter using the method provided.
7892
*

src/Utils/CoreHelper.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public static function isAssociative(array $array): bool
8484
return false;
8585
}
8686

87+
/**
88+
* Check if provided value is null or empty.
89+
*
90+
* @param $value mixed Value to be checked.
91+
* @return bool True if given value is empty of null.
92+
*/
93+
public static function isNullOrEmpty($value): bool
94+
{
95+
if (is_string($value) && $value == '0') {
96+
return false;
97+
}
98+
return empty($value);
99+
}
100+
87101
/**
88102
* Check if all the given value or values are present in the provided list.
89103
*
@@ -150,4 +164,26 @@ public static function getOsInfo(string $osFamily = PHP_OS_FAMILY, string $funct
150164
}
151165
return $osFamily . '-' . call_user_func($functionName, 'r');
152166
}
167+
168+
/**
169+
* Return base64 encoded string for given username and password, prepended with Basic substring.
170+
*/
171+
public static function getBasicAuthEncodedString(string $username, string $password): string
172+
{
173+
if ($username == '' || $password == '') {
174+
return '';
175+
}
176+
return 'Basic ' . base64_encode("$username:$password");
177+
}
178+
179+
/**
180+
* Return the accessToken prepended with Bearer substring.
181+
*/
182+
public static function getBearerAuthString(string $accessToken): string
183+
{
184+
if ($accessToken == '') {
185+
return '';
186+
}
187+
return 'Bearer ' . $accessToken;
188+
}
153189
}

tests/AuthenticationTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public function testHeaderAuthWithMissingField()
4343
MockHelper::getClient()->validateAuth($auth);
4444
}
4545

46+
public function testHeaderAuthWithEmptyField()
47+
{
48+
$this->expectException(AuthValidationException::class);
49+
$this->expectExceptionMessage("Following authentication credentials are required:" .
50+
"\n-> Missing required header field: token");
51+
52+
$auth = Auth::or('headerWithEmpty');
53+
MockHelper::getClient()->validateAuth($auth);
54+
}
55+
4656
public function testHeaderOrQueryAuth1()
4757
{
4858
$request = new Request('http://localhost:3000');
@@ -85,6 +95,19 @@ public function testHeaderWithMissingFieldOrQueryAuth()
8595
);
8696
}
8797

98+
public function testHeaderWithEmptyFieldOrQueryAuth()
99+
{
100+
$request = new Request('http://localhost:3000');
101+
$auth = Auth::or('headerWithEmpty', 'query');
102+
MockHelper::getClient()->validateAuth($auth)->apply($request);
103+
104+
$this->assertEquals([], $request->getHeaders());
105+
$this->assertEquals(
106+
'http://localhost:3000?token=someAuthToken&authorization=accessToken',
107+
$request->getQueryUrl()
108+
);
109+
}
110+
88111
public function testHeaderOrQueryAuthWithMissingFields()
89112
{
90113
$this->expectException(AuthValidationException::class);
@@ -122,6 +145,16 @@ public function testHeaderWithMissingFieldAndQueryAuth()
122145
MockHelper::getClient()->validateAuth($auth);
123146
}
124147

148+
public function testHeaderWithEmptyFieldAndQueryAuth()
149+
{
150+
$this->expectException(AuthValidationException::class);
151+
$this->expectExceptionMessage("Following authentication credentials are required:" .
152+
"\n-> Missing required header field: token");
153+
154+
$auth = Auth::and('headerWithEmpty', 'query');
155+
MockHelper::getClient()->validateAuth($auth);
156+
}
157+
125158
public function testHeaderAndQueryAuthWithMissingFields()
126159
{
127160
$this->expectException(AuthValidationException::class);

tests/Mocking/Authentication/FormAuthManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class FormAuthManager extends CoreAuth
1010
public function __construct($token, $accessToken)
1111
{
1212
parent::__construct(
13-
FormParam::init('token', $token)->required(),
14-
FormParam::init('authorization', $accessToken)->required()
13+
FormParam::init('token', $token)->requiredNonEmpty(),
14+
FormParam::init('authorization', $accessToken)->requiredNonEmpty()
1515
);
1616
}
1717
}

tests/Mocking/Authentication/HeaderAuthManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class HeaderAuthManager extends CoreAuth
1010
public function __construct($token, $accessToken)
1111
{
1212
parent::__construct(
13-
HeaderParam::init('token', $token)->required(),
14-
HeaderParam::init('authorization', $accessToken)->required()
13+
HeaderParam::init('token', $token)->requiredNonEmpty(),
14+
HeaderParam::init('authorization', $accessToken)->requiredNonEmpty()
1515
);
1616
}
1717
}

tests/Mocking/Authentication/QueryAuthManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class QueryAuthManager extends CoreAuth
1010
public function __construct($token, $accessToken)
1111
{
1212
parent::__construct(
13-
QueryParam::init('token', $token)->required(),
14-
QueryParam::init('authorization', $accessToken)->required()
13+
QueryParam::init('token', $token)->requiredNonEmpty(),
14+
QueryParam::init('authorization', $accessToken)->requiredNonEmpty()
1515
);
1616
}
1717
}

tests/Mocking/MockHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public static function getClient(): Client
8383
->authManagers([
8484
"header" => new HeaderAuthManager('someAuthToken', 'accessToken'),
8585
"headerWithNull" => new HeaderAuthManager('someAuthToken', null),
86+
"headerWithEmpty" => new HeaderAuthManager('', 'accessToken'),
8687
"query" => new QueryAuthManager('someAuthToken', 'accessToken'),
8788
"queryWithNull" => new QueryAuthManager(null, 'accessToken'),
8889
"form" => new FormAuthManager('someAuthToken', 'accessToken'),

0 commit comments

Comments
 (0)