diff --git a/src/Utilities/Repository.php b/src/Utilities/Repository.php index e0b8e2e..6f95902 100644 --- a/src/Utilities/Repository.php +++ b/src/Utilities/Repository.php @@ -204,13 +204,6 @@ class Repository */ private static function isSorted($item) { - // Not sortable? - if (!self::isSortable($item)) { - return false; - } - - // It's an object or it's an array and position has been set? - return (is_object($item) && null !== $item->getPosition()) || diff --git a/tests/Utilities/RepositoryTest.php b/tests/Utilities/RepositoryTest.php index 89f0c0f..dfb85c4 100644 --- a/tests/Utilities/RepositoryTest.php +++ b/tests/Utilities/RepositoryTest.php @@ -72,6 +72,19 @@ class RepositoryTest extends BaseTestCase static::assertEquals($before, $after); } + /** + * @param string $description Description of test + * @param array $items Objects who have "getPosition()" and "setPosition()" methods or arrays + * @param array $expected Expected items with positions replenished + * + * @dataProvider provideSortedItems + */ + public function testReplenishPositionsUsingSortedItems(string $description, array $items, array $expected) + { + Repository::replenishPositions($items); + static::assertSame($expected, $items, $description); + } + /** * @param array $items Objects who have "getPosition()" and "setPosition()" methods or arrays * @dataProvider provideArraysWithoutExtremePosition @@ -833,4 +846,88 @@ class RepositoryTest extends BaseTestCase 'qb.first_name DESC', ]; } + + public function provideSortedItems() + { + $sortable1 = new Sortable(); + $sortable1->setPosition(1); + + $sortable2 = new Sortable(); + $sortable2->setPosition(2); + + $sortable3 = new Sortable(); + $sortable3->setPosition(309); + + yield[ + 'An array with 1 item only', + [ + [ + 'test 1', + 'position' => 1, + ], + ], + [ + [ + 'test 1', + 'position' => 1, + ], + ], + ]; + + yield[ + 'An array with more than 1 item', + [ + [ + 'test 1', + 'position' => 1, + ], + [ + 'test 2', + 'position' => 2, + ], + [ + 'test 3', + 'position' => 309, + ], + ], + [ + [ + 'test 1', + 'position' => 1, + ], + [ + 'test 2', + 'position' => 2, + ], + [ + 'test 3', + 'position' => 309, + ], + ], + ]; + + yield[ + '1 object only', + [ + $sortable1, + ], + [ + $sortable1, + ], + ]; + + yield[ + 'More than 1 object', + [ + $sortable1, + $sortable2, + $sortable3, + ], + [ + $sortable1, + $sortable2, + $sortable3, + ], + ]; + } } diff --git a/tests/ValueObject/TemplateTest.php b/tests/ValueObject/TemplateTest.php index 6de3828..953565d 100644 --- a/tests/ValueObject/TemplateTest.php +++ b/tests/ValueObject/TemplateTest.php @@ -13,6 +13,7 @@ use Meritoo\Common\Exception\ValueObject\Template\InvalidContentException; use Meritoo\Common\Exception\ValueObject\Template\NotEnoughValuesException; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Type\OopVisibilityType; +use Meritoo\Common\Utilities\Reflection; use Meritoo\Common\ValueObject\Template; /** @@ -50,6 +51,18 @@ class TemplateTest extends BaseTestCase new Template($content); } + /** + * @param string $description Description of test + * @param string $content Raw string with placeholders (content of the template) + * + * @dataProvider provideValidContent + */ + public function testIsValidUsingValidContent(string $description, string $content): void + { + $template = new Template($content); + static::assertSame($content, Reflection::getPropertyValue($template, 'content', true), $description); + } + /** * @param Template $template Template to fill * @param array $values Pairs of key-value where: key - name of placeholder, value - value of the @@ -212,4 +225,22 @@ class TemplateTest extends BaseTestCase 'My name is Jane Brown and I live in NY, USA', ]; } + + public function provideValidContent(): ?Generator + { + yield[ + 'Template with 1 placeholder', + '%test%', + ]; + + yield[ + 'Template with 2 placeholders', + 'My name is %name% and I am %profession%', + ]; + + yield[ + 'Template with 2 placeholders that contains space', + 'My name is %first name% %last name% and I live in %current location%', + ]; + } }