Skip to content

Commit

Permalink
Add custom exception for invalid postcodes, fix to tokenizer, add uni…
Browse files Browse the repository at this point in the history
…t tests
  • Loading branch information
Jordan Hall committed Mar 9, 2018
1 parent afb0c8e commit 406cae4
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage_clover: tests/Logs/clover.xml
json_path: tests/Logs/coveralls-upload.json
service_name: travis-ci
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php
dist: trusty
php:
- '5.6'
- '7.0'
- '7.1'
- 'hhvm'
install:
- composer update
script:
- ./vendor/bin/phpunit --coverage-clover ./tests/Logs/clover.xml
after_script:
- php vendor/bin/php-coveralls -v
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"guzzlehttp/guzzle": "~6.0",
"fzaninotto/faker": "^1.6"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"fzaninotto/faker": "^1.6",
"satooshi/php-coveralls": "^2.0"
},
"autoload": {
"psr-4": {
"RapidWeb\\Postcodes\\": "src/"
Expand Down
28 changes: 28 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./tests/Integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">src/Examples</directory>
</exclude>
</whitelist>
</filter>
<php>

</php>
</phpunit>
6 changes: 6 additions & 0 deletions src/Exceptions/InvalidPostcodeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace RapidWeb\Postcodes\Exceptions;

class InvalidPostcodeException extends \Exception
{
}
5 changes: 3 additions & 2 deletions src/Utils/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use RapidWeb\Postcodes\Utils\Validator;
use Exception;
use RapidWeb\Postcodes\Exceptions\InvalidPostcodeException;

abstract class Tokenizer
{
Expand All @@ -24,12 +25,12 @@ public static function inward($postcode)
return $postcodeEnd;
}

private function sanityCheck($postcode)
private static function sanityCheck($postcode)
{
$validated = Validator::validatePostcode($postcode);
if(!$validated)
{
throw new Exception("Post code provided is not valid");
throw new InvalidPostcodeException("Post code provided is not valid");
}

}
Expand Down
96 changes: 96 additions & 0 deletions tests/Unit/BasicUsageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

use PHPUnit\Framework\TestCase;
use RapidWeb\Postcodes\Utils\Validator;
use RapidWeb\Postcodes\Utils\Generator;
use RapidWeb\Postcodes\Utils\Tokenizer;
use RapidWeb\Postcodes\Exceptions\InvalidPostcodeException;

final class BasicUsageTest extends TestCase
{
public function testValidation()
{
$postcodes = ['ST163DP', 'TN30YA', 'ST78PP', 'CM233WE', 'E16AW', 'E106QX'];

foreach($postcodes as $postcode) {
$this->assertTrue(Validator::validatePostcode($postcode));
}
}

public function testValidationFailure()
{
$postcodes = ['ST163DPA', 'XF2P90', 'Ollie', 'cake'];

foreach($postcodes as $postcode) {
$this->assertFalse(Validator::validatePostcode($postcode));
}
}

public function testGeneration()
{
$postcodes = [];

for ($i=0; $i < 100; $i++) {
$postcodes[] = Generator::generatePostcode();
}

foreach($postcodes as $postcode) {
$this->assertTrue(Validator::validatePostcode($postcode));
}
}

public function testOutwardAndInwardCodes()
{
$postcodeTestItems = [
[
'postcode' => 'ST163DP',
'outward' => 'ST16',
'inward' => '3DP'
],
[
'postcode' => 'TN30YA',
'outward' => 'TN3',
'inward' => '0YA'
],
[
'postcode' => 'ST78PP',
'outward' => 'ST7',
'inward' => '8PP'
],
[
'postcode' => 'CM233WE',
'outward' => 'CM23',
'inward' => '3WE'
],
[
'postcode' => 'E16AW',
'outward' => 'E1',
'inward' => '6AW'
],
[
'postcode' => 'E106QX',
'outward' => 'E10',
'inward' => '6QX'
]
];

foreach($postcodeTestItems as $postcodeTestItem) {
$this->assertEquals($postcodeTestItem['outward'], Tokenizer::outward($postcodeTestItem['postcode']));
$this->assertEquals($postcodeTestItem['inward'], Tokenizer::inward($postcodeTestItem['postcode']));
}
}

public function testOutwardCodeWithInvalidPostcode()
{
$this->expectException(InvalidPostcodeException::class);

$outward = Tokenizer::outward('ST163DPA');
}

public function testInwardCodeWithInvalidPostcode()
{
$this->expectException(InvalidPostcodeException::class);

$outward = Tokenizer::inward('ST163DPA');
}
}

0 comments on commit 406cae4

Please sign in to comment.