From 37380156d03c5d17305a423ae68e9622a87bfbf2 Mon Sep 17 00:00:00 2001 From: sohag hossain Date: Thu, 28 Oct 2021 07:22:06 +0600 Subject: [PATCH] Added exception and according testcase --- src/ShortCode/Code.php | 21 +++- .../CodeFormatTypeMismatchException.php | 7 ++ src/ShortCode/Random.php | 23 ++-- tests/ShortCode/RandomTest.php | 107 +++++++++++------- 4 files changed, 98 insertions(+), 60 deletions(-) create mode 100644 src/ShortCode/Exception/CodeFormatTypeMismatchException.php diff --git a/src/ShortCode/Code.php b/src/ShortCode/Code.php index d533aff..5f457f7 100644 --- a/src/ShortCode/Code.php +++ b/src/ShortCode/Code.php @@ -10,6 +10,8 @@ namespace ShortCode; +use ShortCode\Exception\CodeFormatTypeMismatchException; + /** * Code * @@ -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]; + } } diff --git a/src/ShortCode/Exception/CodeFormatTypeMismatchException.php b/src/ShortCode/Exception/CodeFormatTypeMismatchException.php new file mode 100644 index 0000000..a08946d --- /dev/null +++ b/src/ShortCode/Exception/CodeFormatTypeMismatchException.php @@ -0,0 +1,7 @@ + 20) { $typeName = self::getTypeName($type); diff --git a/tests/ShortCode/RandomTest.php b/tests/ShortCode/RandomTest.php index fc0d75e..bb383a0 100644 --- a/tests/ShortCode/RandomTest.php +++ b/tests/ShortCode/RandomTest.php @@ -11,6 +11,8 @@ namespace ShortCode; use PHPUnit\Framework\TestCase; +use ShortCode\Exception\UnexpectedCodeLength; +use ShortCode\Exception\CodeFormatTypeMismatchException; /** * RandomTest @@ -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, '!*'); + } } \ No newline at end of file