From ffa3fbffe77c1caa4afb9aa26bd851c9ecd38cfc Mon Sep 17 00:00:00 2001 From: Meritoo Date: Fri, 22 Sep 2017 23:27:21 +0200 Subject: [PATCH] Exception - DisabledMethodException - an exception used while method cannot be called, because is disabled --- .../Method/DisabledMethodException.php | 38 +++++++++++ .../Method/DisabledMethodExceptionTest.php | 65 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/Meritoo/Common/Exception/Method/DisabledMethodException.php create mode 100644 tests/Meritoo/Common/Test/Exception/Method/DisabledMethodExceptionTest.php diff --git a/src/Meritoo/Common/Exception/Method/DisabledMethodException.php b/src/Meritoo/Common/Exception/Method/DisabledMethodException.php new file mode 100644 index 0000000..ca98911 --- /dev/null +++ b/src/Meritoo/Common/Exception/Method/DisabledMethodException.php @@ -0,0 +1,38 @@ + + * @copyright Meritoo.pl + */ +class DisabledMethodException extends Exception +{ + /** + * Class constructor + * + * @param string $disabledMethod Name of the disabled method + * @param string $alternativeMethod (optional) Name of the alternative method + */ + public function __construct($disabledMethod, $alternativeMethod = '') + { + $template = 'Method %s() cannot be called, because is disabled.'; + + if (!empty($alternativeMethod)) { + $template .= ' Use %s() instead.'; + } + + $message = sprintf($template, $disabledMethod, $alternativeMethod); + parent::__construct($message); + } +} diff --git a/tests/Meritoo/Common/Test/Exception/Method/DisabledMethodExceptionTest.php b/tests/Meritoo/Common/Test/Exception/Method/DisabledMethodExceptionTest.php new file mode 100644 index 0000000..06e6e67 --- /dev/null +++ b/tests/Meritoo/Common/Test/Exception/Method/DisabledMethodExceptionTest.php @@ -0,0 +1,65 @@ + + * @copyright Meritoo.pl + */ +class DisabledMethodExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(DisabledMethodException::class, OopVisibilityType::IS_PUBLIC, 2, 1); + } + + /** + * @param string $disabledMethod Name of the disabled method + * @param string $alternativeMethod Name of the alternative method + * @param string $expectedMessage Expected exception's message + * + * @internal param string $emptyFilePath Path of the empty file + * @dataProvider provideMethodsNames + */ + public function testConstructorMessage($disabledMethod, $alternativeMethod, $expectedMessage) + { + $exception = new DisabledMethodException($disabledMethod, $alternativeMethod); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides name of the disabled method, name of the alternative method and expected exception's message + * + * @return Generator + */ + public function provideMethodsNames() + { + $templateShort = 'Method %s() cannot be called, because is disabled.'; + $templateLong = $templateShort . ' Use %s() instead.'; + + yield[ + 'FooBar::loremIpsum', + '', + sprintf($templateShort, 'FooBar::loremIpsum'), + ]; + + yield[ + 'FooBar::loremIpsum', + 'AnotherClass::alternativeMethod', + sprintf($templateLong, 'FooBar::loremIpsum', 'AnotherClass::alternativeMethod'), + ]; + } +}