Working on tests for Resource
This commit is contained in:
parent
6c173cdc89
commit
aea1bae3bf
2 changed files with 197 additions and 121 deletions
|
@ -126,11 +126,6 @@ class Resource extends Object
|
|||
*/
|
||||
}
|
||||
|
||||
$filter = isset($this->filter[$method]);
|
||||
$validate = isset($this->validate[$method]);
|
||||
|
||||
if ($filter || $validate)
|
||||
{
|
||||
// Hack together some new globals
|
||||
if (in_array($method, ['PUT', 'DELETE']))
|
||||
{
|
||||
|
@ -139,6 +134,11 @@ class Resource extends Object
|
|||
// @todo Populate it
|
||||
}
|
||||
|
||||
$filter = isset($this->filter[$method]);
|
||||
$validate = isset($this->validate[$method]);
|
||||
|
||||
if ($filter || $validate)
|
||||
{
|
||||
$global =& $GLOBALS['_' . $method];
|
||||
|
||||
// Checks that the required parameters are present
|
||||
|
@ -199,16 +199,19 @@ class Resource extends Object
|
|||
{
|
||||
$rule = explode(':', $rule);
|
||||
|
||||
for ($i = 1; $i <= 2; $i++)
|
||||
{
|
||||
if (!isset($rule[$i]))
|
||||
{
|
||||
$rule[$i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
switch ($rule[0])
|
||||
{
|
||||
// {{{ Checks using filter_var()
|
||||
|
||||
case 'filter':
|
||||
if (!isset($rule[1]))
|
||||
{
|
||||
$rule[1] = false;
|
||||
}
|
||||
|
||||
switch ($rule[1])
|
||||
{
|
||||
case 'boolean':
|
||||
|
@ -218,17 +221,17 @@ class Resource extends Object
|
|||
case 'ip':
|
||||
case 'url':
|
||||
$filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1]));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('Invalid filter, expecting boolean, email, float, int, ip or url.');
|
||||
break;
|
||||
}
|
||||
|
||||
if (!filter_var($value, $filter))
|
||||
{
|
||||
$this->errors[$variable][] = $message;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->errors[$variable] = 'Invalid filter, expecting boolean, email, float, int, ip or url.';
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
@ -236,18 +239,6 @@ class Resource extends Object
|
|||
// {{{ Checks using strlen()
|
||||
|
||||
case 'length':
|
||||
if (count($rule) < 3)
|
||||
{
|
||||
throw new \Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!filter_var($rule[2], FILTER_VALIDATE_INT))
|
||||
{
|
||||
throw new \Exception('Invalid length value, expecting an integer.');
|
||||
}
|
||||
else
|
||||
{
|
||||
$length = strlen($value);
|
||||
|
||||
switch ($rule[1])
|
||||
|
@ -277,7 +268,8 @@ class Resource extends Object
|
|||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.');
|
||||
$valid = false;
|
||||
$message = 'Invalid operator, expecting <, <=, ==, !=, >= or >.';
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -285,8 +277,6 @@ class Resource extends Object
|
|||
{
|
||||
$this->errors[$variable][] = $message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
@ -294,20 +284,10 @@ class Resource extends Object
|
|||
// {{{ Checks using preg_match()
|
||||
|
||||
case 'regex':
|
||||
if (count($rule) < 3)
|
||||
{
|
||||
throw new \Exception('Invalid validation rule, expected: "regex:is|not:string".');
|
||||
}
|
||||
else
|
||||
{
|
||||
$rule[1] = strtolower($rule[1]);
|
||||
|
||||
if (($rule[1] == 'is' && preg_match($rule[2], $value))
|
||||
|| ($rule[1] == 'not' && !preg_match($rule[2], $value)))
|
||||
if (preg_match($rule[1], $value))
|
||||
{
|
||||
$this->errors[$variable][] = $message;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// }}}
|
||||
|
@ -346,25 +326,26 @@ class Resource extends Object
|
|||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
// Gets the profiler status
|
||||
// @todo Refactor out that stripos
|
||||
$profiler = $this->config->pickles['profiler'];
|
||||
$profiler = $profiler === true
|
||||
|| stripos($profiler, 'timers') !== false;
|
||||
$profiler = $this->config['pickles']['profiler'];
|
||||
|
||||
// Starts a timer before the resource is executed
|
||||
if ($profiler)
|
||||
{
|
||||
Profiler::timer('resource ' . $method);
|
||||
}
|
||||
*/
|
||||
|
||||
$this->response = $this->$method();
|
||||
|
||||
/*
|
||||
// Stops the resource timer
|
||||
if ($profiler)
|
||||
{
|
||||
Profiler::timer('resource ' . $method);
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,13 +35,31 @@ namespace Resources\v1
|
|||
'isURL' => ['filter:url' => 'Error'],
|
||||
'isNotURL' => ['filter:url' => 'Error'],
|
||||
'invalidRule' => ['filter' => 'Error'],
|
||||
'lessThan' => ['length:<:10' => 'Error'],
|
||||
'lessThanEqual' => ['length:<=:10' => 'Error'],
|
||||
'equal' => ['length:==:10' => 'Error'],
|
||||
'notEqual' => ['length:!=:10' => 'Error'],
|
||||
'greaterThan' => ['length:>=:10' => 'Error'],
|
||||
'greaterThanEqual' => ['length:>:10' => 'Error'],
|
||||
'greaterLessThan' => ['length:><:10' => 'Error'],
|
||||
'regex' => ['regex:/[a-z]+/' => 'Error'],
|
||||
],
|
||||
];
|
||||
|
||||
public function GET()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function PUT()
|
||||
{
|
||||
return ['foo' => 'bar'];
|
||||
}
|
||||
|
||||
public function ERROR()
|
||||
{
|
||||
throw new \Exception('Error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,8 +71,8 @@ namespace
|
|||
{
|
||||
$response = json_encode([
|
||||
'meta' => [
|
||||
'status' => 500,
|
||||
'message' => 'Invalid filter, expecting boolean, email, float, int, ip or url.',
|
||||
'status' => 400,
|
||||
'message' => 'Missing or invalid parameters.',
|
||||
'errors' => [
|
||||
'missing' => ['The missing parameter is required.'],
|
||||
'isNotBoolean' => ['Error'],
|
||||
|
@ -63,6 +81,9 @@ namespace
|
|||
'isNotInt' => ['Error'],
|
||||
'isNotIP' => ['Error'],
|
||||
'isNotURL' => ['Error'],
|
||||
'invalidRule' => ['Invalid filter, expecting boolean, email, float, int, ip or url.'],
|
||||
'greaterLessThan' => ['Invalid operator, expecting <, <=, ==, !=, >= or >.'],
|
||||
'regex' => ['Error'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
@ -87,6 +108,14 @@ namespace
|
|||
'isURL' => 'http://joshtronic.com',
|
||||
'isNotURL' => 'doubleUdoubleUdoubleUdot',
|
||||
'invalidRule' => 'invalid',
|
||||
'lessThan' => '...',
|
||||
'lessThanEqual' => '.......',
|
||||
'equal' => '..........',
|
||||
'notEqual' => '.......',
|
||||
'greaterThan' => '............',
|
||||
'greaterThanEqual' => '............',
|
||||
'greaterLessThan' => '......',
|
||||
'regex' => 'abc',
|
||||
];
|
||||
|
||||
new Pickles\Router();
|
||||
|
@ -112,7 +141,27 @@ namespace
|
|||
new Pickles\Router();
|
||||
}
|
||||
|
||||
public function testAuthMisconfigured()
|
||||
public function testPUT()
|
||||
{
|
||||
$response = json_encode([
|
||||
'meta' => [
|
||||
'status' => 200,
|
||||
'message' => 'OK',
|
||||
],
|
||||
'response' => [
|
||||
'foo' => 'bar',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->expectOutputString($response);
|
||||
|
||||
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
||||
$_REQUEST['request'] = 'v1/resource/1';
|
||||
|
||||
new Pickles\Router();
|
||||
}
|
||||
|
||||
public function testMisconfiguredAuth()
|
||||
{
|
||||
$response = json_encode([
|
||||
'meta' => [
|
||||
|
@ -129,9 +178,55 @@ namespace
|
|||
new Pickles\Router();
|
||||
}
|
||||
|
||||
public function testValidation()
|
||||
public function testMisconfiguredAuth()
|
||||
{
|
||||
$response = json_encode([
|
||||
'meta' => [
|
||||
'status' => 401,
|
||||
'message' => 'Authentication is not configured properly.',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->expectOutputString($response);
|
||||
|
||||
$_SERVER['REQUEST_METHOD'] = 'DELETE';
|
||||
$_REQUEST['request'] = 'v1/resource/1';
|
||||
|
||||
new Pickles\Router();
|
||||
}
|
||||
|
||||
public function testMethodNotAllowed()
|
||||
{
|
||||
$response = json_encode([
|
||||
'meta' => [
|
||||
'status' => 405,
|
||||
'message' => 'Method not allowed.',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->expectOutputString($response);
|
||||
|
||||
$_SERVER['REQUEST_METHOD'] = 'NOPE';
|
||||
$_REQUEST['request'] = 'v1/resource/1';
|
||||
|
||||
new Pickles\Router();
|
||||
}
|
||||
|
||||
public function testLowErrorCode()
|
||||
{
|
||||
$response = json_encode([
|
||||
'meta' => [
|
||||
'status' => 500,
|
||||
'message' => 'Error',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->expectOutputString($response);
|
||||
|
||||
$_SERVER['REQUEST_METHOD'] = 'ERROR';
|
||||
$_REQUEST['request'] = 'v1/resource/1';
|
||||
|
||||
new Pickles\Router();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue