From d88ead92feaa353840cfaa7c5fd828061ff76254 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Tue, 5 Mar 2019 10:11:48 +0100 Subject: [PATCH] Tests > use @dataProvider --- CHANGELOG.md | 1 + src/Utilities/Miscellaneous.php | 48 +-- tests/Utilities/MiscellaneousTest.php | 422 +++++++++++++++++++++++--- 3 files changed, 403 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9306157..a44377d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Common and useful classes, methods, exceptions etc. 1. Arrays > refactoring & more tests 2. ValueObject > Human > represents a human 3. Tests > use `Meritoo\Test\Common` namespace (instead of `Meritoo\Common\Test`) +4. Tests > use @dataProvider # 0.1.5 diff --git a/src/Utilities/Miscellaneous.php b/src/Utilities/Miscellaneous.php index 283e929..7dd7e61 100644 --- a/src/Utilities/Miscellaneous.php +++ b/src/Utilities/Miscellaneous.php @@ -425,6 +425,14 @@ class Miscellaneous */ public static function replace($subject, $search, $replacement, $quoteStrings = false) { + /* + * Unknown source or item to find or replacement is an empty array? + * Nothing to do + */ + if (empty($subject) || empty($search) || [] === $replacement) { + return $subject; + } + $effect = $subject; $searchIsString = is_string($search); @@ -444,14 +452,24 @@ class Miscellaneous $bothAreStrings = $searchIsString && $replacementIsString; $bothAreArrays = $searchIsArray && $replacementIsArray; + if ($quoteStrings) { + if ($replacementIsString) { + $replacement = '\'' . $replacement . '\''; + } elseif ($replacementIsArray) { + foreach ($replacement as &$item) { + if (is_string($item)) { + $item = '\'' . $item . '\''; + } + } + + unset($item); + } + } + /* * First step: replace strings, simple operation with strings */ - if ($searchIsString && $replacementIsString) { - if ($quoteStrings) { - $replacement = '\'' . $replacement . '\''; - } - + if ($bothAreStrings) { $effect = str_replace($search, $replacement, $subject); } @@ -460,16 +478,16 @@ class Miscellaneous * Attention. Searched and replacement value should be the same type: strings or arrays. */ if ($effect === $subject && ($bothAreStrings || $bothAreArrays)) { - if ($quoteStrings && $replacementIsString) { - $replacement = '\'' . $replacement . '\''; - } - /* * I have to avoid string that contains spaces only, e.g. " ". * It's required to avoid bug: preg_replace(): Empty regular expression. */ if ($searchIsArray || ($searchIsString && !empty(trim($search)))) { - $effect = preg_replace($search, $replacement, $subject); + $replaced = @preg_replace($search, $replacement, $subject); + + if (null !== $replaced && [] !== $replaced) { + $effect = $replaced; + } } } @@ -500,16 +518,6 @@ class Miscellaneous $exploded = explode($search, $subSubject); $explodedCount = count($exploded); - if ($quoteStrings) { - foreach ($replacement as &$item) { - if (is_string($item)) { - $item = '\'' . $item . '\''; - } - } - - unset($item); - } - foreach ($exploded as $key => $item) { $subEffect .= $item; diff --git a/tests/Utilities/MiscellaneousTest.php b/tests/Utilities/MiscellaneousTest.php index 38aec86..c2ed2bb 100644 --- a/tests/Utilities/MiscellaneousTest.php +++ b/tests/Utilities/MiscellaneousTest.php @@ -14,7 +14,6 @@ use Meritoo\Common\Exception\Regex\InvalidColorHexValueException; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Utilities\Locale; use Meritoo\Common\Utilities\Miscellaneous; -use ReflectionException; use stdClass; /** @@ -226,57 +225,68 @@ class MiscellaneousTest extends BaseTestCase self::assertEquals($this->stringSmall, Miscellaneous::replace($this->stringSmall, $search, $replacement3, true)); } - public function testReplace() + /** + * @param string $description Description of test + * @param string|array $subject The string or an array of strings to search and replace + * @param string|array $search String or pattern or array of patterns to find. It may be: string, an array + * of strings or an array of patterns. + * @param string|array $replacement The string or an array of strings to replace. It may be: string or an array + * of strings. + * @param mixed $result Result of replacing + * + * @dataProvider provideEmptyValuesToReplace + */ + public function testReplaceUsingEmptyValues($description, $subject, $search, $replacement, $result) { - /* - * Common variables - */ - $quoteStrings = true; + static::assertSame($result, Miscellaneous::replace($subject, $search, $replacement), $description); + } - $subject = [ - $this->stringSmall, - $this->stringDotSeparated, - ]; + /** + * @param string $description Description of test + * @param string $subject The string or an array of strings to search and replace + * @param string $search String or pattern or array of patterns to find. It may be: string, an array of + * strings or an array of patterns. + * @param string $replacement The string or an array of strings to replace. It may be: string or an array of + * strings. + * @param mixed $result Result of replacing + * + * @dataProvider provideStringsToReplace + */ + public function testReplaceUsingStrings($description, $subject, $search, $replacement, $result) + { + static::assertSame($result, Miscellaneous::replace($subject, $search, $replacement), $description); + } - /* - * Testing replace with an array - */ - $search = [ - '|ipsum|', - '|pellentesque|', - ]; + /** + * @param string $description Description of test + * @param string $subject The string or an array of strings to search and replace + * @param string $search String or pattern or array of patterns to find. It may be: string, an array of + * strings or an array of patterns. + * @param string $replacement The string or an array of strings to replace. It may be: string or an array of + * strings. + * @param mixed $result Result of replacing + * + * @dataProvider provideRegexToReplace + */ + public function testReplaceUsingRegex($description, $subject, $search, $replacement, $result) + { + static::assertSame($result, Miscellaneous::replace($subject, $search, $replacement), $description); + } - $replacement = [ - 'commodo', - 'interdum', - ]; - - $replaced1 = Miscellaneous::replace($subject, $search, $replacement); - $replaced2 = Miscellaneous::replace($subject, $search, $replacement, true); - - self::assertEquals('Lorem commodo dolor sit amet.', $replaced1[0]); - self::assertEquals('Etiam ullamcorper. Suspendisse a interdum dui, non felis.', $replaced1[1]); - - self::assertEquals('Lorem commodo dolor sit amet.', $replaced2[0]); - self::assertEquals('Etiam ullamcorper. Suspendisse a interdum dui, non felis.', $replaced2[1]); - - /* - * Testing replace with string - */ - $searchString = 'ipsum'; - $replacementString = 'commodo'; - - $replaced = Miscellaneous::replace($subject, $searchString, $replacementString, $quoteStrings); - self::assertEquals('Lorem \'commodo\' dolor sit amet.', $replaced[0]); - - /* - * Testing replace with mixed values: - * - subject: array - * - search: string - * - replacement: string - */ - $replacedMixed = Miscellaneous::replace($subject, $searchString, $replacement, $quoteStrings); - self::assertEquals('Lorem \'commodo\' dolor sit amet.', $replacedMixed[0]); + /** + * @param string $description Description of test + * @param string $subject The string or an array of strings to search and replace + * @param string $search String or pattern or array of patterns to find. It may be: string, an array of + * strings or an array of patterns. + * @param string $replacement The string or an array of strings to replace. It may be: string or an array of + * strings. + * @param mixed $result Result of replacing + * + * @dataProvider provideDataToReplaceWithQuoteStrings + */ + public function testReplaceWithQuoteStrings($description, $subject, $search, $replacement, $result) + { + static::assertSame($result, Miscellaneous::replace($subject, $search, $replacement, true), $description); } public function testUppercaseFirst() @@ -1156,6 +1166,322 @@ class MiscellaneousTest extends BaseTestCase ]; } + public function provideEmptyValuesToReplace() + { + yield[ + 'An empty string as subject', + '', + 'test', + 'another test', + '', + ]; + + yield[ + 'An empty array as subject', + [], + 'test', + 'another test', + [], + ]; + + yield[ + 'Null as subject', + null, + 'test', + 'another test', + null, + ]; + + yield[ + 'An empty string to search', + 'test', + '', + 'another test', + 'test', + ]; + + yield[ + 'An empty array to search', + 'test', + [], + 'another test', + 'test', + ]; + + yield[ + 'Null to search', + 'test', + null, + 'another test', + 'test', + ]; + + yield[ + 'An empty string as replacement', + 'test', + 'another test', + '', + 'test', + ]; + + yield[ + 'An empty array as replacement', + 'test', + 'another test', + [], + 'test', + ]; + + yield[ + 'Null as replacement', + 'test', + 'another test', + null, + 'test', + ]; + } + + public function provideStringsToReplace() + { + yield[ + 'Different count of strings to search and replace - 1st part', + 'Lorem ipsum dolor sit amet', + [ + 'ipsum', + ], + 'commodo', + 'Lorem ipsum dolor sit amet', + ]; + + yield[ + 'Different count of strings to search and replace - 2nd part', + 'Lorem ipsum dolor sit amet', + 'ipsum', + [ + 'commodo', + ], + 'Lorem commodo dolor sit amet', + ]; + + yield[ + 'Replace 1 not existing word in 1 sentence (nothing to replace)', + 'Lorem ipsum dolor sit amet', + 'plum', + 'commodo', + 'Lorem ipsum dolor sit amet', + ]; + + yield[ + 'Replace 1 word in 1 sentence', + 'Lorem ipsum dolor sit amet', + 'ipsum', + 'commodo', + 'Lorem commodo dolor sit amet', + ]; + + yield[ + 'Replace 1 not existing word in 2 sentences (nothing to replace)', + [ + 'Lorem ipsum dolor sit amet', + 'Maecenas sed diam eget risus varius blandit sit amet', + ], + 'plum', + 'commodo', + [ + 'Lorem ipsum dolor sit amet', + 'Maecenas sed diam eget risus varius blandit sit amet', + ], + ]; + + yield[ + 'Replace 1 word in 2 sentences', + [ + 'Lorem ipsum dolor sit amet', + 'Maecenas sed diam eget risus varius blandit sit amet', + ], + 'amet', + 'commodo', + [ + 'Lorem ipsum dolor sit commodo', + 'Maecenas sed diam eget risus varius blandit sit commodo', + ], + ]; + } + + public function provideRegexToReplace() + { + yield[ + 'Different count of strings to search and replace - 1st part', + 'Lorem ipsum dolor sit amet', + [ + '|ipsum|', + ], + 'commodo', + 'Lorem ipsum dolor sit amet', + ]; + + yield[ + 'Different count of strings to search and replace - 2nd part', + 'Lorem ipsum dolor sit amet', + '|ipsum|', + [ + 'commodo', + ], + 'Lorem ipsum dolor sit amet', + ]; + + yield[ + '1 pattern (word -> "")', + 'Lorem ipsum dolor sit amet', + '|ipsum|', + '', + 'Lorem dolor sit amet', + ]; + + yield[ + '1 pattern (word -> word)', + 'Lorem ipsum dolor sit amet', + '|ipsum|', + 'commodo', + 'Lorem commodo dolor sit amet', + ]; + + yield[ + '2 patterns (word -> word)', + 'Lorem ipsum dolor sit amet', + [ + '|ipsum|', + '|amet|', + ], + [ + 'commodo', + 'egestas', + ], + 'Lorem commodo dolor sit egestas', + ]; + + yield[ + '1 word in 2 sentences', + [ + 'Lorem ipsum dolor sit amet', + 'Maecenas sed diam eget risus varius blandit sit amet', + ], + '|amet|', + 'commodo', + [ + 'Lorem ipsum dolor sit commodo', + 'Maecenas sed diam eget risus varius blandit sit commodo', + ], + ]; + + yield[ + '2 words in 2 sentences', + [ + 'Lorem ipsum dolor sit amet', + 'Maecenas sed diam eget risus varius blandit sit amet', + ], + [ + '|ipsum|', + '|amet|', + ], + [ + 'commodo', + 'egestas', + ], + [ + 'Lorem commodo dolor sit egestas', + 'Maecenas sed diam eget risus varius blandit sit egestas', + ], + ]; + } + + public function provideDataToReplaceWithQuoteStrings() + { + yield[ + 'An empty string as subject', + '', + 'test', + 'another test', + '', + ]; + + yield[ + 'An empty string to search', + 'test', + '', + 'another test', + 'test', + ]; + + yield[ + 'An empty string as replacement', + 'test', + 'another test', + '', + 'test', + ]; + + yield[ + 'Replace 1 not existing word in 1 sentence (nothing to replace)', + 'Lorem ipsum dolor sit amet', + 'plum', + 'commodo', + 'Lorem ipsum dolor sit amet', + ]; + + yield[ + 'Replace 1 word in 1 sentence', + 'Lorem ipsum dolor sit amet', + 'ipsum', + 'commodo', + 'Lorem \'commodo\' dolor sit amet', + ]; + + yield[ + 'Replace 1 word in 2 sentences', + [ + 'Lorem ipsum dolor sit amet', + 'Maecenas sed diam eget risus varius blandit sit amet', + ], + 'amet', + 'commodo', + [ + 'Lorem ipsum dolor sit \'commodo\'', + 'Maecenas sed diam eget risus varius blandit sit \'commodo\'', + ], + ]; + + yield[ + '1 pattern (word -> "")', + 'Lorem ipsum dolor sit amet', + '|ipsum|', + '', + 'Lorem \'\' dolor sit amet', + ]; + + yield[ + '1 pattern (word -> word)', + 'Lorem ipsum dolor sit amet', + '|ipsum|', + 'commodo', + 'Lorem \'commodo\' dolor sit amet', + ]; + + yield[ + '2 patterns (word -> word)', + 'Lorem ipsum dolor sit amet', + [ + '|ipsum|', + '|amet|', + ], + [ + 'commodo', + 'egestas', + ], + 'Lorem \'commodo\' dolor sit \'egestas\'', + ]; + } + /** * {@inheritdoc} */