Skip to content

Commit

Permalink
Merge pull request #11 from Surfoo/develop
Browse files Browse the repository at this point in the history
Throw an exception if the referenceCode is too short.
  • Loading branch information
Surfoo authored Jan 19, 2021
2 parents 7249416 + 0b61db2 commit 28f5881
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ composer.lock
cache
vendor
.php_cs.cache
.phpunit.result.cache
sami.phar
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: php
php:
- 7.3
- 7.4
- 8.0

env:
# - dependencies=--prefer-lowest
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.14.0] - 2021-01-19
### Added
- Throw an exception if the referenceCode is too short

## [3.13.0] - 2021-01-17
### Added
- New API method added:
Expand Down
16 changes: 8 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<phpunit bootstrap="vendor/autoload.php">
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="units">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
6 changes: 5 additions & 1 deletion src/Lib/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ public static function decimalToDegreeDecimal(float $latitude, float $longitude)
}

/**
* Concert a reference code to an Id
* Convert a reference code to an Id
*/
public static function referenceCodeToId(string $referenceCode): int
{
if (\strlen($referenceCode) <= 2) {
throw new UtilsException('referenceCode "' . $referenceCode . '" too short.');
}

if (\substr($referenceCode, 0, 2) == 'GC') {
$referenceCode = \str_replace('SO', '50', $referenceCode);
}
Expand Down
25 changes: 7 additions & 18 deletions tests/Lib/Utils/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Geocaching\Lib\Utils\Utils;
use PHPUnit\Framework\TestCase;

final class UtilsTest extends TestCase
{

public function providerDecimalToDegreeDecimal()
{
return [
Expand All @@ -20,10 +20,6 @@ public function providerDecimalToDegreeDecimal()

/**
* @dataProvider providerDecimalToDegreeDecimal
*
* @param float $latitude
* @param float $longitude
* @param string $expected
*/
public function testDecimalToDegreeDecimal(float $latitude, float $longitude, string $expected)
{
Expand All @@ -46,9 +42,6 @@ public function providerReferenceCode()

/**
* @dataProvider providerReferenceCode
*
* @param string $referenceCode
* @param integer $expected
*/
public function testReferenceCodeToIdSuccessful(string $referenceCode, int $expected): void
{
Expand All @@ -61,17 +54,18 @@ public function testReferenceCodeToIdSuccessful(string $referenceCode, int $expe
public function providerReferenceCodeFail()
{
return [
['GCSRTOP']
['GC', 'referenceCode "GC" too short.'],
['GCSRTOP', 'Only chars 0123456789ABCDEFGHJKMNPQRTVWXYZ are supported.'],
];
}

/**
* @dataProvider providerReferenceCodeFail
*/
public function testReferenceCodeToIdFail(string $referenceCode)
public function testReferenceCodeToIdFail(string $referenceCode, string $expected)
{
$this->expectException(Geocaching\Exception\UtilsException::class);
$this->expectExceptionMessage("Only chars 0123456789ABCDEFGHJKMNPQRTVWXYZ are supported.");
$this->expectExceptionMessage($expected);

Utils::referenceCodeToId($referenceCode);
}
Expand All @@ -90,10 +84,6 @@ public function providerId()

/**
* @dataProvider providerId
*
* @param string $number
* @param string $prefix
* @param string $expected
*/
public function testIdToReferenceCodeSucessful(string $number, string $prefix, string $expected): void
{
Expand All @@ -102,5 +92,4 @@ public function testIdToReferenceCodeSucessful(string $number, string $prefix, s
$this->assertEquals($expected, $result);
$this->assertIsString($result);
}

}
}

0 comments on commit 28f5881

Please sign in to comment.