Skip to content

Commit

Permalink
Merge pull request #6 from adambullmer/testing
Browse files Browse the repository at this point in the history
Added Unit Testing
  • Loading branch information
adambullmer committed Apr 29, 2016
2 parents 7ad171d + 20ebb19 commit 1600582
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 22 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
machine:
php:
version: 7.0.3

dependencies:
override:
- composer install

test:
override:
- ./vendor/bin/phpunit --coverage-text
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "5.3.*"
}
}
4 changes: 4 additions & 0 deletions php/RuleFormatException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace USDLRegex;

class RuleFormatException extends \Exception {}
4 changes: 4 additions & 0 deletions php/StateNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace USDLRegex;

class StateNotFoundException extends \Exception {}
119 changes: 97 additions & 22 deletions php/Validator.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php
namespace USDLRegex;

$string = file_get_contents(__DIR__.'/../regex.json');
$rules = json_decode($string, TRUE);

/**
* Validation class.
*/
class Validator {
/**
* Map of states and their rules
* @var array
*/
public $rules = array();

/**
* Flag for adding verbosity to output
* @var boolean
Expand All @@ -22,12 +25,14 @@ class Validator {

function __construct($options = array()) {
$defaults = array(
'rules' => json_decode(file_get_contents(__DIR__.'/../regex.json'), TRUE),
'verbose' => FALSE,
'caseInsensitive' => FALSE,
);

$options = array_merge($defaults, $options);

$this->rules = $options['rules'];
$this->verbose = $options['verbose'];
$this->caseInsensitive = $options['caseInsensitive'];
}
Expand All @@ -37,36 +42,106 @@ function __construct($options = array()) {
* in the provided string.
*
* @param String $state 2 letter abbreviation of the state
* @param String $dl_string string representatino of the driver license number
* @param String $dlString string representatino of the driver license number
*
* @return Bool TRUE/FALSE if the number is valid
*/
public function validate($state, $dl_string) {
global $rules;

$state = strtoupper($state);
$rule = $rules[$state]['rule'];
$regexFlags = '';
public function validate($state, $dlString) {
$rule = $this->__getRule($state);
$regexFlags = $this->__getValidatorFlags();

if ($this->verbose === TRUE) {
echo "Validating $dl_string using rule $rule\n";
echo "Validating $dlString using rule $rule\n";
}

if ($this->caseInsensitive === TRUE) {
$regexFlags .= 'i';
}
$isValid = (bool)preg_match("/$rule/$regexFlags", $dlString);

if (!preg_match("/$rule/$regexFlags", $dl_string)) {
if ($this->verbose === TRUE) {
echo "Didn't match against the following criteria:\n";
foreach ($rules[$state]['description'] as $description) {
echo " $description \n";
}
if ($isValid === FALSE && $this->verbose === TRUE) {
echo "Didn't match against the following criteria:\n";
foreach ($this->__getDescriptions($state) as $description) {
echo " $description\n";
}
}

return $isValid;
}

/**
* Finds and returns the registered state definition.
*
* @param String $key State to lookup rules for
*
* @return Array Array of Rule and Descriptions
*
* @throws USDLRegex\ValueException No State key was provided
* @throws USDLRegex\StateNotFoundException Passed state doesn't exist in key
*/
private function __getState($key=NULL) {
if ($key === NULL) {
throw new ValueException('Must provide a key to get a rule');
}

$key = strtoupper($key);

return FALSE;
if (!array_key_exists($key, $this->rules)) {
throw new StateNotFoundException("State '$key' not found in rule set");
}

return $this->rules[$key];
}

/**
* Finds and returns the validation rule for the state.
*
* @param String $key State to lookup rules for
*
* @return String String representation of regex
*
* @throws USDLRegex\RuleFormatException 'rule' key doesn't exist for the passed
* state.
*/
private function __getRule($key=NULL) {
$state = $this->__getState($key);

if (!array_key_exists('rule', $state)) {
throw new RuleFormatException("Malformed rules JSON: State '$key' does not have a 'rule' key.");
}

return $state['rule'];
}

/**
* Finds and returns the list of friendly descriptions for the state rule regex.
*
* @param String $key State to lookup rules for
*
* @return Array Array of strings with friendly regex rules
*
* @throws USDLRegex\RuleFormatException 'description' key doesn't exist for the
* passed state.
*/
private function __getDescriptions($key=NULL) {
$state = $this->__getState($key);

if (!array_key_exists('description', $state)) {
throw new RuleFormatException("Malformed rules JSON: State '$key' does not have a 'description' key.");
}

return $state['description'];
}

/**
* Compiles a list of regex directives to be used while validating.
*
* @return String string of regex directives
*/
private function __getValidatorFlags() {
$validatorFlags = '';

if ($this->caseInsensitive === TRUE) {
$validatorFlags .= 'i';
}

return TRUE;
return $validatorFlags;
}
}
4 changes: 4 additions & 0 deletions php/ValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace USDLRegex;

class ValueException extends \Exception {}
13 changes: 13 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./test/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./php</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit 1600582

Please sign in to comment.