diff --git a/CHANGELOG.md b/CHANGELOG.md index a79b066..de7526b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Common and useful classes, methods, exceptions etc. # 1.0.3 1. Travis CI > run many tasks using Phing > update configuration +2. Template with placeholders > verification of placeholders without values > make stronger and point out which are +missing # 1.0.2 diff --git a/src/Exception/ValueObject/Template/MissingPlaceholdersInValuesException.php b/src/Exception/ValueObject/Template/MissingPlaceholdersInValuesException.php new file mode 100644 index 0000000..3974583 --- /dev/null +++ b/src/Exception/ValueObject/Template/MissingPlaceholdersInValuesException.php @@ -0,0 +1,36 @@ + + * @copyright Meritoo + */ +class MissingPlaceholdersInValuesException extends Exception +{ + /** + * Creates an exception + * + * @param string $content Content of template + * @param array $missingPlaceholders Missing placeholders in provided values, iow. placeholders without values + * @return MissingPlaceholdersInValuesException + */ + public static function create(string $content, array $missingPlaceholders): MissingPlaceholdersInValuesException + { + $template = 'Cannot fill template \'%s\', because of missing values for placeholder(s): %s. Did you provide all' + . ' required values?'; + $message = sprintf($template, $content, implode(', ', $missingPlaceholders)); + + return new static($message); + } +} diff --git a/src/Exception/ValueObject/Template/NotEnoughValuesException.php b/src/Exception/ValueObject/Template/NotEnoughValuesException.php deleted file mode 100644 index 3ff696b..0000000 --- a/src/Exception/ValueObject/Template/NotEnoughValuesException.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @copyright Meritoo - */ -class NotEnoughValuesException extends Exception -{ - /** - * Creates an exception - * - * @param string $content Invalid content of template - * @param int $valuesCount Count of values - * @param int $placeholdersCount Count of placeholders - * @return NotEnoughValuesException - */ - public static function create(string $content, int $valuesCount, int $placeholdersCount): NotEnoughValuesException - { - $template = 'Not enough values (%d) to fill all placeholders (%d) in template \'%s\'. Did you provide all' - . ' required values?'; - $message = sprintf($template, $valuesCount, $placeholdersCount, $content); - - return new static($message); - } -} diff --git a/src/ValueObject/Template.php b/src/ValueObject/Template.php index f3aa60f..15bcd57 100644 --- a/src/ValueObject/Template.php +++ b/src/ValueObject/Template.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace Meritoo\Common\ValueObject; use Meritoo\Common\Exception\ValueObject\Template\InvalidContentException; -use Meritoo\Common\Exception\ValueObject\Template\NotEnoughValuesException; +use Meritoo\Common\Exception\ValueObject\Template\MissingPlaceholdersInValuesException; /** * Template with placeholders that may be filled by real data @@ -54,18 +54,18 @@ class Template * Returns content of the template filled with given values (by replacing placeholders with their proper values) * * @param array $values Pairs of key-value where: key - name of placeholder, value - value of the placeholder - * @throws NotEnoughValuesException + * @throws MissingPlaceholdersInValuesException * @return string */ public function fill(array $values): string { $placeholders = static::getPlaceholders($this->content); - $valuesCount = count($values); - $placeholdersCount = count($placeholders[0]); + $providedPlaceholders = array_keys($values); + $missingPlaceholders = array_diff($placeholders[1], $providedPlaceholders); - // Oops, not enough values (iow. more placeholders than values) - if ($placeholdersCount > $valuesCount) { - throw NotEnoughValuesException::create($this->content, $valuesCount, $placeholdersCount); + // Oops, there are placeholders without values (iow. provided values are different than placeholders) + if (!empty($missingPlaceholders)) { + throw MissingPlaceholdersInValuesException::create($this->content, $missingPlaceholders); } $result = $this->content; diff --git a/tests/Collection/CollectionTest.php b/tests/Collection/CollectionTest.php index 4a28dce..222b83c 100644 --- a/tests/Collection/CollectionTest.php +++ b/tests/Collection/CollectionTest.php @@ -455,6 +455,19 @@ class CollectionTest extends BaseTestCase 3 => 'I am 4th', ]), ]; + + yield[ + 'This is test 6', + 'test2', + 4, + 'test2', + new Collection([ + 'test1' => 'I am 1st', + 'test2' => 'I am 2nd', + 2 => 'I am 3rd', + 3 => 'I am 4th', + ]), + ]; } public function provideElementGetByIndex() diff --git a/tests/Exception/ValueObject/Template/MissingPlaceholdersInValuesExceptionTest.php b/tests/Exception/ValueObject/Template/MissingPlaceholdersInValuesExceptionTest.php new file mode 100644 index 0000000..dfcd549 --- /dev/null +++ b/tests/Exception/ValueObject/Template/MissingPlaceholdersInValuesExceptionTest.php @@ -0,0 +1,85 @@ + + * @copyright Meritoo + * + * @internal + * @covers \Meritoo\Common\Exception\ValueObject\Template\MissingPlaceholdersInValuesException + */ +class MissingPlaceholdersInValuesExceptionTest extends BaseTestCase +{ + public function testConstructorVisibilityAndArguments(): void + { + static::assertConstructorVisibilityAndArguments( + MissingPlaceholdersInValuesException::class, + OopVisibilityType::IS_PUBLIC, + 3 + ); + } + + /** + * @param string $description Description of test + * @param string $content Content of template + * @param array $missingPlaceholders Missing placeholders in provided values, iow. placeholders without values + * @param string $expectedMessage Expected exception's message + * + * @dataProvider provideContentAndMissingPlaceholders + */ + public function testCreate( + string $description, + string $content, + array $missingPlaceholders, + string $expectedMessage + ): void { + $exception = MissingPlaceholdersInValuesException::create($content, $missingPlaceholders); + static::assertSame($expectedMessage, $exception->getMessage(), $description); + } + + public function provideContentAndMissingPlaceholders(): ?\Generator + { + $template = 'Cannot fill template \'%s\', because of missing values for placeholder(s): %s. Did you provide all' + . ' required values?'; + + yield[ + 'Missing 2nd placeholder', + '%test1% - %test2%', + [ + 'test2', + ], + sprintf( + $template, + '%test1% - %test2%', + 'test2' + ), + ]; + + yield[ + 'Missing 2nd and 3rd placeholder', + '%test1% / %test2% / %test3%', + [ + 'test2', + 'test3', + ], + sprintf( + $template, + '%test1% / %test2% / %test3%', + 'test2, test3' + ), + ]; + } +} diff --git a/tests/Exception/ValueObject/Template/NotEnoughValuesExceptionTest.php b/tests/Exception/ValueObject/Template/NotEnoughValuesExceptionTest.php deleted file mode 100644 index 533fb55..0000000 --- a/tests/Exception/ValueObject/Template/NotEnoughValuesExceptionTest.php +++ /dev/null @@ -1,85 +0,0 @@ - - * @copyright Meritoo - * - * @internal - * @covers \Meritoo\Common\Exception\ValueObject\Template\NotEnoughValuesException - */ -class NotEnoughValuesExceptionTest extends BaseTestCase -{ - public function testConstructorVisibilityAndArguments(): void - { - static::assertConstructorVisibilityAndArguments( - NotEnoughValuesException::class, - OopVisibilityType::IS_PUBLIC, - 3 - ); - } - - /** - * @param string $description Description of test - * @param string $content Invalid content of template - * @param int $valuesCount Count of values - * @param int $placeholdersCount Count of placeholders - * @param string $expectedMessage Expected exception's message - * - * @dataProvider provideContentAndValuesPlaceholdersCount - */ - public function testCreate( - string $description, - string $content, - int $valuesCount, - int $placeholdersCount, - string $expectedMessage - ): void { - $exception = NotEnoughValuesException::create($content, $valuesCount, $placeholdersCount); - static::assertSame($expectedMessage, $exception->getMessage(), $description); - } - - public function provideContentAndValuesPlaceholdersCount(): ?Generator - { - $template = 'Not enough values (%d) to fill all placeholders (%d) in template \'%s\'. Did you provide all' - . ' required values?'; - - yield[ - 'An empty string', - '', - 3, - 1, - sprintf($template, 3, 1, ''), - ]; - - yield[ - 'Simple string', - 'Lorem ipsum', - 1, - 4, - sprintf($template, 1, 4, 'Lorem ipsum'), - ]; - - yield[ - 'One sentence', - 'Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh.', - 5, - 0, - sprintf($template, 5, 0, 'Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh.'), - ]; - } -} diff --git a/tests/ValueObject/TemplateTest.php b/tests/ValueObject/TemplateTest.php index 953565d..0c97067 100644 --- a/tests/ValueObject/TemplateTest.php +++ b/tests/ValueObject/TemplateTest.php @@ -10,7 +10,7 @@ namespace Meritoo\Test\Common\ValueObject; use Generator; use Meritoo\Common\Exception\ValueObject\Template\InvalidContentException; -use Meritoo\Common\Exception\ValueObject\Template\NotEnoughValuesException; +use Meritoo\Common\Exception\ValueObject\Template\MissingPlaceholdersInValuesException; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Type\OopVisibilityType; use Meritoo\Common\Utilities\Reflection; @@ -69,11 +69,11 @@ class TemplateTest extends BaseTestCase * placeholder * @param string $exceptionMessage Expected message of exception * - * @dataProvider provideTemplateToFillUsingNotEnoughValues + * @dataProvider provideTemplateToFillUsingIncorrectValues */ - public function testFillUsingNotEnoughValues(Template $template, array $values, string $exceptionMessage): void + public function testFillUsingIncorrectValues(Template $template, array $values, string $exceptionMessage): void { - $this->expectException(NotEnoughValuesException::class); + $this->expectException(MissingPlaceholdersInValuesException::class); $this->expectExceptionMessage($exceptionMessage); $template->fill($values); @@ -118,19 +118,30 @@ class TemplateTest extends BaseTestCase ]; } - public function provideTemplateToFillUsingNotEnoughValues(): ?Generator + public function provideTemplateToFillUsingIncorrectValues(): ?Generator { - $template = 'Not enough values (%d) to fill all placeholders (%d) in template \'%s\'. Did you provide all' + $template = 'Cannot fill template \'%s\', because of missing values for placeholder(s): %s. Did you provide all' . ' required values?'; + yield[ + new Template('%test%'), + [ + 'something' => 123, + ], + sprintf( + $template, + '%test%', + 'test' + ), + ]; + yield[ new Template('%test%'), [], sprintf( $template, - 0, - 1, - '%test%' + '%test%', + 'test' ), ]; @@ -141,24 +152,39 @@ class TemplateTest extends BaseTestCase ], sprintf( $template, - 1, - 2, - '%test1% - %test2%' + '%test1% - %test2%', + 'test2' + ), + ]; + + yield[ + new Template('%test1% - %test2%'), + [ + 'test1' => 123, + 'test3' => 456, + ], + sprintf( + $template, + '%test1% - %test2%', + 'test2' + ), + ]; + + yield[ + new Template('%test1% / %test2% / %test3%'), + [ + 'test1' => 123, + ], + sprintf( + $template, + '%test1% / %test2% / %test3%', + 'test2, test3' ), ]; } public function provideTemplateToFill(): ?Generator { - yield[ - 'Template with 1 placeholder, but incorrect values', - new Template('%test%'), - [ - 'something' => 123, - ], - '%test%', - ]; - yield[ 'Template with 1 placeholder', new Template('%test%'),