[Miscellaneous] [Regex] Implement data providers in tests

This commit is contained in:
Meritoo
2019-12-18 20:32:21 +01:00
parent ddb3f0a544
commit 5cd58aec25
3 changed files with 227 additions and 48 deletions

View File

@@ -889,8 +889,12 @@ class Miscellaneous
* @param string $separator The separator which divides elements of string * @param string $separator The separator which divides elements of string
* @return array * @return array
*/ */
public static function getStringElements($string, $separator) public static function getStringElements(string $string, string $separator): array
{ {
if (empty($string) || empty($separator)) {
return [];
}
$matches = []; $matches = [];
$pattern = sprintf('|[^\%s]+|', $separator); $pattern = sprintf('|[^\%s]+|', $separator);
$matchCount = preg_match_all($pattern, $string, $matches); $matchCount = preg_match_all($pattern, $string, $matches);
@@ -909,21 +913,15 @@ class Miscellaneous
* @param string $separator The separator which divides elements of string * @param string $separator The separator which divides elements of string
* @return null|string * @return null|string
*/ */
public static function getLastElementOfString($string, $separator) public static function getLastElementOfString($string, $separator): ?string
{ {
$elements = self::getStringElements($string, $separator); $elements = self::getStringElements($string, $separator);
/*
* No elements?
* Nothing to do
*/
if (empty($elements)) { if (empty($elements)) {
return null; return null;
} }
$element = Arrays::getLastElement($elements); return Arrays::getLastElement($elements);
return trim($element);
} }
/** /**

View File

@@ -94,22 +94,16 @@ class MiscellaneousTest extends BaseTestCase
self::assertEquals($withoutExtension, Miscellaneous::getFileNameWithoutExtension($fileName)); self::assertEquals($withoutExtension, Miscellaneous::getFileNameWithoutExtension($fileName));
} }
public function testGetFileNameFromPath(): void /**
* @param string $description Description of test
* @param string $path A path that contains file name
* @param string $expected Expected file name
*
* @dataProvider provideFilePath
*/
public function testGetFileNameFromPath(string $description, string $path, string $expected): void
{ {
// Path with file static::assertEquals($expected, Miscellaneous::getFileNameFromPath($path), $description);
self::assertEquals('sit.amet.JPG', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit.amet.JPG'));
// Path with complicated name of file
self::assertEquals('this-1_2 3 & my! 4+file.jpg', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/this-1_2 3 & my! 4+file.jpg'));
// Path without file
self::assertEquals('', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit-amet'));
// Path with a dot "." in name of directory
self::assertEquals('sit.amet.JPG', Miscellaneous::getFileNameFromPath('lorem/ipsum.dolor/sit.amet.JPG'));
// Relative path
self::assertEquals('sit.amet.JPG', Miscellaneous::getFileNameFromPath('lorem/ipsum/../dolor/sit.amet.JPG'));
} }
public function testGetUniqueFileName() public function testGetUniqueFileName()
@@ -480,12 +474,21 @@ class MiscellaneousTest extends BaseTestCase
self::assertEquals('1.75 MB', Miscellaneous::getHumanReadableSize(1024 * 1024 * 1.75)); self::assertEquals('1.75 MB', Miscellaneous::getHumanReadableSize(1024 * 1024 * 1.75));
} }
public function testGetLastElementOfString() /**
{ * @param string $description
self::assertEquals('elit', Miscellaneous::getLastElementOfString($this->stringCommaSeparated, ' ')); * @param string $string
self::assertEquals('consectetur adipiscing elit', Miscellaneous::getLastElementOfString($this->stringCommaSeparated, ',')); * @param string $separator
self::assertEquals(null, Miscellaneous::getLastElementOfString($this->stringCommaSeparated, ';')); * @param string|null $expected
self::assertEquals(null, Miscellaneous::getLastElementOfString($this->stringCommaSeparated, '.')); *
* @dataProvider provideLastElementOfString
*/
public function testGetLastElementOfString(
string $description,
string $string,
string $separator,
?string $expected
): void {
self::assertEquals($expected, Miscellaneous::getLastElementOfString($string, $separator), $description);
} }
public function testTrimSmart() public function testTrimSmart()
@@ -554,15 +557,21 @@ class MiscellaneousTest extends BaseTestCase
self::assertEquals(sprintf('%s.%s', $fileName, 'txt'), Miscellaneous::includeFileExtension($fileName, 'txt')); self::assertEquals(sprintf('%s.%s', $fileName, 'txt'), Miscellaneous::includeFileExtension($fileName, 'txt'));
} }
public function testGetStringElements() /**
{ * @param string $description
$elements = [ * @param string $string
'Lorem ipsum dolor sit amet', * @param string $separator
' consectetur adipiscing elit', * @param array $expected
]; *
* @dataProvider provideStringElements
self::assertEquals($elements, Miscellaneous::getStringElements($this->stringCommaSeparated, ',')); */
self::assertEquals([], Miscellaneous::getStringElements($this->stringCommaSeparated, ';')); public function testGetStringElements(
string $description,
string $string,
string $separator,
array $expected
): void {
self::assertEquals($expected, Miscellaneous::getStringElements($string, $separator), $description);
} }
public function testGetStringWithoutLastElement() public function testGetStringWithoutLastElement()
@@ -1566,6 +1575,145 @@ class MiscellaneousTest extends BaseTestCase
]; ];
} }
public function provideFilePath(): ?Generator
{
yield[
'Path with file',
'lorem/ipsum-dolor/sit.amet.JPG',
'sit.amet.JPG',
];
yield[
'Path with complicated name of file',
'lorem/ipsum-dolor/this-1_2 3 & my! 4+file.jpg',
'this-1_2 3 & my! 4+file.jpg',
];
yield[
'Path without file',
'lorem/ipsum-dolor/sit-amet',
'',
];
yield[
'Path with a dot "." in name of directory',
'lorem/ipsum.dolor/sit.amet.JPG',
'sit.amet.JPG',
];
yield[
'Relative path',
'lorem/ipsum/../dolor/sit.amet.JPG',
'sit.amet.JPG',
];
}
public function provideStringElements(): ?Generator
{
yield[
'An empty string',
'',
'',
[],
];
yield[
'One-character string',
'a',
',',
[],
];
yield[
'String without given separator',
'abc',
',',
[],
];
yield[
'Simple, short string',
'a, b, c',
',',
[
'a',
' b',
' c',
],
];
yield[
'A sentence',
'Lorem ipsum - dolor sit - amet, consectetur adipiscing - elit.',
'-',
[
'Lorem ipsum ',
' dolor sit ',
' amet, consectetur adipiscing ',
' elit.',
],
];
yield[
'A class namespace',
'This\\Is\\My\\Class\\For\\Testing',
'\\',
[
'This',
'Is',
'My',
'Class',
'For',
'Testing',
],
];
}
public function provideLastElementOfString(): ?Generator
{
yield[
'An empty string',
'',
'',
null,
];
yield[
'One-character string',
'a',
',',
null,
];
yield[
'String without given separator',
'abc',
',',
null,
];
yield[
'Simple, short string',
'a, b, c',
',',
' c',
];
yield[
'A sentence',
'Lorem ipsum - dolor sit - amet, consectetur adipiscing - elit.',
'-',
' elit.',
];
yield[
'A class namespace',
'This\\Is\\My\\Class\\For\\Testing',
'\\',
'Testing',
];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@@ -244,16 +244,16 @@ class RegexTest extends BaseTestCase
self::assertTrue(Regex::contains($this->simpleText, 'l')); self::assertTrue(Regex::contains($this->simpleText, 'l'));
} }
public function testIsFileName(): void /**
* @param string $description Description of test
* @param string $fileName
* @param bool $expected Expected result
*
* @dataProvider provideFileName
*/
public function testIsFileName(string $description, string $fileName, bool $expected): void
{ {
$filePath = __FILE__; static::assertSame($expected, Regex::isFileName($fileName), $description);
$directoryPath = dirname($filePath);
self::assertTrue(Regex::isFileName($filePath));
self::assertTrue(Regex::isFileName('this-1_2 3 & my! 4+file.jpg'));
self::assertFalse(Regex::isFileName($directoryPath));
self::assertTrue(Regex::isFileName('directory1/directory2/this-1_2 3 & my! 4+file.jpg'));
} }
public function testIsQuoted() public function testIsQuoted()
@@ -2214,6 +2214,39 @@ class RegexTest extends BaseTestCase
]; ];
} }
public function provideFileName(): ?Generator
{
yield[
'An empty string',
'',
false,
];
yield[
'Path of this file, of file with test case',
__DIR__,
false,
];
yield[
'Name of this file, of file with test case',
__FILE__,
true,
];
yield[
'Complicated name of file',
'this-1_2 3 & my! 4+file.jpg',
true,
];
yield[
'Complicated name of file',
'directory1/directory2/this-1_2 3 & my! 4+file.jpg',
true,
];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */