Skip to content

Commit

Permalink
unit tests for ServerSideValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweissman committed Jun 12, 2017
1 parent 46e7cd2 commit 534d5d7
Show file tree
Hide file tree
Showing 4 changed files with 871 additions and 35 deletions.
57 changes: 32 additions & 25 deletions Fortress/ServerSideValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/fortress
* @copyright Copyright (c) 2013-2017 Alexander Weissman
* @license https://github.com/userfrosting/fortress/blob/master/licenses/UserFrosting.md (MIT License)
*/
namespace UserFrosting\Fortress;

use \Valitron\Validator;
use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface;
use UserFrosting\I18n\MessageTranslator;
use Valitron\Validator;

/**
* ServerSideValidator Class
*
* Loads validation rules from a schema and validates a target array of data.
*
* @author Alexander Weissman
* @link https://alexanderweissman.com
* @author Alexander Weissman (https://alexanderweissman.com)
*/
class ServerSideValidator extends Validator implements ServerSideValidatorInterface
{
/**
* @var RequestSchema
* @var RequestSchemaInterface
*/
protected $schema;

/**
* @var MessageTranslatorInterface
* @var MessageTranslator
*/
protected $translator;

/** Create a new server-side validator.
*
* @param RequestSchema $schema A RequestSchema object, containing the validation rules.
* @param RequestSchemaInterface $schema A RequestSchemaInterface object, containing the validation rules.
* @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema.
*/
public function __construct($schema, $translator)
public function __construct(RequestSchemaInterface $schema, MessageTranslator $translator)
{
// Set schema
$this->setSchema($schema);
Expand All @@ -49,38 +49,41 @@ public function __construct($schema, $translator)
}

/**
* Set the schema for this validator, as a valid RequestSchema object.
*
* @param RequestSchema $schema A RequestSchema object, containing the validation rules.
* {@inheritDoc}
*/
public function setSchema($schema)
public function setSchema(RequestSchemaInterface $schema)
{
$this->schema = $schema;
}

/**
* Set the translator for this validator, as a valid MessageTranslator object.
*
* @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema.
* {@inheritDoc}
*/
public function setTranslator($translator)
public function setTranslator(MessageTranslator $translator)
{
$this->translator = $translator;
}

/**
* Validate the specified data against the schema rules.
*
* @param array $data An array of data, mapping field names to field values.
* @return boolean True if the data was successfully validated, false otherwise.
* {@inheritDoc}
*/
public function validate($data = [])
public function validate(array $data = [])
{
$this->_fields = $data; // Setting the parent class Validator's field data.
$this->generateSchemaRules(); // Build Validator rules from the schema.
return parent::validate(); // Validate!
}

/**
* {@inheritDoc}
*
* We expose this method to the public interface for testing purposes.
*/
public function hasRule($name, $field)
{
return parent::hasRule($name, $field);
}

/**
* Validate that a field has a particular value.
*
Expand All @@ -90,12 +93,16 @@ public function validate($data = [])
* @param bool $caseSensitive
* @return bool
*/
protected function validateEqualsValue($field, $value, $targetValue, $caseSensitive = false)
protected function validateEqualsValue($field, $value, $params)
{
$targetValue = $params[0];
$caseSensitive = is_bool($params[1]) ? $params[1] : false;

if (!$caseSensitive) {
$value = strtolower($value);
$targetValue = strtolower($targetValue);
}

return $value == $targetValue;
}

Expand All @@ -108,9 +115,9 @@ protected function validateEqualsValue($field, $value, $targetValue, $caseSensit
* @param bool $caseSensitive
* @return bool
*/
protected function validateNotEqualsValue($field, $value, $targetValue, $caseSensitive = false)
protected function validateNotEqualsValue($field, $value, $params)
{
return !$this->validateEqualsValue($field, $value, $targetValue, $caseSensitive);
return !$this->validateEqualsValue($field, $value, $params);
}

/**
Expand Down Expand Up @@ -176,7 +183,7 @@ private function ruleWithMessage($rule, $messageSet)
*/
private function generateSchemaRules()
{
foreach ($this->schema->getSchema() as $fieldName => $field) {
foreach ($this->schema->all() as $fieldName => $field) {
if (!isset($field['validators'])) {
continue;
}
Expand Down
31 changes: 21 additions & 10 deletions Fortress/ServerSideValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,56 @@
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/fortress
* @copyright Copyright (c) 2013-2017 Alexander Weissman
* @license https://github.com/userfrosting/fortress/blob/master/licenses/UserFrosting.md (MIT License)
*/

namespace UserFrosting\Fortress;

use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface;
use UserFrosting\I18n\MessageTranslator;

/**
* ServerSideValidator Interface
*
* Loads validation rules from a schema and validates a target array of data.
*
* @author Alexander Weissman
* @link https://alexanderweissman.com
* @author Alexander Weissman (https://alexanderweissman.com)
*/
interface ServerSideValidatorInterface
{
/**
* Set the schema for this validator, as a valid RequestSchema object.
* Set the schema for this validator, as a valid RequestSchemaInterface object.
*
* @param RequestSchema $schema A RequestSchema object, containing the validation rules.
* @param RequestSchemaInterface $schema A RequestSchemaInterface object, containing the validation rules.
*/
public function setSchema($schema);
public function setSchema(RequestSchemaInterface $schema);

/**
* Set the translator for this validator, as a valid MessageTranslator object.
*
* @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema.
*/
public function setTranslator($schema);
public function setTranslator(MessageTranslator $translator);

/**
* Validate the specified data against the schema rules.
*
* @param array $data An array of data, mapping field names to field values.
* @return boolean True if the data was successfully validated, false otherwise.
*/
public function validate($data);
public function validate(array $data);

/**
* Get array of fields and data
*
* @return array
*/
public function data();

/**
* Get array of error messages
*
* @param null|string $field
* @return array|bool
*/
public function errors();
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"require": {
"ezyang/htmlpurifier": "4.7.0",
"php": ">=5.6.0",
"userfrosting/i18n": "dev-develop",
"userfrosting/support": "dev-develop",
"vlucas/valitron": "1.2.3"
},
Expand Down
Loading

0 comments on commit 534d5d7

Please sign in to comment.