mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Exception - DisabledMethodException - an exception used while method cannot be called, because is disabled
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Meritoo\Common\Exception\Method;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception used while method cannot be called, because is disabled
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Meritoo\Common\Test\Exception\Method;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of an exception used while method cannot be called, because is disabled
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @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'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user