diff --git a/tests/Meritoo/Common/Test/Exception/Base/UnknownTypeExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Base/UnknownTypeExceptionTest.php index 815e0c6..a273299 100644 --- a/tests/Meritoo/Common/Test/Exception/Base/UnknownTypeExceptionTest.php +++ b/tests/Meritoo/Common/Test/Exception/Base/UnknownTypeExceptionTest.php @@ -9,8 +9,9 @@ namespace Meritoo\Common\Test\Exception\Base; use Meritoo\Common\Exception\Base\UnknownTypeException; +use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Type\Base\BaseType; -use PHPUnit\Framework\TestCase; +use Meritoo\Common\Type\OopVisibilityType; /** * Test case of the exception used while type of something is unknown @@ -18,8 +19,13 @@ use PHPUnit\Framework\TestCase; * @author Krzysztof Niziol * @copyright Meritoo.pl */ -class UnknownTypeExceptionTest extends TestCase +class UnknownTypeExceptionTest extends BaseTestCase { + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(UnknownTestTypeException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + public function testWithoutException() { self::assertEquals('Test 2', (new TestService())->getTranslatedType('test_2')); diff --git a/tests/Meritoo/Common/Test/Exception/Date/UnknownDatePartTypeExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Date/UnknownDatePartTypeExceptionTest.php new file mode 100644 index 0000000..a318406 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Date/UnknownDatePartTypeExceptionTest.php @@ -0,0 +1,71 @@ + + * @copyright Meritoo.pl + */ +class UnknownDatePartTypeExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(UnknownDatePartTypeException::class, OopVisibilityType::IS_PUBLIC, 2, 2); + } + + /** + * @param string $unknownDatePart Type of date part, e.g. "year". One of DatePartType class constants. + * @param string $value Incorrect value + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideDatePartAndValue + */ + public function testConstructorMessage($unknownDatePart, $value, $expectedMessage) + { + $exception = new UnknownDatePartTypeException($unknownDatePart, $value); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides type of date part, incorrect value and expected exception's message + * + * @return Generator + */ + public function provideDatePartAndValue() + { + $template = 'The \'%s\' type of date part (with value %s) is unknown. Probably doesn\'t exist or there is a' + . ' typo. You should use one of these types: day, hour, minute, month, second, year.'; + + yield[ + DatePartType::DAY, + '44', + sprintf($template, DatePartType::DAY, '44'), + ]; + + yield[ + DatePartType::MONTH, + '22', + sprintf($template, DatePartType::MONTH, '22'), + ]; + + yield[ + DatePartType::MINUTE, + '77', + sprintf($template, DatePartType::MINUTE, '77'), + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/File/EmptyFileExceptionTest.php b/tests/Meritoo/Common/Test/Exception/File/EmptyFileExceptionTest.php new file mode 100644 index 0000000..506592e --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/File/EmptyFileExceptionTest.php @@ -0,0 +1,60 @@ + + * @copyright Meritoo.pl + */ +class EmptyFileExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(EmptyFileException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param string $emptyFilePath Path of the empty file + * @param string $expectedMessage Expected exception's message + * + * @dataProvider providePathOfFile + */ + public function testConstructorMessage($emptyFilePath, $expectedMessage) + { + $exception = new EmptyFileException($emptyFilePath); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides path of the empty file and expected exception's message + * + * @return Generator + */ + public function providePathOfFile() + { + $template = 'File with path \'%s\' is empty (has no content). Did you provide path of proper file?'; + + yield[ + 'aa/bb/cc', + sprintf($template, 'aa/bb/cc'), + ]; + + yield[ + 'images/show/car.jpg', + sprintf($template, 'images/show/car.jpg'), + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/File/EmptyFilePathExceptionTest.php b/tests/Meritoo/Common/Test/Exception/File/EmptyFilePathExceptionTest.php new file mode 100644 index 0000000..084831b --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/File/EmptyFilePathExceptionTest.php @@ -0,0 +1,33 @@ + + * @copyright Meritoo.pl + */ +class EmptyFilePathExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(EmptyFilePathException::class, OopVisibilityType::IS_PUBLIC); + } + + public function testConstructorMessage() + { + $exception = new EmptyFilePathException(); + static::assertEquals('Path of the file is empty. Did you provide path of proper file?', $exception->getMessage()); + } +} diff --git a/tests/Meritoo/Common/Test/Exception/File/NotExistingFileExceptionTest.php b/tests/Meritoo/Common/Test/Exception/File/NotExistingFileExceptionTest.php new file mode 100644 index 0000000..3361395 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/File/NotExistingFileExceptionTest.php @@ -0,0 +1,60 @@ + + * @copyright Meritoo.pl + */ +class NotExistingFileExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(NotExistingFileException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param string $notExistingFilePath Path of not existing (or not readable) file + * @param string $expectedMessage Expected exception's message + * + * @dataProvider providePathOfFile + */ + public function testConstructorMessage($notExistingFilePath, $expectedMessage) + { + $exception = new NotExistingFileException($notExistingFilePath); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides path of not existing file and expected exception's message + * + * @return Generator + */ + public function providePathOfFile() + { + $template = 'File with path \'%s\' does not exist (or is not readable). Did you provide path of proper file?'; + + yield[ + 'aa/bb/cc', + sprintf($template, 'aa/bb/cc'), + ]; + + yield[ + 'images/show/car.jpg', + sprintf($template, 'images/show/car.jpg'), + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/Reflection/CannotResolveClassNameExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Reflection/CannotResolveClassNameExceptionTest.php new file mode 100644 index 0000000..c0e2629 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Reflection/CannotResolveClassNameExceptionTest.php @@ -0,0 +1,73 @@ + + * @copyright Meritoo.pl + */ +class CannotResolveClassNameExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(CannotResolveClassNameException::class, OopVisibilityType::IS_PUBLIC, 2, 1); + } + + /** + * @param array|object|string $source Source of the class's / trait's name. It can be an array of objects, + * namespaces, object or namespace. + * @param bool $forClass If is set to true, message of this exception for class is prepared. + * Otherwise - for trait. + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideClassName + */ + public function testConstructorMessage($source, $forClass, $expectedMessage) + { + $exception = new CannotResolveClassNameException($source, $forClass); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides source of the class's / trait's name, information if message of this exception should be prepared for + * class and the expected exception's message + * + * @return Generator + */ + public function provideClassName() + { + yield[ + 'Not\Existing\Class', + true, + 'Name of class from given \'string\' Not\Existing\Class cannot be resolved. Is there everything ok?', + ]; + + yield[ + 'Not\Existing\Trait', + false, + 'Name of trait from given \'string\' Not\Existing\Trait cannot be resolved. Is there everything ok?', + ]; + + yield[ + [ + new \stdClass(), + new \stdClass(), + ], + true, + 'Name of class from given \'array\' cannot be resolved. Is there everything ok?', + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/Reflection/MissingChildClassesExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Reflection/MissingChildClassesExceptionTest.php new file mode 100644 index 0000000..dfff334 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Reflection/MissingChildClassesExceptionTest.php @@ -0,0 +1,65 @@ + + * @copyright Meritoo.pl + */ +class MissingChildClassesExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(MissingChildClassesException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param array|object|string $parentClass Class that hasn't child classes, but it should. An array of objects, + * strings, object or string. + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideParentClass + */ + public function testConstructorMessage($parentClass, $expectedMessage) + { + $exception = new MissingChildClassesException($parentClass); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides name of class that hasn't child classes, but it should, and expected exception's message + * + * @return Generator + */ + public function provideParentClass() + { + $template = 'The \'%s\' class requires one child class at least who will extend her (maybe is an abstract' + . ' class), but the child classes are missing. Did you forget to extend this class?'; + + yield[ + MissingChildClassesException::class, + sprintf($template, MissingChildClassesException::class), + ]; + + yield[ + [ + new \stdClass(), + new \stdClass(), + ], + sprintf($template, \stdClass::class), + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/Reflection/TooManyChildClassesExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Reflection/TooManyChildClassesExceptionTest.php new file mode 100644 index 0000000..4514657 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Reflection/TooManyChildClassesExceptionTest.php @@ -0,0 +1,74 @@ + + * @copyright Meritoo.pl + */ +class TooManyChildClassesExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(TooManyChildClassesException::class, OopVisibilityType::IS_PUBLIC, 2, 2); + } + + /** + * @param array|object|string $parentClass Class that has more than one child class, but it shouldn't. An array + * of objects, strings, object or string. + * @param array $childClasses Child classes + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideParentAndChildClasses + */ + public function testConstructorMessage($parentClass, array $childClasses, $expectedMessage) + { + $exception = new TooManyChildClassesException($parentClass, $childClasses); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides name of class that has more than one child class, but it shouldn't, child classes, and expected + * exception's message + * + * @return Generator + */ + public function provideParentAndChildClasses() + { + $template = "The '%s' class requires one child class at most who will extend her, but more than one child" + . " class was found:\n- %s\n\nWhy did you create more than one classes that extend '%s' class?"; + + yield[ + BaseTestCase::class, + [ + \stdClass::class, + OopVisibilityType::class, + ], + sprintf($template, BaseTestCase::class, implode("\n- ", [ + \stdClass::class, + OopVisibilityType::class, + ]), BaseTestCase::class), + ]; + + yield[ + TooManyChildClassesException::class, + [ + \stdClass::class, + ], + sprintf($template, TooManyChildClassesException::class, implode("\n- ", [\stdClass::class]), TooManyChildClassesException::class), + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/Regex/IncorrectColorHexLengthExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Regex/IncorrectColorHexLengthExceptionTest.php new file mode 100644 index 0000000..9cbd34f --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Regex/IncorrectColorHexLengthExceptionTest.php @@ -0,0 +1,60 @@ + + * @copyright Meritoo.pl + */ +class IncorrectColorHexLengthExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(IncorrectColorHexLengthException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param string $color Incorrect hexadecimal value of color + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideColor + */ + public function testConstructorMessage($color, $expectedMessage) + { + $exception = new IncorrectColorHexLengthException($color); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides incorrect hexadecimal value of color and expected exception's message + * + * @return Generator + */ + public function provideColor() + { + $template = 'Length of hexadecimal value of color \'%s\' is incorrect. It\'s %d, but it should be 3 or 6. Is there everything ok?'; + + yield[ + '', + sprintf($template, '', strlen('')), + ]; + + yield[ + 'aa-bb-cc', + sprintf($template, 'aa-bb-cc', strlen('aa-bb-cc')), + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/Regex/InvalidColorHexValueExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Regex/InvalidColorHexValueExceptionTest.php new file mode 100644 index 0000000..7376676 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Regex/InvalidColorHexValueExceptionTest.php @@ -0,0 +1,60 @@ + + * @copyright Meritoo.pl + */ +class InvalidColorHexValueExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(InvalidColorHexValueException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param string $color Invalid hexadecimal value of color + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideColor + */ + public function testConstructorMessage($color, $expectedMessage) + { + $exception = new InvalidColorHexValueException($color); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides invalid hexadecimal value of color and expected exception's message + * + * @return Generator + */ + public function provideColor() + { + $template = 'Hexadecimal value of color \'%s\' is invalid. Is there everything ok?'; + + yield[ + '', + sprintf($template, ''), + ]; + + yield[ + 'aa-bb-cc', + sprintf($template, 'aa-bb-cc'), + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/Regex/InvalidUrlExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Regex/InvalidUrlExceptionTest.php new file mode 100644 index 0000000..735bae3 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Regex/InvalidUrlExceptionTest.php @@ -0,0 +1,60 @@ + + * @copyright Meritoo.pl + */ +class InvalidUrlExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(InvalidUrlException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param string $url Invalid url + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideUrl + */ + public function testConstructorMessage($url, $expectedMessage) + { + $exception = new InvalidUrlException($url); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides invalid url and expected exception's message + * + * @return Generator + */ + public function provideUrl() + { + $template = 'Url \'%s\' is invalid. Is there everything ok?'; + + yield[ + 'aa/bb/cc', + sprintf($template, 'aa/bb/cc'), + ]; + + yield[ + 'http:/images\show\car.jpg', + sprintf($template, 'http:/images\show\car.jpg'), + ]; + } +} diff --git a/tests/Meritoo/Common/Test/Exception/Type/UnknownOopVisibilityTypeExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Type/UnknownOopVisibilityTypeExceptionTest.php new file mode 100644 index 0000000..b522263 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Type/UnknownOopVisibilityTypeExceptionTest.php @@ -0,0 +1,64 @@ + + * @copyright Meritoo.pl + */ +class UnknownOopVisibilityTypeExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(UnknownOopVisibilityTypeException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param string $unknownType Unknown OOP-related visibility + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideUnknownType + */ + public function testConstructorMessage($unknownType, $expectedMessage) + { + $exception = new UnknownOopVisibilityTypeException($unknownType); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides path of the empty file and expected exception's message + * + * @return Generator + */ + public function provideUnknownType() + { + $allTypes = (new OopVisibilityType())->getAll(); + + $template = 'The \'%s\' type of OOP-related visibility is unknown. Probably doesn\'t exist or there is a typo.' + . ' You should use one of these types: %s.'; + + yield[ + '', + sprintf($template, '', implode(', ', $allTypes)), + ]; + + yield[ + 123, + sprintf($template, 123, implode(', ', $allTypes)), + ]; + } +}