From 72480ecc27a91d025bce773ee1e011573d1ada46 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Tue, 26 Sep 2017 14:39:00 +0200 Subject: [PATCH] Tests - test cases of exceptions --- .../Exception/UnknownInstanceOfResultItem.php | 6 ++ .../CreateSessionKeyFailedExceptionTest.php | 61 +++++++++++++++++ .../UnknownInstanceOfResultItemTest.php | 65 +++++++++++++++++++ .../Exception/UnknownMethodExceptionTest.php | 64 ++++++++++++++++++ 4 files changed, 196 insertions(+) create mode 100644 tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/CreateSessionKeyFailedExceptionTest.php create mode 100644 tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/UnknownInstanceOfResultItemTest.php create mode 100644 tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/UnknownMethodExceptionTest.php diff --git a/src/Meritoo/LimeSurvey/ApiClient/Exception/UnknownInstanceOfResultItem.php b/src/Meritoo/LimeSurvey/ApiClient/Exception/UnknownInstanceOfResultItem.php index 550ab1f..49cd739 100644 --- a/src/Meritoo/LimeSurvey/ApiClient/Exception/UnknownInstanceOfResultItem.php +++ b/src/Meritoo/LimeSurvey/ApiClient/Exception/UnknownInstanceOfResultItem.php @@ -1,5 +1,11 @@ + * @copyright Meritoo.pl + */ +class CreateSessionKeyFailedExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(CreateSessionKeyFailedException::class, OopVisibilityType::IS_PUBLIC, 1, 0); + } + + /** + * @param string $reason Reason of failure, e.g. "Invalid user name or password" + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideReason + */ + public function testConstructorMessage($reason, $expectedMessage) + { + $exception = new CreateSessionKeyFailedException($reason); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides reason of failure + * + * @return Generator + */ + public function provideReason() + { + $shortMessage = 'Create of the session key has failed'; + $longMessageTemplate = sprintf('%s. Reason: \'%s\'.', $shortMessage, '%s'); + + yield[ + '', + $shortMessage, + ]; + + yield[ + 'Invalid user name or password', + sprintf($longMessageTemplate, 'Invalid user name or password'), + ]; + } +} diff --git a/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/UnknownInstanceOfResultItemTest.php b/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/UnknownInstanceOfResultItemTest.php new file mode 100644 index 0000000..ae93f68 --- /dev/null +++ b/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/UnknownInstanceOfResultItemTest.php @@ -0,0 +1,65 @@ + + * @copyright Meritoo.pl + */ +class UnknownInstanceOfResultItemTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(UnknownInstanceOfResultItem::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param string $method Name of called method while talking to the LimeSurvey's API. One of the + * MethodType class constants. + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideMethodName + */ + public function testConstructorMessage($method, $expectedMessage) + { + $exception = new UnknownInstanceOfResultItem($method); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides name of called method + * + * @return Generator + */ + public function provideMethodName() + { + $template = 'Instance of one item used by result the of \'%s\' LimeSurvey API\'s method is unknown. Proper' + . ' class is not mapped in %s::%s() method. Did you forget about this?'; + + yield[ + MethodType::LIST_SURVEYS, + sprintf($template, MethodType::LIST_SURVEYS, ResultProcessor::class, 'getItemInstance'), + ]; + + yield[ + MethodType::ADD_PARTICIPANTS, + sprintf($template, MethodType::ADD_PARTICIPANTS, ResultProcessor::class, 'getItemInstance'), + ]; + } +} diff --git a/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/UnknownMethodExceptionTest.php b/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/UnknownMethodExceptionTest.php new file mode 100644 index 0000000..681bcfb --- /dev/null +++ b/tests/Meritoo/LimeSurvey/Test/ApiClient/Exception/UnknownMethodExceptionTest.php @@ -0,0 +1,64 @@ + + * @copyright Meritoo.pl + */ +class UnknownMethodExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments() + { + static::assertConstructorVisibilityAndArguments(UnknownMethodException::class, OopVisibilityType::IS_PUBLIC, 1, 1); + } + + /** + * @param string $unknownType The unknown type of something (value of constant) + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideUnknownType + */ + public function testConstructorMessage($unknownType, $expectedMessage) + { + $exception = new UnknownMethodException($unknownType); + static::assertEquals($expectedMessage, $exception->getMessage()); + } + + /** + * Provides name of called method + * + * @return Generator + */ + public function provideUnknownType() + { + $allMethods = implode(', ', (new MethodType())->getAll()); + + $template = 'The \'%s\' type of name of method used while talking to the LimeSurvey\'s API is unknown. Probably' + . ' doesn\'t exist or there is a typo. You should use one of these types: %s.'; + + yield[ + MethodType::ADD_PARTICIPANTS, + sprintf($template, MethodType::ADD_PARTICIPANTS, $allMethods), + ]; + + yield[ + MethodType::ADD_PARTICIPANTS, + sprintf($template, MethodType::ADD_PARTICIPANTS, $allMethods), + ]; + } +}