Working on tests for Resource

This commit is contained in:
Josh Sherman 2014-09-28 21:49:49 -04:00
parent 6c173cdc89
commit aea1bae3bf
2 changed files with 197 additions and 121 deletions

View file

@ -126,19 +126,19 @@ class Resource extends Object
*/ */
} }
// Hack together some new globals
if (in_array($method, ['PUT', 'DELETE']))
{
$GLOBALS['_' . $method] = [];
// @todo Populate it
}
$filter = isset($this->filter[$method]); $filter = isset($this->filter[$method]);
$validate = isset($this->validate[$method]); $validate = isset($this->validate[$method]);
if ($filter || $validate) if ($filter || $validate)
{ {
// Hack together some new globals
if (in_array($method, ['PUT', 'DELETE']))
{
$GLOBALS['_' . $method] = [];
// @todo Populate it
}
$global =& $GLOBALS['_' . $method]; $global =& $GLOBALS['_' . $method];
// Checks that the required parameters are present // Checks that the required parameters are present
@ -199,16 +199,19 @@ class Resource extends Object
{ {
$rule = explode(':', $rule); $rule = explode(':', $rule);
for ($i = 1; $i <= 2; $i++)
{
if (!isset($rule[$i]))
{
$rule[$i] = false;
}
}
switch ($rule[0]) switch ($rule[0])
{ {
// {{{ Checks using filter_var() // {{{ Checks using filter_var()
case 'filter': case 'filter':
if (!isset($rule[1]))
{
$rule[1] = false;
}
switch ($rule[1]) switch ($rule[1])
{ {
case 'boolean': case 'boolean':
@ -218,74 +221,61 @@ class Resource extends Object
case 'ip': case 'ip':
case 'url': case 'url':
$filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1])); $filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1]));
if (!filter_var($value, $filter))
{
$this->errors[$variable][] = $message;
}
break; break;
default: default:
throw new \Exception('Invalid filter, expecting boolean, email, float, int, ip or url.'); $this->errors[$variable] = 'Invalid filter, expecting boolean, email, float, int, ip or url.';
break; break;
} }
if (!filter_var($value, $filter))
{
$this->errors[$variable][] = $message;
}
break; break;
// }}} // }}}
// {{{ Checks using strlen() // {{{ Checks using strlen()
case 'length': case 'length':
if (count($rule) < 3) $length = strlen($value);
switch ($rule[1])
{ {
throw new \Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".'); case '<':
$valid = $length < $rule[2];
break;
case '<=':
$valid = $length <= $rule[2];
break;
case '==':
$valid = $length == $rule[2];
break;
case '!=':
$valid = $length != $rule[2];
break;
case '>=':
$valid = $length >= $rule[2];
break;
case '>':
$valid = $length > $rule[2];
break;
default:
$valid = false;
$message = 'Invalid operator, expecting <, <=, ==, !=, >= or >.';
break;
} }
else
if (!$valid)
{ {
if (!filter_var($rule[2], FILTER_VALIDATE_INT)) $this->errors[$variable][] = $message;
{
throw new \Exception('Invalid length value, expecting an integer.');
}
else
{
$length = strlen($value);
switch ($rule[1])
{
case '<':
$valid = $length < $rule[2];
break;
case '<=':
$valid = $length <= $rule[2];
break;
case '==':
$valid = $length == $rule[2];
break;
case '!=':
$valid = $length != $rule[2];
break;
case '>=':
$valid = $length >= $rule[2];
break;
case '>':
$valid = $length > $rule[2];
break;
default:
throw new \Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.');
break;
}
if (!$valid)
{
$this->errors[$variable][] = $message;
}
}
} }
break; break;
@ -294,19 +284,9 @@ class Resource extends Object
// {{{ Checks using preg_match() // {{{ Checks using preg_match()
case 'regex': case 'regex':
if (count($rule) < 3) if (preg_match($rule[1], $value))
{ {
throw new \Exception('Invalid validation rule, expected: "regex:is|not:string".'); $this->errors[$variable][] = $message;
}
else
{
$rule[1] = strtolower($rule[1]);
if (($rule[1] == 'is' && preg_match($rule[2], $value))
|| ($rule[1] == 'not' && !preg_match($rule[2], $value)))
{
$this->errors[$variable][] = $message;
}
} }
break; break;
@ -346,25 +326,26 @@ class Resource extends Object
} }
else else
{ {
/*
// Gets the profiler status // Gets the profiler status
// @todo Refactor out that stripos $profiler = $this->config['pickles']['profiler'];
$profiler = $this->config->pickles['profiler'];
$profiler = $profiler === true
|| stripos($profiler, 'timers') !== false;
// Starts a timer before the resource is executed // Starts a timer before the resource is executed
if ($profiler) if ($profiler)
{ {
Profiler::timer('resource ' . $method); Profiler::timer('resource ' . $method);
} }
*/
$this->response = $this->$method(); $this->response = $this->$method();
/*
// Stops the resource timer // Stops the resource timer
if ($profiler) if ($profiler)
{ {
Profiler::timer('resource ' . $method); Profiler::timer('resource ' . $method);
} }
*/
} }
} }
} }

View file

@ -22,26 +22,44 @@ namespace Resources\v1
public $validate = [ public $validate = [
'GET' => [ 'GET' => [
'missing', 'missing',
'isBoolean' => ['filter:boolean' => 'Error'], 'isBoolean' => ['filter:boolean' => 'Error'],
'isNotBoolean' => ['filter:boolean' => 'Error'], 'isNotBoolean' => ['filter:boolean' => 'Error'],
'isEmail' => ['filter:email' => 'Error'], 'isEmail' => ['filter:email' => 'Error'],
'isNotEmail' => ['filter:email' => 'Error'], 'isNotEmail' => ['filter:email' => 'Error'],
'isFloat' => ['filter:float' => 'Error'], 'isFloat' => ['filter:float' => 'Error'],
'isNotFloat' => ['filter:float' => 'Error'], 'isNotFloat' => ['filter:float' => 'Error'],
'isInt' => ['filter:int' => 'Error'], 'isInt' => ['filter:int' => 'Error'],
'isNotInt' => ['filter:int' => 'Error'], 'isNotInt' => ['filter:int' => 'Error'],
'isIP' => ['filter:ip' => 'Error'], 'isIP' => ['filter:ip' => 'Error'],
'isNotIP' => ['filter:ip' => 'Error'], 'isNotIP' => ['filter:ip' => 'Error'],
'isURL' => ['filter:url' => 'Error'], 'isURL' => ['filter:url' => 'Error'],
'isNotURL' => ['filter:url' => 'Error'], 'isNotURL' => ['filter:url' => 'Error'],
'invalidRule' => ['filter' => '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 GET()
{
}
public function PUT()
{ {
return ['foo' => 'bar']; return ['foo' => 'bar'];
} }
public function ERROR()
{
throw new \Exception('Error');
}
} }
} }
@ -53,16 +71,19 @@ namespace
{ {
$response = json_encode([ $response = json_encode([
'meta' => [ 'meta' => [
'status' => 500, 'status' => 400,
'message' => 'Invalid filter, expecting boolean, email, float, int, ip or url.', 'message' => 'Missing or invalid parameters.',
'errors' => [ 'errors' => [
'missing' => ['The missing parameter is required.'], 'missing' => ['The missing parameter is required.'],
'isNotBoolean' => ['Error'], 'isNotBoolean' => ['Error'],
'isNotEmail' => ['Error'], 'isNotEmail' => ['Error'],
'isNotFloat' => ['Error'], 'isNotFloat' => ['Error'],
'isNotInt' => ['Error'], 'isNotInt' => ['Error'],
'isNotIP' => ['Error'], 'isNotIP' => ['Error'],
'isNotURL' => ['Error'], 'isNotURL' => ['Error'],
'invalidRule' => ['Invalid filter, expecting boolean, email, float, int, ip or url.'],
'greaterLessThan' => ['Invalid operator, expecting <, <=, ==, !=, >= or >.'],
'regex' => ['Error'],
], ],
], ],
]); ]);
@ -72,21 +93,29 @@ namespace
$_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_METHOD'] = 'GET';
$_REQUEST['request'] = 'v1/resource/1'; $_REQUEST['request'] = 'v1/resource/1';
$_GET = [ $_GET = [
'foo' => ' bar ', 'foo' => ' bar ',
'bar' => 'unencrypted', 'bar' => 'unencrypted',
'isBoolean' => true, 'isBoolean' => true,
'isNotBoolean' => 'invalid', 'isNotBoolean' => 'invalid',
'isEmail' => 'foo@bar.com', 'isEmail' => 'foo@bar.com',
'isNotEmail' => 'nope', 'isNotEmail' => 'nope',
'isFloat' => 1.234567890, 'isFloat' => 1.234567890,
'isNotFloat' => 'five', 'isNotFloat' => 'five',
'isInt' => 22381, 'isInt' => 22381,
'isNotInt' => 'pretzel', 'isNotInt' => 'pretzel',
'isIP' => '127.0.0.1', 'isIP' => '127.0.0.1',
'isNotIP' => 'home', 'isNotIP' => 'home',
'isURL' => 'http://joshtronic.com', 'isURL' => 'http://joshtronic.com',
'isNotURL' => 'doubleUdoubleUdoubleUdot', 'isNotURL' => 'doubleUdoubleUdoubleUdot',
'invalidRule' => 'invalid', 'invalidRule' => 'invalid',
'lessThan' => '...',
'lessThanEqual' => '.......',
'equal' => '..........',
'notEqual' => '.......',
'greaterThan' => '............',
'greaterThanEqual' => '............',
'greaterLessThan' => '......',
'regex' => 'abc',
]; ];
new Pickles\Router(); new Pickles\Router();
@ -112,7 +141,27 @@ namespace
new Pickles\Router(); 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([ $response = json_encode([
'meta' => [ 'meta' => [
@ -129,9 +178,55 @@ namespace
new Pickles\Router(); 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();
} }
} }
} }