Skip to content

Added exception and according testcase #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/ShortCode/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace ShortCode;

use ShortCode\Exception\CodeFormatTypeMismatchException;

/**
* Code
*
Expand Down Expand Up @@ -72,11 +74,18 @@ protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput
return $retval;
}

protected static function getTypeName($value)
{
$class = new \ReflectionClass(__CLASS__);
$constants = array_flip($class->getConstants());
/**
* @throws CodeFormatTypeMismatchException
*/
protected static function getTypeName($value)
{
$class = new \ReflectionClass(__CLASS__);
$constants = array_flip($class->getConstants());

return $constants[$value];
}
if (!isset($constants[$value])) {
throw new CodeFormatTypeMismatchException("Please, provide a valid code type");
}

return $constants[$value];
}
}
7 changes: 7 additions & 0 deletions src/ShortCode/Exception/CodeFormatTypeMismatchException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ShortCode\Exception;

class CodeFormatTypeMismatchException extends \Exception {

}
23 changes: 13 additions & 10 deletions src/ShortCode/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
*/
class Random extends Code
{

/**
* Get a random code of fixed length.
*
* @param int $length length of code, default 8
* @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM
*
* @return string
*/
/**
* Get a random code of fixed length.
*
* @param int $length length of code, default 8
* @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM
*
* @return string
* @throws Exception\CodeFormatTypeMismatchException
*/
public static function get($length = 8, $outputFormat = Code::FORMAT_ALNUM)
{
static::throwUnlessAcceptable($outputFormat, $length);
Expand All @@ -47,7 +47,10 @@ public static function get($length = 8, $outputFormat = Code::FORMAT_ALNUM)
return $output;
}

private static function throwUnlessAcceptable($type, $length)
/**
* @throws Exception\CodeFormatTypeMismatchException
*/
private static function throwUnlessAcceptable($type, $length)
{
if($length > 20) {
$typeName = self::getTypeName($type);
Expand Down
107 changes: 63 additions & 44 deletions tests/ShortCode/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace ShortCode;

use PHPUnit\Framework\TestCase;
use ShortCode\Exception\UnexpectedCodeLength;
use ShortCode\Exception\CodeFormatTypeMismatchException;

/**
* RandomTest
Expand All @@ -19,56 +21,73 @@
*/
class RandomTest extends TestCase
{
use TestHelper;
use TestHelper;

public function testRandomCodeGeneration()
{
$code = Random::get();
$matchWith = [];
/**
* @throws CodeFormatTypeMismatchException
*/
public function testRandomCodeGeneration()
{
$code = Random::get();
$matchWith = [];

for($i = 0; $i < 10; $i++) {
$matchWith[$i] = Random::get(8, Code::FORMAT_NUMBER);
}
for($i = 0; $i < 10; $i++) {
$matchWith[$i] = Random::get(8, Code::FORMAT_NUMBER);
}

$this->assertNotContains($code, $matchWith);
}
$this->assertNotContains($code, $matchWith);
}

/**
* @dataProvider formatProvider
*
* @param $format
* @param $formatName
*
* @throws CodeFormatTypeMismatchException
*/
public function testCodeGeneratedWithCorrectFormat($format, $formatName)
{
$code = Random::get(6, $format);
$length = strlen($code);
$this->assertSame(1, preg_match("/[$format]{". $length. '}/', $code), 'Trying with '. $formatName);
}

/**
* @dataProvider formatProvider
*
* @param $format
* @param $formatName
*/
public function testCodeGeneratedWithCorrectFormat($format, $formatName)
{
$code = Random::get(6, $format);
$length = strlen($code);
$this->assertSame(1, preg_match("/[$format]{". $length. '}/', $code), 'Trying with '. $formatName);
}
/**
* @dataProvider formatProvider
*
* @param $format
* @param $formatName
*
* @throws CodeFormatTypeMismatchException
*/
public function testCodeGeneratedWithCorrectLength($format, $formatName)
{
$code4 = Random::get(4, $format);
$code10 = Random::get(10, $format);
$code20 = Random::get(20, $format);

/**
* @dataProvider formatProvider
*
* @param $format
* @param $formatName
*/
public function testCodeGeneratedWithCorrectLength($format, $formatName)
{
$code4 = Random::get(4, $format);
$code10 = Random::get(10, $format);
$code20 = Random::get(20, $format);
$this->assertSame(4, strlen($code4), "Trying 4 char code with $formatName");
$this->assertSame(10, strlen($code10), "Trying 10 char code with $formatName");
$this->assertSame(20, strlen($code20), "Trying 20 char code with $formatName");
}

$this->assertSame(4, strlen($code4), "Trying 4 char code with $formatName");
$this->assertSame(10, strlen($code10), "Trying 10 char code with $formatName");
$this->assertSame(20, strlen($code20), "Trying 20 char code with $formatName");
}
/**
* @throws CodeFormatTypeMismatchException
*/
public function testExceptionIfAskedForMoreThen20Chars()
{
$this->expectException(UnexpectedCodeLength::class);
Random::get(25, Code::FORMAT_NUMBER);
}

/**
* @expectedException ShortCode\Exception\UnexpectedCodeLength
*/
public function testExceptionIfAskedForMoreThen20Chars()
{
Random::get(25, Code::FORMAT_NUMBER);
}
/**
* @throws CodeFormatTypeMismatchException
*/
public function testExceptionIfLengthMoreThan20CharsAndOutputFormatMismatch() {
$this->testExceptionIfAskedForMoreThen20Chars();

$this->expectException(CodeFormatTypeMismatchException::class);
Random::get(90, '!*');
}
}