Template with placeholders > verification of placeholders without values > make stronger and point out which are missing

This commit is contained in:
Meritoo
2019-04-10 08:57:33 +02:00
parent 39b0172a85
commit e66cbd54e2
8 changed files with 190 additions and 152 deletions

View File

@@ -5,6 +5,8 @@ Common and useful classes, methods, exceptions etc.
# 1.0.3 # 1.0.3
1. Travis CI > run many tasks using Phing > update configuration 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 # 1.0.2

View File

@@ -0,0 +1,36 @@
<?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\ValueObject\Template;
use Exception;
/**
* An exception used while there are missing values required to fill all placeholders in template
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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);
}
}

View File

@@ -1,39 +0,0 @@
<?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.
*/
declare(strict_types=1);
namespace Meritoo\Common\Exception\ValueObject\Template;
use Exception;
/**
* An exception used while there is not enough values to fill all placeholders in template
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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);
}
}

View File

@@ -11,7 +11,7 @@ declare(strict_types=1);
namespace Meritoo\Common\ValueObject; namespace Meritoo\Common\ValueObject;
use Meritoo\Common\Exception\ValueObject\Template\InvalidContentException; 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 * 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) * 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 * @param array $values Pairs of key-value where: key - name of placeholder, value - value of the placeholder
* @throws NotEnoughValuesException * @throws MissingPlaceholdersInValuesException
* @return string * @return string
*/ */
public function fill(array $values): string public function fill(array $values): string
{ {
$placeholders = static::getPlaceholders($this->content); $placeholders = static::getPlaceholders($this->content);
$valuesCount = count($values); $providedPlaceholders = array_keys($values);
$placeholdersCount = count($placeholders[0]); $missingPlaceholders = array_diff($placeholders[1], $providedPlaceholders);
// Oops, not enough values (iow. more placeholders than values) // Oops, there are placeholders without values (iow. provided values are different than placeholders)
if ($placeholdersCount > $valuesCount) { if (!empty($missingPlaceholders)) {
throw NotEnoughValuesException::create($this->content, $valuesCount, $placeholdersCount); throw MissingPlaceholdersInValuesException::create($this->content, $missingPlaceholders);
} }
$result = $this->content; $result = $this->content;

View File

@@ -455,6 +455,19 @@ class CollectionTest extends BaseTestCase
3 => 'I am 4th', 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() public function provideElementGetByIndex()

View File

@@ -0,0 +1,85 @@
<?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\Test\Common\Exception\ValueObject\Template;
use Meritoo\Common\Exception\ValueObject\Template\MissingPlaceholdersInValuesException;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
/**
* Test case of an exception used while there are missing values required to fill all placeholders in template
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*
* @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'
),
];
}
}

View File

@@ -1,85 +0,0 @@
<?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\Test\Common\Exception\ValueObject\Template;
use Generator;
use Meritoo\Common\Exception\ValueObject\Template\NotEnoughValuesException;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType;
/**
* Test case of an exception used while there is not enough values to fill all placeholders in template
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*
* @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.'),
];
}
}

View File

@@ -10,7 +10,7 @@ namespace Meritoo\Test\Common\ValueObject;
use Generator; use Generator;
use Meritoo\Common\Exception\ValueObject\Template\InvalidContentException; 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\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType; use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Common\Utilities\Reflection; use Meritoo\Common\Utilities\Reflection;
@@ -69,11 +69,11 @@ class TemplateTest extends BaseTestCase
* placeholder * placeholder
* @param string $exceptionMessage Expected message of exception * @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); $this->expectExceptionMessage($exceptionMessage);
$template->fill($values); $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?'; . ' required values?';
yield[
new Template('%test%'),
[
'something' => 123,
],
sprintf(
$template,
'%test%',
'test'
),
];
yield[ yield[
new Template('%test%'), new Template('%test%'),
[], [],
sprintf( sprintf(
$template, $template,
0, '%test%',
1, 'test'
'%test%'
), ),
]; ];
@@ -141,24 +152,39 @@ class TemplateTest extends BaseTestCase
], ],
sprintf( sprintf(
$template, $template,
1, '%test1% - %test2%',
2, 'test2'
'%test1% - %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 public function provideTemplateToFill(): ?Generator
{ {
yield[
'Template with 1 placeholder, but incorrect values',
new Template('%test%'),
[
'something' => 123,
],
'%test%',
];
yield[ yield[
'Template with 1 placeholder', 'Template with 1 placeholder',
new Template('%test%'), new Template('%test%'),