Skip to content

Commit

Permalink
adding new validation method, tests, and documentation.
Browse files Browse the repository at this point in the history
updating readme to reflect new methods and abilities for functions.

documenting new validation method.

wrote tests
  • Loading branch information
Tim committed Jul 14, 2020
1 parent dbd006b commit 9e28206
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 25 deletions.
59 changes: 34 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ The code thrown is `Exception::missingData`
### ->validateSubDocument( $name )
You can validate a sub document using this method. This will throw an exception if the sub document does not exist as a document class object. If it does, ->validate is called on the property which may throw other exceptions.

The code thrown is `Exception::missingData` if the property is not set or `Exception::missingData`
The code thrown is `Exception::missingData` if the property is not set or `Exception::wrongType` if the property is not set as a document subclass.

### ->validateValueInList( string $name, string $value, array $list )
You can validate a value to be in a list using this method.
Expand All @@ -67,15 +67,23 @@ The code thrown is `Exception::disallowedValue`

The suggestion is made to implement a setter function for any values you want to set using this validation function. Before you set the value on the document class you would call this validation function. This will protect your document object from ever having wrong values set on it.

### ->validateNotNull( string ...$names )
Validates that a property exists and is not equal to null. `=== null`

The code thrown is `Exception::noSuchProperty` if property does not exist on class.

The code thrown is `Exception::missingData` if the property === null.

## Example
```
```php
<?PHP namespace application\libraries\documents;

class customer extends \treehousetim\document\document
{
protected $first_name;
protected $last_name;
protected $address_line_1;
protected $address_line_2;
protected $city;
protected $state_province;
protected $postal_code;
Expand All @@ -84,35 +92,36 @@ class customer extends \treehousetim\document\document
public function jsonSerialize ()
{
$this->validate();
$out = [];
$out ['first_name'] = $this->first_name;
$out ['last_name'] = $this->last_name;
$out ['city'] = $this->city;
$out ['state_province'] = $this->state_province;
$out ['postal_code'] = $this->postal_code;
$out ['email'] = $this->email;
$out ['address_line_1'] = $this->address_line_1;
return $out;
return $this->getFieldArray(
'first_name',
'last_name',
'city',
'state_province',
'postal_code',
'email',
'address_line_1',
'address_line_2'
);
}
//------------------------------------------------------------------------
protected function validate()
{
$this->validateRequired( 'first_name' );
$this->validateRequired( 'last_name' );
$this->validateRequired( 'city' );
$this->validateRequired( 'state_province' );
$this->validateRequired( 'postal_code' );
$this->validateRequired( 'address_line_1' );
$this->validateRequired(
'first_name',
'last_name',
'city',
'state_province',
'postal_code',
'address_line_1'
);

$this->validateNotNull( 'address_line_2' );
}
}
```

## Testing the codebase
If you have cloned this repo, you can run the tests.

Run composer install to install PHPUnit and Monolog.
## Testing the code base
If you have cloned this repository, you can run the tests.

1. `composer install`
2. `./vendor/bin/phpunit`
1. Install test dependencies: `composer install`
2. Run tests: `composer test`
11 changes: 11 additions & 0 deletions src/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ protected function validateRequired( string ...$names )
}
}
//------------------------------------------------------------------------
protected function validateNotNull( string ...$names )
{
foreach( $names as $name )
{
if( $this->{$name} === null )
{
throw new Exception( $name . ':: missing', Exception::missingData );
}
}
}
//------------------------------------------------------------------------
protected function validateSubDocument( string $name )
{
$this->validateRequired( $name );
Expand Down
10 changes: 10 additions & 0 deletions tests/nameDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ protected function validate()
$this->validateRequired( 'last_name' );
$this->validateRequired( 'full_name' );
}
//------------------------------------------------------------------------
public function doValidateNotNull()
{
$this->validateNotNull( 'first_name', 'last_name' );
}
//------------------------------------------------------------------------
public function doValidateNotNullNonField()
{
$this->validateNotNull( 'fizbuz' );
}
}
28 changes: 28 additions & 0 deletions tests/validationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ public function testValidateValueInListSuccess()
$doc->suffix( nameDocument::sufMD );
$this->assertEquals( $doc->suffix, nameDocument::sufMD );
}
//------------------------------------------------------------------------
public function testNotNull()
{
$this->expectException( documentException::class );
$this->expectExceptionCode( documentException::missingData );

$doc = new \treehousetim\document\test\nameDocument();
$doc->doValidateNotNull();
}
//------------------------------------------------------------------------
public function testNotNullEquals()
{
$doc = new \treehousetim\document\test\nameDocument();
$doc
->first_name( '' )
->last_name( '' );
$doc->doValidateNotNull();

$this->assertEquals( $doc->first_name, '' );
$this->assertEquals( $doc->last_name, '' );
}
//------------------------------------------------------------------------
public function testNotNullNonField()
{
$this->expectException( documentException::class );
$this->expectExceptionCode( documentException::noSuchProperty );

$doc = new \treehousetim\document\test\nameDocument();
$doc->doValidateNotNullNonField();
}
}

0 comments on commit 9e28206

Please sign in to comment.